Class: RestaurantListService
- Inherits:
-
Object
- Object
- RestaurantListService
- Includes:
- ElasticAPM::SpanHelpers
- Defined in:
- app/services/restaurant_list_service.rb
Overview
Service class to handle restaurant listing logic, extracting complex logic from controller Provides pagination, filtering, and caching capabilities for restaurant data
Instance Method Summary collapse
-
#cache_key ⇒ String
Generate cache key for caching the response.
-
#call ⇒ Hash
Execute the restaurant listing logic.
-
#initialize(params) ⇒ RestaurantListService
constructor
A new instance of RestaurantListService.
Constructor Details
#initialize(params) ⇒ RestaurantListService
Returns a new instance of RestaurantListService.
6 7 8 |
# File 'app/services/restaurant_list_service.rb', line 6 def initialize(params) @params = params end |
Instance Method Details
#cache_key ⇒ String
Generate cache key for caching the response
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/services/restaurant_list_service.rb', line 12 def cache_key [ self.class.to_s, CityHash.hash32(@params), 'restaurant_lists', I18n.locale, @params[:name_like], "page:#{page}", "per_page:#{per_page}", "updated_at:#{Restaurant.maximum(:updated_at).to_i}", ].join('|') end |
#call ⇒ Hash
Execute the restaurant listing logic
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/services/restaurant_list_service.rb', line 27 def call ElasticAPM.with_span('RestaurantListService#call', 'app') do restaurants = build_base_query restaurants = (restaurants) restaurants = apply_name_filter(restaurants) if @params[:name_like].present? total_entries = restaurants.count restaurants = paginate_restaurants(restaurants) restaurants_data = format_restaurants_data(restaurants) build_response(restaurants_data, total_entries) rescue StandardError => e HH_LOGGER.error('Error in RestaurantListService#call', { error: e., params: @params }) APMErrorHandler.report(e, context: { params: @params }) { success: false, message: 'Failed to fetch restaurants' } end end |