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/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 50d62fc1..c0f7a452 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