-
-
Save dcomartin/fa65d313c4402bb8449dc41b3467bbf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QuickOrderHandler2 : IRequestHandler<QuickOrderCommand> | |
{ | |
private readonly IOrderRepository _orderRepository; | |
private readonly ISalesProductRepository _salesProductRepository; | |
public QuickOrderHandler2(IOrderRepository orderRepository, ISalesProductRepository salesProductRepository) | |
{ | |
_orderRepository = orderRepository; | |
_salesProductRepository = salesProductRepository; | |
} | |
public async Task Handle(QuickOrderCommand request) | |
{ | |
using var trx = _orderRepository.BeginTransaction(IsolationLevel.Serializable); | |
var salesProduct = await _salesProductRepository.SalesProduct(request.Sku, trx); | |
var quickOrder = salesProduct.CreateQuickOrder(); | |
_orderRepository.Save(quickOrder, trx); | |
await _salesProductRepository.Save(salesProduct, request.Version); | |
trx.Commit(); | |
} | |
} | |
public class SalesProduct | |
{ | |
public Sku Sku { get; protected set; } | |
public decimal Price { get; protected set; } | |
public int AvailableToPromise { get; protected set; } | |
public SalesProduct(Sku sku, decimal price, int availableToPromise) | |
{ | |
Sku = sku; | |
Price = price; | |
AvailableToPromise = availableToPromise; | |
} | |
public QuickOrder CreateQuickOrder() | |
{ | |
AvailableToPromise = AvailableToPromise - 1; | |
return QuickOrder.Factory(this); | |
} | |
} | |
public class QuickOrder | |
{ | |
public Sku Sku { get; } | |
public decimal Price { get; } | |
public QuickOrder(Sku sku, decimal price) | |
{ | |
Sku = sku; | |
Price = price; | |
} | |
public QuickOrder(SalesProduct product) | |
{ | |
Sku = product.Sku; | |
Price = product.Price; | |
} | |
public static QuickOrder Factory(SalesProduct salesProduct) | |
{ | |
if (salesProduct.AvailableToPromise <= 0) | |
{ | |
throw new InvalidOperationException("Product has no quantity on hand."); | |
} | |
return new QuickOrder(salesProduct); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment