Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 14, 2021 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/f7578134f12ca8dfab60d2d7ef32786a to your computer and use it in GitHub Desktop.
Save dcomartin/f7578134f12ca8dfab60d2d7ef32786a to your computer and use it in GitHub Desktop.
public class QuickOrderTrxHandler : IRequestHandler<QuickOrderCommand>
{
private readonly ICatalogService _catalogService;
private readonly IWarehouseService _warehouseService;
private readonly IOrderRepository _orderRepository;
public QuickOrderTrxHandler(ICatalogService catalogService, IWarehouseService warehouseService, IOrderRepository orderRepository)
{
_catalogService = catalogService;
_warehouseService = warehouseService;
_orderRepository = orderRepository;
}
public async Task Handle(QuickOrderCommand request)
{
using var trx = _orderRepository.BeginTransaction(IsolationLevel.Serializable);
if (await _warehouseService.QuantityOnHand(request.Sku, trx) <= 0)
{
throw new InvalidOperationException("Product has no quantity on hand.");
}
var price = await _catalogService.Price(request.Sku, trx);
var quickOrder = new QuickOrder(request.Sku, price);
_orderRepository.Save(quickOrder);
trx.Commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment