-
-
Save dcomartin/bd445b5f23b0def405d31b7dc9973ac7 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 PurchaseProductHandler : IRequestHandler<PurchaseProduct> | |
{ | |
private readonly SalesDbContext _dbContext; | |
private readonly IProductQuery _productQuery; | |
public PurchaseProductHandler(SalesDbContext dbContext, IProductQuery productQuery) | |
{ | |
_dbContext = dbContext; | |
_productQuery = productQuery; | |
} | |
public async Task<Unit> Handle(PurchaseProduct request, CancellationToken cancellationToken) | |
{ | |
var product = await _productQuery.GetProduct(request.ProductId); | |
var order = new Order | |
{ | |
OrderId = Guid.NewGuid(), | |
Items = new List<OrderItem> | |
{ | |
new OrderItem | |
{ | |
ProductId = request.ProductId, | |
Price = product.Price | |
} | |
} | |
}; | |
await _dbContext.Orders.AddAsync(order, cancellationToken); | |
await _dbContext.SaveChangesAsync(); | |
return Unit.Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment