Class: Api::V5::GroupLandingPagesController

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

Overview

GroupLandingPages

Constant Summary

Constants inherited from BaseController

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

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

#budgetsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api/v5/group_landing_pages_controller.rb', line 31

def budgets
  cache_key = "#{self.class}:budgets:#{GroupLandingPage.where(group_single_type: 'Budget').cache_key}:#{I18n.locale}"
  my_response_cache cache_key, :json, public: true do
    data = GroupLandingPage.includes(:translations).where(group_single_type: 'Budget').
      limit(3).order(budget_min_cents: :asc)
    options = {
      serialization_context: serialization_context,
      serializer: ActiveModel::Serializer::CollectionSerializer,
      adapter: :json_api,
      each_serializer: Api::V5::GroupLandingPageSerializer,
    }

    result = render_to_string(
      json: ActiveModelSerializers::SerializableResource.new(data, options).as_json.merge(
        success: true, message: nil,
      ),
    )
    JSON.parse result
  end
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/api/v5/group_landing_pages_controller.rb', line 6

def index
  # Use timestamp of latest record instead of loading all records
  cache_key = "#{self.class}:index:#{GroupLandingPage.maximum(:updated_at).to_i}:#{I18n.locale}"
  my_response_cache cache_key, :json, public: true do
    active_glp = GroupLandingPage.active.includes(:translations, :group_landing_page_groups)

    # Filter out group landing pages with no content
    glp_with_content = active_glp.reject(&:has_no_content?)

    options = {
      serialization_context: serialization_context,
      serializer: ActiveModel::Serializer::CollectionSerializer,
      adapter: :json_api,
      each_serializer: Api::V5::GroupLandingPageSerializer,
    }

    result = render_to_string(
      json: ActiveModelSerializers::SerializableResource.new(glp_with_content, options).as_json.merge(
        success: true, message: nil,
      ),
    )
    JSON.parse result
  end
end

#reviewsObject



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/api/v5/group_landing_pages_controller.rb', line 66

def reviews
  review_type = params.require(:review_type)
  city_id = params[:city_id].to_i if params[:city_id].present?

  landing_page = begin
    GroupLandingPage.friendly.find(params.require(:group_landing_page_slug))
  rescue StandardError
    nil
  end
  return render json: { success: false, data: [], message: 'not found' } if landing_page.nil?

  group_single_type = landing_page.group_single_type
  landing_page_groups = landing_page.group_landing_page_groups

  if group_single_type.present?
    compact_restaurants_filter = CompactRestaurantsFilter.new
    compact_restaurants = if group_single_type == 'Top'
                            compact_restaurants_filter.sort_by(:top).collections
                          elsif group_single_type == 'New'
                            compact_restaurants_filter.sort_by(:new).collections
                          elsif group_single_type == 'Featured'
                            compact_restaurants_filter.featured_only.sort_by(:rank).collections
                          end
  end

  restaurant_ids = if group_single_type.present? && compact_restaurants&.any?
                     compact_restaurants&.select(&:restaurant)&.map(&:restaurant)&.pluck(:id)
                   elsif landing_page_groups.present?
                     restaurant_ids = []
                     landing_page_groups.each do |glpg|
                       group = glpg.group
                       next if group.blank?

                       restaurant_ids << group.restaurant_ids
                     end
                     restaurant_ids.flatten
                   else
                     []
                   end

  if city_id.present?
    # filter restaurants by city
    restaurant_ids.select! do |restaurant_id|
      restaurant = Restaurant.find_by(id: restaurant_id)
      restaurant.present? && restaurant.city_id == city_id
    end
  end

  cache_key = "#{self.class}:reviews:#{landing_page.cache_key}:#{city_id}:#{Time.zone.today}:#{review_type}:#{MyLocaleManager.normalize_locale}"
  my_response_cache cache_key, :json, public: true, public_expires_in: 24.hours do
    case review_type
    when 'user'
      filter = Api::V5::ReviewsFilter.new
      filter.scope_by(restaurant_id: restaurant_ids, branch_id: nil).page_number(1).per_page(20)
      user_reviews = filter.collections.where(rating: 5).where("review_photos_count > 3 AND reviews.review IS NOT NULL AND reviews.review <> ''").order('created_at DESC')

      options = {
        include: 'restaurant',
        adapter: :json_api,
        each_serializer: Api::V5::ReviewSerializer,
        serialization_context: serialization_context,
      }
      result = render_to_string(json: ActiveModelSerializers::SerializableResource.new(user_reviews, options).as_json.merge(
        success: true, message: nil,
      ))
      JSON.parse result
    when 'blogger'
      filter = Api::V5::ReviewsFilter.new
      filter.scope_by_blogger(restaurant_id: restaurant_ids, branch_id: nil).page_number(1).per_page(20)
      blogger_reviews = filter.collections.order('priority DESC')
      options = {
        include: 'restaurant',
        adapter: :json_api,
        each_serializer: Api::V5::BloggerReviewSerializer,
        serialization_context: serialization_context,
      }
      result = render_to_string(json: ActiveModelSerializers::SerializableResource.new(blogger_reviews, options).as_json.merge(
        success: true, message: nil,
      ))
      JSON.parse result
    else
      raise NotImplementedError
    end
  end
end

#showObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/api/v5/group_landing_pages_controller.rb', line 52

def show
  @group_landing_page = GroupLandingPage.friendly.find(params.require(:slug))
  cache_key = "#{self.class}:show:#{@group_landing_page.cache_key}:#{I18n.locale}"
  my_response_cache cache_key, :json do
    JSON.parse(
      render_to_string(json: resource_as_json(@group_landing_page, Api::V5::GroupLandingPageSerializer, {}).merge(
        success: true, message: nil,
      )),
    )
  end
rescue ActiveRecord::RecordNotFound, ActionView::MissingTemplate
  head :not_found
end