From 72ccdfdf9bbea6269bc1a28199373abd5e8ce302 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Tue, 4 Apr 2023 19:53:27 -0600 Subject: AƱade specs de ReviewsController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/reviews_controller.rb | 8 +++++++- app/models/product_review.rb | 2 ++ .../reviews/has_user_already_posted_review_query.rb | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/queries/reviews/has_user_already_posted_review_query.rb (limited to 'app') diff --git a/app/controllers/api/reviews_controller.rb b/app/controllers/api/reviews_controller.rb index 1f48362..52e7ec3 100644 --- a/app/controllers/api/reviews_controller.rb +++ b/app/controllers/api/reviews_controller.rb @@ -10,6 +10,12 @@ module Api render status: :not_found and return if @product.nil? + if params[:by].present? + render json: { posted: Reviews::HasUserAlreadyPostedReviewQuery.new(@product).review_posted?(params[:by]) }, + status: :ok + return + end + @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 @@ -41,7 +47,7 @@ module Api end def serialized_collection - @serialized_collection ||= ProductReviewSerializer.new(@reviews) + @serialized_collection ||= ProductReviewSerializer.new(@reviews.page(params[:page])) end def serialized_object diff --git a/app/models/product_review.rb b/app/models/product_review.rb index 0985e7c..85804f2 100644 --- a/app/models/product_review.rb +++ b/app/models/product_review.rb @@ -11,6 +11,8 @@ class ProductReview < ApplicationRecord validates :rating, presence: true, inclusion: { in: 1..5 } validate :sole_user_review + paginates_per 15 + def sole_user_review return if ProductReview.find_by(user_account_id:, product_id:).nil? diff --git a/app/queries/reviews/has_user_already_posted_review_query.rb b/app/queries/reviews/has_user_already_posted_review_query.rb new file mode 100644 index 0000000..182f12e --- /dev/null +++ b/app/queries/reviews/has_user_already_posted_review_query.rb @@ -0,0 +1,14 @@ +# frozen_string_literal + +module Reviews + # HasUserAlreadyPostedReviewQuery + class HasUserAlreadyPostedReviewQuery + def initialize(product) + @product = product + end + + def review_posted?(user_account_id) + ProductReview.find_by(user_account_id:, product_id: @product.id).present? + end + end +end -- cgit v1.2.3