summaryrefslogtreecommitdiff
path: root/app/controllers/api/products_controller.rb
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-03-22 17:40:42 -0600
committerHombreLaser <sebastian-440@live.com>2023-03-22 17:40:42 -0600
commit5868c1230bdfde7fa10d498a12e214df2722415c (patch)
tree21abc54589101602dad5a65e3df6846072bc969a /app/controllers/api/products_controller.rb
parent3e106dc7f5fbd6961c67f41aea66033c27b5142f (diff)
Arregla bugs del controlador de products
Diffstat (limited to 'app/controllers/api/products_controller.rb')
-rw-r--r--app/controllers/api/products_controller.rb33
1 files changed, 16 insertions, 17 deletions
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