1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

fix bug: product amount cannot update

This commit is contained in:
Du Peng 2022-08-04 14:02:19 +02:00 committed by Sylvain
parent 851294e8d9
commit ec62931a78
2 changed files with 14 additions and 14 deletions

View File

@ -15,13 +15,7 @@ class API::ProductsController < API::ApiController
def create def create
authorize Product authorize Product
@product = Product.new(product_params) @product = Product.new(product_params)
if @product.amount.present? @product.amount = ProductService.amount_multiplied_by_hundred(@product.amount)
if @product.amount.zero?
@product.amount = nil
else
@product.amount *= 100
end
end
if @product.save if @product.save
render status: :created render status: :created
else else
@ -33,13 +27,7 @@ class API::ProductsController < API::ApiController
authorize @product authorize @product
product_parameters = product_params product_parameters = product_params
if product_parameters[:amount].present? product_parameters[:amount] = ProductService.amount_multiplied_by_hundred(product_parameters[:amount])
if product_parameters[:amount].zero?
product_parameters[:amount] = nil
else
product_parameters[:amount] *= 100
end
end
if @product.update(product_parameters) if @product.update(product_parameters)
render status: :ok render status: :ok
else else

View File

@ -5,4 +5,16 @@ class ProductService
def self.list def self.list
Product.all Product.all
end end
# amount params multiplied by hundred
def self.amount_multiplied_by_hundred(amount)
if amount.present?
v = amount.to_f
return nil if v.zero?
return v * 100
end
nil
end
end end