Class: Api::Aoa::V1::RestaurantsController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::GetRestaurantList
Defined in:
app/controllers/api/aoa/v1/restaurants_controller.rb

Overview

controller restaurant for aoa

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE

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 Method Details

#blogger_reviewsObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/api/aoa/v1/restaurants_controller.rb', line 93

def blogger_reviews
  resource = Restaurant.fetch params.require(:restaurant_id)

  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:blogger_reviews:#{CityHash.hash32([I18n.locale, params,
                                                                                   resource.cache_key])}"
  my_response_cache cache_key, :json, public: true, public_expires_in: 24.hours do
    page_param = params.require(:page)
    per_page = page_param.fetch(:size, 100).to_i
    page = page_param.fetch(:number, 1).to_i
    reviews_filter(resource, page, per_page)
  end
end

#restaurant_pictureObject



106
107
108
109
110
# File 'app/controllers/api/aoa/v1/restaurants_controller.rb', line 106

def restaurant_picture
  expires_in 1.day, public: true
  response = Api::Aoa::V1::RestaurantPictureSerializer.new(@restaurant).as_json.merge(success: true, message: nil)
  render json: response
end

#searchObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/api/aoa/v1/restaurants_controller.rb', line 8

def search
  RequestStore.store[:skip_host] = false

  params[:package_type] = 'ayce,pp,xp,hs,bfp,sm' if params[:package_type].blank?
  price_params = params[:price]

  if price_params.present?
    min = price_params[:x].to_i
    max = price_params.fetch(:y, HhPackage::Pricing::MAX_SEAT).to_i
    params[:price_filter] = {
      min: min,
      max: max,
    }
  end

  location_params = params[:location_ids]
  if location_params.present?
    params[:sort] = 'distance_location'
  end

  cache_key = CityHash.hash32([self.class.to_s, Time.zone.today, Time.zone.now.hour, I18n.locale, params])

  aoa_restaurant_list("#{CACHE_NAMESPACE}:#{self.class}|#{cache_key}|search")
end

#showObject



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
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
# File 'app/controllers/api/aoa/v1/restaurants_controller.rb', line 33

def show
  RequestStore.store[:skip_host] = false
  id = params.require(:id)
  resource = Restaurant.fetch(id)
  now = Time.now_in_tz(resource.time_zone)
  date = now.to_date
  hour = now.strftime('%H')
  minutes = Time.up_to_nearest_15(now.strftime('%M').to_i)
  cache_key = CityHash.hash32([self.class.to_s,
                               resource.view_cache_key,
                               params,
                               I18n.locale,
                               RequestStore.store[:skip_host],
                               date, hour, minutes])
  my_response_cache("show:#{cache_key}", :json, public: true) do
    resource = Restaurant.find(id).decorate
    includes = []
    includes.push('pictures') if params.key?(:include_pictures) && params[:include_pictures].to_s == 'true'
    includes.push('restaurant_packages') if params.key?(:include_packages) && params[:include_packages].to_s == 'true'
    if params.key?(:include_last_reviews) && params[:include_last_reviews].to_s == 'true'
      includes.push('last_reviews')
    end
    if params.key?(:include_blogger_reviews) && params[:include_blogger_reviews].to_s == 'true'
      includes.push('blogger_reviews')
    end
    options = {
      include: includes,
      user: user_signed_in? ? current_user : nil,
    }
    reviews_filter = Api::V5::ReviewsFilter.new
    reviews_filter.scope_by(restaurant_id: resource.id)
    reviews_filter.use_default_order

    review_stat = resource.review_stat
    review_stat = Restaurants::ReviewStat.new if review_stat.blank?
    review_stat.attributes.extract! 'id', 'one_star'

    options[:meta] = {
      reviews: {
        total: review_stat.total.to_i,
        average: resource&.weighted_score&.round(1) || 0,
        stars_count: {
          one: review_stat.one_star.to_f,
          two: review_stat.two_star.to_f,
          three: review_stat.three_star.to_f,
          four: review_stat.four_star.to_f,
          five: review_stat.five_star.to_f,
        },
      },
      criteo_item: [{ id: resource.id, quantity: 1, price: resource.price_per_person(as_string: false) }],
    }
    resource_as_json(resource, Api::Aoa::V1::RestaurantSerializer, options).merge(success: true, message: nil)
  end
  set_status_header(true)
rescue ActiveRecord::RecordNotFound, Hashids::InputError
  set_status_header(false)
  response = { success: false, data: [], message: 'Sorry we can not identify your requested restaurant' }
  render json: response
end