summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-03-16 20:36:24 -0600
committerHombreLaser <sebastian-440@live.com>2023-03-16 20:36:24 -0600
commitec60f7fb4be7213174f150318e418cdd8832a39b (patch)
tree03fd80ae3b7f4b0d55578fcc8fe3190eb988916f /app
parent5130f549452ead2402a782cbd54667020f196379 (diff)
Añade código de ProductsController
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/products_controller.rb53
-rw-r--r--app/controllers/serializers/product_serializer.rb21
-rw-r--r--app/models/product.rb12
3 files changed, 80 insertions, 6 deletions
diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb
index 0d78151..febb207 100644
--- a/app/controllers/api/products_controller.rb
+++ b/app/controllers/api/products_controller.rb
@@ -3,20 +3,61 @@
module Api
# ProductsController
class ProductsController < MasterController
- def show; end
+ def show
+ @product = Product.find_by(public_id: params[:id])
- def index; end
+ render json: { error_message: 'No existe el producto' }, status: :not_found and return if @product.nil?
- def create; end
+ render json: serialized_object.serializable_hash, status: :ok
+ end
+
+ def index
+ @products = Product.all
+
+ render json: serialized_collection.serializable_hash, status: :ok
+ end
+
+ def create
+ @product = Product.new(permitted_params)
+
+ if @product.save
+ render json: serialized_object.serializable_hash, status: :ok
+ else
+ render json: @product.errors.full_messages, status: :unprocessable_entity
+ end
+ end
+
+ def update
+ @product = Product.find_by(public_id: params[:id])
- def update; end
+ if @product.update(permitted_params)
+ render json: serialized_object.serializable_hash, status: :ok
+ else
+ render json: @product.errors.full_messages, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ @product = Product.find_by(public_id: params[:id])
- def destroy; end
+ render json: { error_message: 'No existe el producto' }, status: :not_found and return if @product.nil?
+
+ @product.destroy
+ render status: :see_other
+ end
private
+ def serialized_object
+ Serializers::ProductSerializer(@product).new
+ end
+
+ def serialized_collection
+ Serializers::ProductSerializer.new(@products.page(params[:page]))
+ end
+
def permitted_params
- params.permit(:name, :unitary_price, :bulk_price, :picture, :available_quantity, :categories)
+ params.permit(:name, :unitary_price, :bulk_price, :picture, :available_quantity, :categories, :company_id)
end
end
end
diff --git a/app/controllers/serializers/product_serializer.rb b/app/controllers/serializers/product_serializer.rb
new file mode 100644
index 0000000..4a2158d
--- /dev/null
+++ b/app/controllers/serializers/product_serializer.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Serializers
+ # ProductSerializer
+ class ProductSerializer < BaseSerializer
+ extend ActionView::RoutingUrlFor
+
+ attributes :name, :unitary_price, :bulk_price, :available_quantity, :categories
+
+ attribute :picture do |object|
+ object.picture.url
+ end
+
+ belongs_to :company, links: {
+ self: :url,
+ related: lambda(object) {
+ company_path(object.company)
+ }
+ }
+ end
+end \ No newline at end of file
diff --git a/app/models/product.rb b/app/models/product.rb
index 72d7f33..87a7a71 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -21,5 +21,17 @@ class Product < ApplicationRecord
has_one_attached :picture
+ around_create :generate_public_id
+
serialize :categories, Array
+
+ def to_param
+ public_id
+ end
+
+ private
+
+ def generate_public_id
+ self.public_id = SecureRandom.hex(12)
+ end
end