Class: Api::V5::ReviewsController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::Authorization, AwsTranslationHelper, EncryptableHelper
Defined in:
app/controllers/api/v5/reviews_controller.rb

Overview

typed: ignore frozen_string_literal: true

Constant Summary

Constants inherited from BaseController

BaseController::CACHE_NAMESPACE, BaseController::INTERNAL_SERVER_ERROR_MESSAGE, BaseController::ResponseSchema

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EncryptableHelper

#decrypt, #encrypt, #generate_signature

Methods included from AwsTranslationHelper

#aws_translate_client

Methods inherited from BaseController

#identity_cache_memoization

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ResponseCacheConcern

#my_response_cache

Instance Attribute Details

#restaurantObject (readonly)

Returns the value of attribute restaurant.



9
10
11
# File 'app/controllers/api/v5/reviews_controller.rb', line 9

def restaurant
  @restaurant
end

Instance Method Details

#createObject



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
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/api/v5/reviews_controller.rb', line 66

def create
  reservation = Reservation.fetch(params.require(:review).require(:reservation_id))
  restaurant = reservation.restaurant
  unless reservation.user_id == current_user.id
    raise ApiV5::Errors::AuthenticationError,
          'You are not authorized to perform this operation'
  end

  review = reservation.review || reservation.build_review
  result = false
  errors = nil
  ActiveRecord::Base.transaction do
    operation = nil
    if first_step_params.present? && !review.persisted?
      review.assign_attributes first_step_params
      first_step = ReviewService::Create.new(review, current_user)
      first_step.disclose_review = params[:review][:disclose_review].to_s == 'true'
      first_step.object.branch_id = restaurant.branch_id
      first_step.object.restaurant_id = restaurant.id
      operation = first_step.execute
      unless operation.success?
        errors = operation.errors
        raise ActiveRecord::Rollback
      end
    end

    reservation.reload
    reservation.review.reload
    review = reservation.review
    if second_step_params.present? && review.persisted?
      second_step = ReviewService::SecondStep.new(review.id, second_step_params)
      second_step.object.branch_id = restaurant.branch_id
      second_step.object.restaurant_id = restaurant.id
      operation = second_step.execute
      unless operation.success?
        errors = operation.errors
        raise ActiveRecord::Rollback
      end
      result = true
      review.reload
    end
  end
  if result
    render json: resource_as_json(review, Api::V5::ReviewSerializer, {}).merge(
      success: true,
      message: I18n.t('actions.review.created'),
    )
  else
    render json: { data: nil, success: false, message: errors.full_messages.to_sentence }
  end
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/api/v5/reviews_controller.rb', line 13

def index
  @restaurant = Restaurant.fetch_by_id(params.require(:restaurant_id))
  return render '/404', status: :not_found if @restaurant.nil?

  permitted_params = params.dup.permit!.slice(:page, :sort, :include_pictures).to_h
  keys = CityHash.hash32([permitted_params, I18n.locale, Date.today, restaurant.id, restaurant.cache_key])
  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:index:#{keys}:index_v1"
  my_response_cache(cache_key, :json, public: true) do
    page = params.fetch(:page, {})
    filter = Api::V5::ReviewsFilter.new
    filter.scope_by(branch_id: restaurant.branch_id, restaurant_id: restaurant.id)
    filter.page_number(page[:number].presence || 1).
      per_page(page[:size].presence || 10).
      use_default_order.
      sort_by(params[:sort])
    includes = []
    includes.push('pictures') if params.key?(:include_pictures) && params[:include_pictures].to_s == 'true'
    options = {
      include: includes,
    }
    filter.as_json(serialization_context, minor_version_param, options).merge(success: true, message: nil)
  end
  set_status_header(true)
end

#searchObject



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
# File 'app/controllers/api/v5/reviews_controller.rb', line 38

def search
  @restaurant = Restaurant.fetch_by_id(params.require(:restaurant_id))
  return render '/404', status: :not_found if @restaurant.nil?

  permitted_params = params.dup.permit!.slice(:page, :sort, :include_pictures).to_h
  keys = CityHash.hash32(
    [permitted_params, I18n.locale, Time.zone.today, restaurant.id, restaurant.cache_key, params[:rating]],
  )
  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:search:#{keys}"
  my_response_cache(cache_key, :json, public: true) do
    page = params.fetch(:page, {})
    filter = Api::V5::ReviewsFilter.new
    filter.scope_by(branch_id: restaurant.branch_id, restaurant_id: restaurant.id)
    filter.filter_rating(rating: params[:rating])
    filter.use_default_order
    filter.page_number(page[:number].presence || 1).
      per_page(page[:size].presence || 10).
      sort_by(params[:sort])
    includes = []
    includes.push('pictures') if params.key?(:include_pictures) && params[:include_pictures].to_s == 'true'
    options = {
      include: includes,
    }
    filter.as_json(serialization_context, minor_version_param, options).merge(success: true, message: nil)
  end
  set_status_header(true)
end

#showObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/api/v5/reviews_controller.rb', line 164

def show
  review_id = Review.decrypt_id(params.require(:id))
  review = Review.find_by(id: review_id)
  return render_not_found unless review

  cache_key = "#{CACHE_NAMESPACE}:#{self.class}:show:#{review.cache_key}:#{MyLocaleManager.normalize_locale}"
  my_response_cache(cache_key, :json, public: true) do
    resource_as_json(review, Api::V5::ReviewSerializer, {}).merge(success: true, message: nil)
  end
rescue ActionController::ParameterMissing => e
  render json: { success: false, error: e.message }, status: :unprocessable_entity
end

#translateObject



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
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/api/v5/reviews_controller.rb', line 118

def translate
  # Check if text or review ID is provided
  if params[:id].present?
    review = Review.find_by(id: params[:id])
    return render_not_found unless review

    text_to_translate = review.review || translate_params[:review]
  else
    text_to_translate = translate_params[:review]
  end

  return render_review_required unless text_to_translate.present?

  target_language_code = if translate_params[:target_language_code].blank?
                           'en'
                         elsif translate_params[:target_language_code] == 'cn'
                           'zh-HK'
                         else
                           translate_params[:target_language_code]
                         end

  if review.present?
    review.translated_reviews ||= []

    existing_translation = review.translated_reviews.find { |t| t['target_language_code'] == target_language_code }

    if existing_translation
      return render_translation(review, existing_translation['translated_text'], target_language_code)
    end
  end

  translated_text = translate_text(text_to_translate, target_language_code)

  if review.present?
    review.translated_reviews << { target_language_code: target_language_code, translated_text: translated_text }

    review.update(translated_reviews: review.translated_reviews)
  end

  if review.present?
    render_translation(review, translated_text, target_language_code)
  else
    render json: { data: { review: translated_text } }
  end
end