Class: Api::V5::RestaurantTagsController
- Inherits:
-
BaseController
- Object
- ActionController::API
- BaseController
- Api::V5::RestaurantTagsController
- Includes:
- Concerns::GetRestaurantList
- Defined in:
- app/controllers/api/v5/restaurant_tags_controller.rb
Overview
RestaurantTags
Constant Summary
Constants inherited from BaseController
BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema
Instance Method Summary collapse
Methods inherited from BaseController
Methods included from LogrageCustomLogger
Methods included from ResponseCacheConcern
Instance Method Details
#deals ⇒ Object
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/restaurant_tags_controller.rb', line 106 def deals filter = Api::V5::RestaurantTagsFilter.new filter.init_default filter.filter_by_show(true) filter.filter_by(params[:type]) if params[:type].present? filter.filter_by_city(params[:city_id]) if params[:city_id].present? filter.recommended_tag(params[:recommended_tag]) if params[:recommended_tag].present? filter.build_collections set_status_header(true) cache_key_params = "#{I18n.locale}|#{params[:city_id]}|#{params[:type]}|#{params[:recommended_tag]}" cache_key = "#{CACHE_NAMESPACE}:#{self.class}|#{RestaurantTag.maximum(:updated_at)}|#{cache_key_params}" my_response_cache cache_key, :json, public: true do = filter.collections.order(title_en: :asc).group_by(&:category) data = .map do |category, | next if category == RestaurantTag::AWARD_TYPE { category: category, tags: .map do |tag| { id: tag.id, type: 'restaurant_tags', attributes: { cover: tag.cover, title: tag.title_format(for_api: true), title_en: tag.title_format(:en, for_api: true), tag_type: tag.category, total_restaurants: tag.total_restaurants, }, } end, } end { data: data, success: true, message: nil, } end set_status_header(true) end |
#index ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/controllers/api/v5/restaurant_tags_controller.rb', line 10 def index page = params.fetch(:page, {}) filter = Api::V5::RestaurantTagsFilter.new filter.init_default filter.filter_by(params[:type]) if params[:type].present? filter.filter_by_city(params[:city_id]) if params[:city_id].present? filter.recommended_tag(params[:recommended_tag]) if params[:recommended_tag].present? filter.page_number(page.fetch(:number, 1)) filter.per_page(page.fetch(:size, 10)) filter.build_collections set_status_header(true) cache_key = "#{CACHE_NAMESPACE}:#{self.class}|index_expiration|#{filter.cache_key}|#{I18n.locale}" my_response_cache cache_key, :json, public: true do filter.as_json(serialization_context, minor_version_param, {}).merge(success: true, message: nil) end end |
#restaurants ⇒ Object
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 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 95 96 97 98 99 100 101 102 103 104 |
# File 'app/controllers/api/v5/restaurant_tags_controller.rb', line 29 def restaurants restaurant_tag = RestaurantTag.fetch(params.require(:restaurant_tag_id)) if params[:compact_mode].to_s == 'true' # First, we find all restaurant_ids associated with the restaurant_tag_id #{restaurant_tag.id} through restaurant_tags_restaurants = RestaurantTagsRestaurant.where(restaurant_tag_id: restaurant_tag.id).select(:restaurant_id) # Next, we find all restaurant_ids associated with the restaurant_tag_id tag id through primary_tags = PrimaryTag.where(restaurant_tag_id: restaurant_tag.id).select(:restaurant_id) # Combine both sets of restaurant_ids combined_restaurant_ids = .union_all() valid_restaurants = Restaurant.where(id: combined_restaurant_ids.select(:restaurant_id)).active.not_expired # Now, we need to handle the branch_id logic. We find all compact_restaurants that directly match the restaurant_ids # and those that match through the branch_id compact_restaurants_direct = CompactRestaurant.where(restaurant_id: valid_restaurants.where(branch_id: nil).select(:id)) compact_restaurants_via_branch = CompactRestaurant.where(branch_id: valid_restaurants.where.not(branch_id: nil).select(:branch_id)).group(:branch_id) # Combine both sets of compact_restaurants all_compact_restaurants = compact_restaurants_direct.union_all(compact_restaurants_via_branch) # Finally, execute the query to fetch the records compact_restaurants = CompactRestaurant.from(all_compact_restaurants, :compact_restaurants) named_cache_key = "#{CACHE_NAMESPACE}:#{self.class}|#{compact_restaurants.cache_key}|#{Date.current_date}" cache_key = CityHash.hash32([named_cache_key, 'compact', params, I18n.locale, GroupLandingPage.maximum(:updated_at)]) page = params.fetch(:page, {}) my_response_cache cache_key, :json, public: true do group_landing_page_group = restaurant_tag.group_landing_page_groups.where(rank_sort_type: 'manual').first # backward compatibility for old version, we use hh-search for the phase 2 if group_landing_page_group.present? && group_landing_page_group.rank_sort_type.manual? compact_restaurants = compact_restaurants.by_rank_group_landing_page(group_landing_page_group.group_landing_page_id). order('CASE WHEN glpr.rank IS NULL THEN 1 ELSE 0 END, glpr.rank asc') end data = compact_restaurants.page(page.fetch(:number, 1)).per(page.fetch(:size, 10)).includes(:translations, :city, :picture, restaurant: :restaurant_info, branch: :restaurants) response_data = ActiveModelSerializers::SerializableResource.new(data.distinct, { serialization_context: serialization_context, serializer: ActiveModel::Serializer::CollectionSerializer, adapter: :json_api, each_serializer: Api::V5::FeaturedRestaurantSerializer, }).as_json response_data.merge({ success: true, message: nil }) end else # Fetch restaurant IDs using primary_tags association primary_tag_restaurant_ids = restaurant_tag..pluck(:restaurant_id) # Fetch all restaurant IDs secondary_tag_restaurant_ids = restaurant_tag.restaurants.ids # Combine and remove duplicates from both sets of restaurant IDs combined_restaurant_ids = (primary_tag_restaurant_ids + secondary_tag_restaurant_ids).uniq # Fetch and filter restaurants using the combined restaurant IDs restaurants = lambda do Restaurant.where(id: combined_restaurant_ids).active.not_expired end params[:sort_by] ||= :rank named_cache_key = "#{CACHE_NAMESPACE}:#{self.class}|#{restaurants.call.}|#{Date.current_date}" restaurant_list(named_cache_key, restaurants) end rescue ActiveRecord::RecordNotFound render json: { data: [], success: false, message: "Restaurant tag with id #{params[:restaurant_tag_id]} not found.", } end |