Skip to content

Commit

Permalink
Introduce stock level commands
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczykt committed Sep 24, 2024
1 parent c727ef1 commit 608dbd1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions rails_application/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand All @@ -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!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Inventory
DecreaseStockLevel = Struct.new(:product_id)
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Inventory
IncreaseStockLevel = Struct.new(:product_id)
end
6 changes: 4 additions & 2 deletions rails_application/app/models/inventory/product_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 608dbd1

Please sign in to comment.