Class: Admin::TagsController

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

Constant Summary

Constants inherited from BaseController

BaseController::INTERNAL_SERVER_ERROR_MESSAGE

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 Method Details

#indexObject



10
11
12
13
14
15
# File 'app/controllers/admin/tags_controller.rb', line 10

def index
  @categories = RestaurantTag.where('title_en LIKE ?', 'Categories:%').order(:id)
  @categories_short_names =
    %w(Romantic Lunch Brunch Bro Girls Mixologists Cafe Family Healthy Insta)
  @restaurant_tag_groups = RestaurantTagGroup.exclude_deleted.order_by_rank.includes(:translations)
end

#index_categoryObject



81
82
83
84
85
86
87
88
# File 'app/controllers/admin/tags_controller.rb', line 81

def index_category
  @categories = []
  @restaurant_categories = []
  RestaurantTag.where('title_en LIKE ?', 'Categories:%').order(:id).each do |c|
    @restaurant_categories[c.id] = c.restaurant_ids
    @categories.push(id: c.id, name: "cat#{c.id}")
  end
end

#index_restaurant_tag_groupsObject



135
136
137
138
139
140
141
142
# File 'app/controllers/admin/tags_controller.rb', line 135

def index_restaurant_tag_groups
  @categories = []
  @restaurant_categories = {}
  RestaurantTagGroup.exclude_deleted.includes(:restaurants).order_by_rank.each do |c|
    @restaurant_categories["c-#{c[:id]}"] = c.restaurants.pluck(:id)
    @categories.push(id: c.id, name: "restaurant-tag-#{c.id}")
  end
end

#index_tagObject



17
18
19
20
21
22
23
24
# File 'app/controllers/admin/tags_controller.rb', line 17

def index_tag
  @new_restaurants = Restaurant.joins(:restaurant_tags).where('restaurant_tags.title_en LIKE ?',
                                                              'Admin:New').pluck(:id)
  @recommended_restaurants = Restaurant.joins(:restaurant_tags).where('restaurant_tags.title_en LIKE ?',
                                                                      'Admin:Recommended').pluck(:id)
  @popular_restaurants = Restaurant.joins(:restaurant_tags).where('restaurant_tags.title_en LIKE ?',
                                                                  'Admin:MostPopular')
end

#set_categoryObject



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
# File 'app/controllers/admin/tags_controller.rb', line 90

def set_category
  checked = params[:checked]
  id = params[:id]
  type = params[:type]
  if checked.present? && id.present? && type.present?
    restaurant = Restaurant.find params[:id]
    tag = RestaurantTag.find(type)

    if checked == 'true'
      restaurant.restaurant_tags << tag

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAGS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag.id,
        {
          add: [id],
          remove: [],
        },
      )

      render json: { status: 'true', type: type, id: id }.to_json
    else
      restaurant.restaurant_tags.delete tag

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAGS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag.id,
        {
          add: [],
          remove: [id],
        },
      )

      render json: { status: 'false', type: type, id: id }.to_json
    end
  else
    # status: :unprocessable_entity}
    render json: { status: 'failed', message: 'Invalid parameter' }
  end
end

#set_restaurant_tag_groupsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/admin/tags_controller.rb', line 144

def set_restaurant_tag_groups
  checked = params[:checked]
  id = params[:id]
  type = params[:type]
  if checked.present? && id.present? && type.present?
    restaurant = Restaurant.find id
    tag = RestaurantTagGroup.exclude_deleted.find(type)

    if checked == 'true'
      tag.restaurants << restaurant

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAG_GROUPS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag.id,
        {
          add: [id],
          remove: [],
        },
      )

      render json: { status: 'true', type: type, id: id }.to_json
    else
      tag.restaurants.delete restaurant

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAG_GROUPS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag.id,
        {
          add: [],
          remove: [id],
        },
      )

      render json: { status: 'false', type: type, id: id }.to_json
    end
  else
    render json: { status: 'failed', message: 'Invalid parameter' }
  end
end

#set_tagObject



26
27
28
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
# File 'app/controllers/admin/tags_controller.rb', line 26

def set_tag
  checked = params[:checked]
  id = params[:id]
  type = params[:type]
  if checked.present? && id.present? && type.present?
    restaurant = Restaurant.find params[:id]
    tag_id = case type
             when 'new'
               170
             when 'recommend'
               171
             when 'popular'
               173
             else
               render(json: { status: :unprocessable_entity, message: 'Invalid parameter' }) && return
             end

    tag = RestaurantTag.find(tag_id)
    if checked == 'true'
      restaurant.restaurant_tags << tag

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAGS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag_id,
        {
          add: [id],
          remove: [],
        },
      )

      render json: { status: 'true', type: type, id: id }.to_json
    else
      restaurant.restaurant_tags.delete tag

      # Send update event to Kafka
      EventDrivenWorkers::HhSearch::ProducerWorker.perform_async(
        EventDrivenClient::Constants::RESTAURANTS_TAGS_TOPIC,
        EventDrivenClient::Constants::UPDATE_EVENT,
        tag_id,
        {
          add: [],
          remove: [id],
        },
      )

      render json: { status: 'false', type: type, id: id }.to_json
    end
  else
    # status: :unprocessable_entity}
    render json: { status: 'failed', message: 'Invalid parameter' }
  end
end