Skip to content

Commit

Permalink
Introduce stock level commands
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczykt authored and lukaszreszke committed Sep 25, 2024
1 parent 0ed66a8 commit 161a06e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 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
2 changes: 1 addition & 1 deletion rails_application/app/controllers/supplies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
11 changes: 8 additions & 3 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 All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions rails_application/app/models/inventory/supply_stock_level.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 161a06e

Please sign in to comment.