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 +++++++++++++++++----- ...4004859_add_index_spanning_users_and_reviews.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20230404004859_add_index_spanning_users_and_reviews.rb 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 diff --git a/db/migrate/20230404004859_add_index_spanning_users_and_reviews.rb b/db/migrate/20230404004859_add_index_spanning_users_and_reviews.rb new file mode 100644 index 0000000..3ca6728 --- /dev/null +++ b/db/migrate/20230404004859_add_index_spanning_users_and_reviews.rb @@ -0,0 +1,5 @@ +class AddIndexSpanningUsersAndReviews < ActiveRecord::Migration[7.0] + def change + add_index(:product_reviews, [:product_id, :user_account_id], unique: true) + end +end diff --git a/db/schema.rb b/db/schema.rb index 8506e8d..fc2bbd1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_03_31_001256) do +ActiveRecord::Schema[7.0].define(version: 2023_04_04_004859) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -76,6 +76,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_31_001256) do t.integer "rating" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["product_id", "user_account_id"], name: "index_product_reviews_on_product_id_and_user_account_id", unique: true t.index ["product_id"], name: "index_product_reviews_on_product_id" t.index ["user_account_id"], name: "index_product_reviews_on_user_account_id" end -- cgit v1.2.3