summaryrefslogtreecommitdiff
path: root/app/controllers/api
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-03-16 21:53:23 -0600
committerHombreLaser <sebastian-440@live.com>2023-03-16 21:53:23 -0600
commit69153b46eee0fec67efa66f9b5f0499730a98829 (patch)
treef07d8de984911fcf6d684b06ae69516cc9aa2b43 /app/controllers/api
parentec60f7fb4be7213174f150318e418cdd8832a39b (diff)
Mejorado código de ProductsController
Diffstat (limited to 'app/controllers/api')
-rw-r--r--app/controllers/api/companies_controller.rb2
-rw-r--r--app/controllers/api/products_controller.rb11
2 files changed, 9 insertions, 4 deletions
diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb
index 63d30c8..086ac5b 100644
--- a/app/controllers/api/companies_controller.rb
+++ b/app/controllers/api/companies_controller.rb
@@ -52,7 +52,7 @@ module Api
end
def not_found_error_message
- { error_message: "No se encontró la compañía #{params[:short_name]}"}
+ { error_message: "No se encontró la compañía #{params[:short_name]}" }
end
end
end
diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb
index febb207..213fa9f 100644
--- a/app/controllers/api/products_controller.rb
+++ b/app/controllers/api/products_controller.rb
@@ -18,7 +18,7 @@ module Api
end
def create
- @product = Product.new(permitted_params)
+ @product = Product.new(object_params)
if @product.save
render json: serialized_object.serializable_hash, status: :ok
@@ -30,7 +30,7 @@ module Api
def update
@product = Product.find_by(public_id: params[:id])
- if @product.update(permitted_params)
+ if @product.update(object_params)
render json: serialized_object.serializable_hash, status: :ok
else
render json: @product.errors.full_messages, status: :unprocessable_entity
@@ -49,7 +49,7 @@ module Api
private
def serialized_object
- Serializers::ProductSerializer(@product).new
+ Serializers::ProductSerializer.new(@product)
end
def serialized_collection
@@ -59,5 +59,10 @@ module Api
def permitted_params
params.permit(:name, :unitary_price, :bulk_price, :picture, :available_quantity, :categories, :company_id)
end
+
+ def object_params
+ categories = permitted_params[:categories].split(',')
+ permitted_params.merge(categories:, public_id: SecureRandom.hex(12))
+ end
end
end