From d8842d83862fc26de22b3a6ed0a89e56f87b2e7e Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 1 Apr 2023 13:07:39 -0600 Subject: AƱade controlador de Reviews MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/reviews_controller.rb | 41 +++++++++++++++++++++++++++ app/models/product.rb | 2 ++ app/models/user_account.rb | 1 + app/serializers/product_review_serializer.rb | 6 ---- app/serializers/product_reviews_serializer.rb | 6 ++++ config/routes.rb | 3 ++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 app/controllers/api/reviews_controller.rb delete mode 100644 app/serializers/product_review_serializer.rb create mode 100644 app/serializers/product_reviews_serializer.rb 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 diff --git a/app/models/product.rb b/app/models/product.rb index e6f3720..b9ca828 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -11,6 +11,8 @@ class Product < ApplicationRecord belongs_to :company + has_many :product_reviews + validates :name, presence: true validates :unitary_price, presence: true validates :unitary_price, comparison: { greater_than: 0 } diff --git a/app/models/user_account.rb b/app/models/user_account.rb index 23f7c5b..0fe5c3d 100644 --- a/app/models/user_account.rb +++ b/app/models/user_account.rb @@ -11,6 +11,7 @@ class UserAccount < ApplicationRecord has_many :user_account_addresses, dependent: :destroy has_many :addresses, through: :user_account_addresses has_many :cards, dependent: :destroy + has_many :product_reviews validates :email, presence: true validates :email, uniqueness: true diff --git a/app/serializers/product_review_serializer.rb b/app/serializers/product_review_serializer.rb deleted file mode 100644 index 15b8937..0000000 --- a/app/serializers/product_review_serializer.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -# ProductReviewSerializer -class ProductReview < BaseSerializer - attributes :review, :rating, :author_name -end diff --git a/app/serializers/product_reviews_serializer.rb b/app/serializers/product_reviews_serializer.rb new file mode 100644 index 0000000..9a48f56 --- /dev/null +++ b/app/serializers/product_reviews_serializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# ProductReviewSerializer +class ProductReviews < BaseSerializer + attributes :review, :rating, :author_name +end diff --git a/config/routes.rb b/config/routes.rb index ccf7b44..8c671ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,9 @@ Rails.application.routes.draw do put '/account', to: 'user_accounts#update' resources :companies, only: %i[index show create update] resources :products, only: %i[index show create update destroy] + resources :products do + resources :reviews, only: %i[index create] + end resources :user_accounts, only: %i[create] resources :refresh_tokens, only: %i[create] end -- cgit v1.2.3