From fc191f53503629087fd453e626ec2a4c58e0a21b Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 6 Apr 2023 14:22:22 -0600 Subject: AƱadidos validadores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/carts_controller.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/api/carts_controller.rb b/app/controllers/api/carts_controller.rb index c33f76e..0e14c17 100644 --- a/app/controllers/api/carts_controller.rb +++ b/app/controllers/api/carts_controller.rb @@ -10,24 +10,21 @@ module Api end def create - product_cart = @cart.product_carts.new(product_id: permitted_params[:product_id], - quantity: permitted_params[:quantity]) + product_cart = @cart.add_product(permitted_params[:product_id], permitted_params[:quantity]) - if product_cart.save + if product_cart.errors.empty? render json: serialized_object.serializable_hash, status: :ok else - render json: { errors: product_cart.as_json }, status: :unprocessable_entity + render json: { errors: product_cart.errors.as_json }, status: :unprocessable_entity end end def destroy - product = @cart.product_carts.products.find_by(public_id: params[:id]) - - render status: :not_found and return if product.nil? - - @cart.product_carts.find_by(cart_id: @cart.id, product_id: product.id).destroy - - render status: :no_content + if @cart.delete_product(product.id) + render status: :no_content + else + render status: :not_found + end end private -- cgit v1.2.3