From a7f095d7171fe496c8037a451aaecfd234c30a01 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Tue, 4 Apr 2023 17:27:21 -0600 Subject: Añade índice entre productos, autores y reseñas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/reviews_controller.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'app') 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 -- cgit v1.2.3