diff --git a/rails_application/app/controllers/orders_controller.rb b/rails_application/app/controllers/orders_controller.rb index 08bccd19..19001480 100644 --- a/rails_application/app/controllers/orders_controller.rb +++ b/rails_application/app/controllers/orders_controller.rb @@ -77,7 +77,7 @@ def add_item @order = Order.find(params[:id]) @order.add_item(product) - Inventory::ProductService.new.decrement_stock_level(product.id) + Inventory::ProductService.new.decrement_stock_level(Inventory::DecreaseStockLevel.new(product_id: product.id)) @order.save! redirect_to edit_order_path(params[:id]) @@ -87,7 +87,7 @@ def remove_item product = Product.find(params[:product_id]) @order = Order.find(params[:id]) @order.remove_item(product) - Inventory::ProductService.new.increment_stock_level(product.id) + Inventory::ProductService.new.increment_stock_level(Inventory::IncreaseStockLevel.new(product_id: product.id)) @order.save! redirect_to edit_order_path(params[:id]) @@ -113,7 +113,7 @@ def expire .where(status: "Draft") .find_each do |order| order.order_items.each do |item| - Inventory::ProductService.new.increment_stock_level(item.product.id) + Inventory::ProductService.new.increment_stock_level(Inventory::IncreaseStockLevel.new(product_id: item.product_id)) end order.status = "Expired" order.save! diff --git a/rails_application/app/controllers/supplies_controller.rb b/rails_application/app/controllers/supplies_controller.rb index 089a5378..956190f5 100644 --- a/rails_application/app/controllers/supplies_controller.rb +++ b/rails_application/app/controllers/supplies_controller.rb @@ -4,7 +4,7 @@ def new end def create - Inventory::ProductService.new.supply(params[:product_id].to_i, params[:quantity].to_i) + Inventory::ProductService.new.supply(Inventory::SupplyStockLevel.new(params[:product_id], params[:quantity])) redirect_to products_path, notice: "Stock level changed" end end diff --git a/rails_application/app/models/inventory/decrease_stock_level.rb b/rails_application/app/models/inventory/decrease_stock_level.rb new file mode 100644 index 00000000..d2c0e238 --- /dev/null +++ b/rails_application/app/models/inventory/decrease_stock_level.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module Inventory + DecreaseStockLevel = Struct.new(:product_id) +end + diff --git a/rails_application/app/models/inventory/increase_stock_level.rb b/rails_application/app/models/inventory/increase_stock_level.rb new file mode 100644 index 00000000..75d9581b --- /dev/null +++ b/rails_application/app/models/inventory/increase_stock_level.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Inventory + IncreaseStockLevel = Struct.new(:product_id) +end diff --git a/rails_application/app/models/inventory/product_service.rb b/rails_application/app/models/inventory/product_service.rb index d64424b8..e5c7866a 100644 --- a/rails_application/app/models/inventory/product_service.rb +++ b/rails_application/app/models/inventory/product_service.rb @@ -7,7 +7,8 @@ def initialize @repository = Infra::AggregateRootRepository.new(event_store) end - def decrement_stock_level(product_id) + def decrement_stock_level(command) + product_id = command.product_id ApplicationRecord.with_advisory_lock("change_stock_level_for_#{product_id}") do product = ::Product.find(product_id) product_stream = event_store.read.stream("Inventory::Product$#{product_id}").to_a @@ -26,7 +27,8 @@ def decrement_stock_level(product_id) end end - def increment_stock_level(product_id) + def increment_stock_level(command) + product_id = command.product_id ApplicationRecord.with_advisory_lock("change_stock_level_for_#{product_id}") do product = ::Product.find(product_id) product_stream = event_store.read.stream("Inventory::Product$#{product_id}").to_a @@ -45,7 +47,10 @@ def increment_stock_level(product_id) end end - def supply(product_id, quantity) + def supply(command) + product_id = command.product_id + quantity = command.quantity + ApplicationRecord.with_advisory_lock("change_stock_level_for_#{product_id}") do product = ::Product.find(product_id) product.stock_level == nil ? product.stock_level = quantity : product.stock_level += quantity diff --git a/rails_application/app/models/inventory/supply_stock_level.rb b/rails_application/app/models/inventory/supply_stock_level.rb new file mode 100644 index 00000000..6464b556 --- /dev/null +++ b/rails_application/app/models/inventory/supply_stock_level.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Inventory + SupplyStockLevel = Struct.new(:product_id, :quantity) do + def initialize(product_id, quantity) + super(product_id.to_i, quantity.to_i) + end + end +end