From ec60f7fb4be7213174f150318e418cdd8832a39b Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 16 Mar 2023 20:36:24 -0600 Subject: Añade código de ProductsController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/products_controller.rb | 53 ++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'app/controllers/api/products_controller.rb') 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 -- cgit v1.2.3