diff options
author | HombreLaser <sebastian-440@live.com> | 2023-04-01 13:07:39 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-04-01 13:07:39 -0600 |
commit | d8842d83862fc26de22b3a6ed0a89e56f87b2e7e (patch) | |
tree | 00f1c4e2c38d3de97e418ddaf1227ebb0f176677 /app/controllers | |
parent | bd3a6121c068f871dc5bd40c79c052e33fcd330e (diff) |
Añade controlador de Reviews
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/api/reviews_controller.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/app/controllers/api/reviews_controller.rb b/app/controllers/api/reviews_controller.rb new file mode 100644 index 0000000..8a5de64 --- /dev/null +++ b/app/controllers/api/reviews_controller.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +# ReviewController +class ReviewsController < AuthenticatedController + skip_before_action only: [:index] + + def index + @product = Product.find_by(public_id: params[:product_id]) + + render status: :not_found if @product.nil? + + @reviews = @product.product_reviews.joins(:user_account).select('product_reviews.*', + '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 })) + + if @review.save + render json: serialized_object.serializable_hash, status: :ok + else + render json: { error_messages: @review.errors.full_messages }, status: :unprocessable_entity + end + end + + private + + def permitted_params + params.permit(:review, :rating) + end + + def serialized_collection + @serialized_collection ||= ProductReviewSerializer.new(@reviews) + end + + def serialized_object + @serialized_object ||= ProductReviewSerializer.new(@review) + end +end |