Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 21, 2020 22:08
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/f4c6a57c1b12225f3c390392e3b490da to your computer and use it in GitHub Desktop.
Save dcomartin/f4c6a57c1b12225f3c390392e3b490da to your computer and use it in GitHub Desktop.
public class PurchaseProductHandler : IRequestHandler<PurchaseProduct>
{
private readonly SalesDbContext _dbContext;
public PurchaseProductHandler(SalesDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Unit> Handle(PurchaseProduct request, CancellationToken cancellationToken)
{
var price = await _dbContext.Products
.Where(x => x.Id == request.ProductId)
.Select(x => x.Price)
.SingleAsync();
var order = new Order
{
OrderId = Guid.NewGuid(),
Items = new List<OrderItem>
{
new OrderItem
{
ProductId = request.ProductId,
Price = 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