From 5868c1230bdfde7fa10d498a12e214df2722415c Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Wed, 22 Mar 2023 17:40:42 -0600 Subject: Arregla bugs del controlador de products --- app/controllers/api/products_controller.rb | 33 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'app/controllers/api') diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 0c3a30b..7856420 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -4,9 +4,9 @@ module Api # ProductsController class ProductsController < MasterController def show - render json: not_found_error_message, status: :not_found and return if product.nil? + render json: not_found_error_message, status: :not_found and return unless Product.exists?(public_id: params[:id]) - render json: serialized_object.serializable_hash, status: :ok + render json: serialized_object(params[:id]).serializable_hash, status: :ok end def index @@ -19,7 +19,7 @@ module Api @product = Product.new(object_params) if @product.save - render json: serialized_object.serializable_hash, status: :ok + render json: serialized_object(@product.public_id).serializable_hash, status: :ok else render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity end @@ -28,8 +28,10 @@ module Api def update @product = Product.find_by(public_id: params[:id]) + render json: not_found_error_message, status: :not_found and return if @product.nil? + if @product.update(object_params) - render json: serialized_object.serializable_hash, status: :ok + render json: serialized_object(@product.public_id).serializable_hash, status: :ok else render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity end @@ -46,23 +48,18 @@ module Api private - def product - @product ||= Product.joins(:company) - .select('products.*', 'companies.name as company_name', - 'companies.short_name as company_short_name') - .find_by(public_id: params[:id]) + def scope + Product.joins(:company) + .select('products.*', 'companies.name as company_name', 'companies.short_name as company_short_name') + .includes(picture_attachment: :blob) end - def serialized_object - Serializers::ProductSerializer.new(product) + def serialized_object(public_id) + Serializers::ProductSerializer.new(scope.find_by(public_id:)) end def serialized_collection - Serializers::ProductSerializer.new( - @products.joins(:company) - .select('products.*', 'companies.name as company_name', 'companies.short_name as company_short_name') - .includes(picture_attachment: :blob).page(params[:page]) - ) + Serializers::ProductSerializer.new(scope.page(params[:page])) end def permitted_params @@ -71,7 +68,9 @@ module Api def object_params categories = permitted_params[:categories].split(',') - permitted_params.merge(categories:, public_id: SecureRandom.hex(12)) + public_id = SecureRandom.hex(12) + public_id = SecureRandom.hex(12) while Product.exists?(public_id:) + permitted_params.merge(categories:, public_id:) end def not_found_error_message -- cgit v1.2.3