summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/reviews_controller.rb22
1 files changed, 17 insertions, 5 deletions
diff --git a/app/controllers/api/reviews_controller.rb b/app/controllers/api/reviews_controller.rb
index 353e382..951e19d 100644
--- a/app/controllers/api/reviews_controller.rb
+++ b/app/controllers/api/reviews_controller.rb
@@ -6,18 +6,22 @@ module Api
skip_before_action :validate_jwt, only: :index
def index
- @product = Product.find_by(public_id: params[:product_id])
+ @product = find_product
render status: :not_found and return if @product.nil?
@reviews = @product.product_reviews.joins(:user_account).select('product_reviews.*',
- 'user_accounts.first_name as author_name')
+ 'user_accounts.first_name as author_name')
render json: serialized_collection.serializable_hash, status: :ok
end
def create
- @review = Review.new(permitted_params.merge({ product_id: params[:product_id],
- user_account_id: current_user_account.id }))
+ @product = find_product
+
+ render status: :not_found and return if @product.nil?
+
+ @review = ProductReview.new(permitted_params.merge({ product_id: @product.id,
+ user_account_id: current_user_account.id }))
if @review.save
render json: serialized_object.serializable_hash, status: :ok
@@ -28,6 +32,10 @@ module Api
private
+ def find_product
+ Product.find_by(public_id: params[:product_id])
+ end
+
def permitted_params
params.permit(:review, :rating)
end
@@ -37,7 +45,11 @@ module Api
end
def serialized_object
- @serialized_object ||= ProductReviewSerializer.new(@review)
+ @serialized_object ||= ProductReviewSerializer.new(
+ ProductReview.joins(:user_account).select('product_reviews.*',
+ 'user_accounts.first_name as author_name')
+ .find(@review.id)
+ )
end
end
end