summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/reviews_controller.rb4
-rw-r--r--app/models/product_review.rb7
2 files changed, 9 insertions, 2 deletions
diff --git a/app/controllers/api/reviews_controller.rb b/app/controllers/api/reviews_controller.rb
index 951e19d..1f48362 100644
--- a/app/controllers/api/reviews_controller.rb
+++ b/app/controllers/api/reviews_controller.rb
@@ -26,7 +26,7 @@ module Api
if @review.save
render json: serialized_object.serializable_hash, status: :ok
else
- render json: { error_messages: @review.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @review.errors.as_json }, status: :unprocessable_entity
end
end
@@ -48,7 +48,7 @@ module Api
@serialized_object ||= ProductReviewSerializer.new(
ProductReview.joins(:user_account).select('product_reviews.*',
'user_accounts.first_name as author_name')
- .find(@review.id)
+ .find(@review.id)
)
end
end
diff --git a/app/models/product_review.rb b/app/models/product_review.rb
index ceade9a..0985e7c 100644
--- a/app/models/product_review.rb
+++ b/app/models/product_review.rb
@@ -9,4 +9,11 @@ class ProductReview < ApplicationRecord
validates :review, presence: true
validates :rating, presence: true, inclusion: { in: 1..5 }
+ validate :sole_user_review
+
+ def sole_user_review
+ return if ProductReview.find_by(user_account_id:, product_id:).nil?
+
+ errors.add(:review, 'User has already published a review')
+ end
end