Skip to content

Commit

Permalink
Move increment/decrement stock level to dedicated service
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczykt committed Sep 24, 2024
1 parent 714a1f4 commit baf91d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 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)
product.decrement!(:stock_level)
Inventory::ProductService.new.decrement_stock_level(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)
product.increment!(:stock_level)
Inventory::ProductService.new.increment_stock_level(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|
item.product.increment!(:stock_level)
Inventory::ProductService.new.increment_stock_level(item.product.id)
end
order.status = "Expired"
order.save!
Expand Down
16 changes: 16 additions & 0 deletions rails_application/app/models/inventory/product_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Inventory
class ProductService

def decrement_stock_level(product_id)
product = ::Product.find(product_id)
product.decrement!(:stock_level)
end

def increment_stock_level(product_id)
product = ::Product.find(product_id)
product.increment!(:stock_level)
end
end
end

0 comments on commit baf91d7

Please sign in to comment.