Class: RestaurantListService

Inherits:
Object
  • Object
show all
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

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_keyString

Generate cache key for caching the response

Returns:

  • (String)

    Cache key based on parameters and data freshness



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

#callHash

Execute the restaurant listing logic

Returns:

  • (Hash)

    Response hash with success status, data, and pagination info



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 = apply_master_menu_filter(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.message, params: @params })
    APMErrorHandler.report(e, context: { params: @params })
    { success: false, message: 'Failed to fetch restaurants' }
  end
end