summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/products_controller.rb33
-rw-r--r--app/controllers/serializers/product_serializer.rb2
2 files changed, 17 insertions, 18 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
diff --git a/app/controllers/serializers/product_serializer.rb b/app/controllers/serializers/product_serializer.rb
index 8e1ac11..7fb2874 100644
--- a/app/controllers/serializers/product_serializer.rb
+++ b/app/controllers/serializers/product_serializer.rb
@@ -6,7 +6,7 @@ module Serializers
include Rails.application.routes.url_helpers
extend ActionView::RoutingUrlFor
- attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories
+ attributes :name, :public_id, :unitary_price, :bulk_price, :available_quantity, :categories
attribute :picture do |object|
object.picture.url