Class: Admin::Restaurants::RestaurantTagsController

Inherits:
BaseController show all
Defined in:
app/controllers/admin/restaurants/restaurant_tags_controller.rb

Overview

Responsible to manage restaurant's tag

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#destroy_session, #identity_cache_memoization, #sign_in_page, #user_developer_session

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from AdminHelper

#dynamic_pricings_formatter, #link_to_admin_reservations_path_by_id, #link_to_admin_restaurants_path_by_id, #link_to_log, #optional_locales, #optional_locales_with_labels, #staff_signed_in?

Methods included from UpdateLocaleConcern

#setup_locale

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Attribute Details

#restaurantObject (readonly)

Returns the value of attribute restaurant.



10
11
12
# File 'app/controllers/admin/restaurants/restaurant_tags_controller.rb', line 10

def restaurant
  @restaurant
end

Instance Method Details

#create_or_update_bulkObject



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
# File 'app/controllers/admin/restaurants/restaurant_tags_controller.rb', line 30

def create_or_update_bulk
  restaurant.hashtag_tags.each do |tag|
    restaurant.restaurant_tags.delete tag.id
  end

  params.require(:restaurant_tags).permit(tags: [:label, :value])[:tags].values.each do |tag|
    restaurant_tag = if tag.key?('value') && tag['value'].to_i.positive?
                       RestaurantTag.fetch(tag['value'])
                     else
                       title = "Hashtags:#{tag['label']}"
                       RestaurantTag.find_or_create_by(title_en: title, title_th: title)
                     end
    restaurant.restaurant_tags << restaurant_tag
  end

  tags = restaurant.hashtag_tags.map do |tag|
    {
      label: tag.title_format('en'),
      value: tag.id,
    }
  end

  if restaurant.bookable_and_not_expired?
    EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
      EventDrivenClient::Constants::RESTAURANTS_TOPIC,
      EventDrivenClient::Constants::UPDATE_EVENT,
      restaurant.id,
      { hashtags: {} },
      { source: 'Admin::Restaurants::RestaurantTagsController#create_or_update_bulk' },
    )
  end

  render json: tags
end

#destroy_allObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/admin/restaurants/restaurant_tags_controller.rb', line 65

def destroy_all
  restaurant.hashtag_tags.each do |tag|
    restaurant.restaurant_tags.delete(tag.id)
  end

  if restaurant.bookable_and_not_expired?
    EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
      EventDrivenClient::Constants::RESTAURANTS_TOPIC,
      EventDrivenClient::Constants::UPDATE_EVENT,
      restaurant.id,
      { hashtags: {} },
      { source: 'Admin::Restaurants::RestaurantTagsController#destroy_all' },
    )
  end

  render json: { success: true }
end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/admin/restaurants/restaurant_tags_controller.rb', line 12

def index
  restaurant_tags = if params[:category].present?
                      restaurant.restaurant_tags.where_category(params[:category])
                    else
                      restaurant.restaurant_tags
                    end
  if params[:serializer] == 'dropdown'
    restaurant_tags = restaurant_tags.map do |tag|
      {
        label: tag.title_format(params.require(:tag_locale)),
        value: tag.id,
      }
    end
  end

  render json: restaurant_tags
end