Class: Api::V5::AddOnPackagesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v5/add_on_packages_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Attribute Details

#restaurant_add_onObject (readonly)

Returns the value of attribute restaurant_add_on.



6
7
8
# File 'app/controllers/api/v5/add_on_packages_controller.rb', line 6

def restaurant_add_on
  @restaurant_add_on
end

Instance Method Details

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/api/v5/add_on_packages_controller.rb', line 8

def index
  restaurant = Restaurant.fetch(params.require(:restaurant_id))
  add_on_packages = restaurant.restaurant_add_ons.active_scope
  total_limit = begin
    AdminSetting.total_restaurant_add_ons_display_limit_on_client_app
  rescue StandardError
    30
  end
  preview_mode = if params[:is_visible_for_staff].present?
                   params[:is_visible_for_staff].to_s == 'true'
                 else
                   false
                 end
  options = {
    include: if params.key?('include_restaurant') && params['include_restaurant'].to_s == 'true'
               [
                 'restaurant',
               ]
             else
               []
             end,
  }

  cache_key_params = [restaurant.id, total_limit, preview_mode, options, I18n.locale]
  cache_key = "#{self.class}:index:#{add_on_packages.maximum(:updated_at)}:#{cache_key_params.join(':')}"

  my_response_cache cache_key, :json, public: true do
    add_on_packages = add_on_packages.exclude_preview unless preview_mode

    ActiveModelSerializers::SerializableResource.new(
      add_on_packages.first(total_limit), {
        serializer: ActiveModel::Serializer::CollectionSerializer,
        options: options,
        adapter: :json_api,
        each_serializer: Api::V5::AddOnPackageSerializer,
      }
    ).as_json.merge(status: true, message: nil)
  end
  set_status_header(true)
rescue ActionController::ParameterMissing => e
  render json: {
    success: false,
    message: e.message,
    data: nil,
  }, status: :unprocessable_entity
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    message: 'restaurant not found',
    data: nil,
  }, status: :not_found
end

#showObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/api/v5/add_on_packages_controller.rb', line 61

def show
  options = {
    include: if params.key?('include_restaurant') && params['include_restaurant'].to_s == 'true'
               [
                 'restaurant',
               ]
             else
               []
             end,
  }
  preview_mode = if params[:is_visible_for_staff].present?
                   params[:is_visible_for_staff].to_s == 'true'
                 else
                   false
                 end

  if restaurant_add_on.preview? && !preview_mode
    render json: {
      success: false,
      message: 'add-on package not found',
      data: nil,
    }, status: :not_found
    return
  end

  cache_key_params = [options, preview_mode, I18n.locale]
  cache_key = "#{self.class}:show:#{restaurant_add_on.cache_key}:#{cache_key_params.join(':')}"

  my_response_cache cache_key, :json, public: true do
    resource_as_json(restaurant_add_on, Api::V5::AddOnPackageSerializer, options).
      merge(status: true, message: nil)
  end
  set_status_header(true)
end