summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-04 17:27:21 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-04 17:27:21 -0600
commita7f095d7171fe496c8037a451aaecfd234c30a01 (patch)
tree9c4115830ff339e48e8e6565908749de37eb21fa /app/controllers
parent283a368edd5d5c13aa8653156dd601505326864e (diff)
Añade índice entre productos, autores y reseñas
Diffstat (limited to 'app/controllers')
-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