Class: Api::V5::BannersController

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

Overview

used in promotion page on website

Constant Summary

Constants inherited from BaseController

Api::V5::BaseController::CACHE_NAMESPACE, Api::V5::BaseController::INTERNAL_SERVER_ERROR_MESSAGE, Api::V5::BaseController::ResponseSchema

Instance Method Summary collapse

Methods included from ImageHelper

#fix_image_url

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#indexObject

Android has limitation, it can't send GET data with JSON body so we allow them to send String restaurant_tag_ids



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
60
61
# File 'app/controllers/api/v5/banners_controller.rb', line 10

def index
  page = params.fetch(:page, {})
  city_id = params[:city_id]
  restaurant_tag_id = params[:restaurant_tag_id]
  cache_key = [CACHE_NAMESPACE,
               self.class,
               'index',
               Banner.maximum(:updated_at),
               Time.zone.today,
               params[:restaurant_tag_ids],
               params[:restaurant_id],
               city_id,
               I18n.locale,
               page,
               restaurant_tag_id].join(':')
  return unless stale? cache_key, template: false

  my_response_cache cache_key, :json, public: true do
    banners = Banner.includes(:cities)

    if restaurant_tag_id.blank?
      banners = banners.includes(:translations).with_locale(I18n.locale)
      banners = banners.where(cities: { id: nil }).or(banners.where(cities: { id: city_id })) if city_id
      banners = banners.where(active: true)
    end

    banners = banners.page(page[:number]).per(page[:size]).order('order_number ASC')

    if params[:restaurant_id].present?
      restaurant = Restaurant.fetch params[:restaurant_id]
      banners = banners.where(restaurant_tag_id: restaurant.restaurant_tag_ids).includes(:restaurant_tag)
    elsif params[:restaurant_tag_ids].present?
      restaurant_tag_ids = if params[:restaurant_tag_ids].is_a?(String)
                             params[:restaurant_tag_ids].split(',').compact
                           else
                             params[:restaurant_tag_ids]
                           end
      banners = banners.where(restaurant_tag_id: Array.wrap(restaurant_tag_ids)).includes(:restaurant_tag)
    elsif restaurant_tag_id.present?
      banners = banners.where(restaurant_tag_id: restaurant_tag_id).includes(:restaurant_tag)
    else
      banners = banners.where(restaurant_tag_id: nil, restaurant_tag_group_id: nil)
    end
    banners.map do |banner|
      banner.attributes.merge('mobile_versions' => fix_url(banner.mobile_versions),
                              'desktop_versions' => fix_url(banner.desktop_versions),
                              'mobile_retina_versions' => fix_url(banner.mobile_retina_versions),
                              'desktop_retina_versions' => fix_url(banner.desktop_retina_versions))
    end
  end
  set_status_header(true)
end