Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created November 10, 2016 00:44
Show Gist options
  • Save dcomartin/8752ffbd87a7a2189f9d9395c07d0a55 to your computer and use it in GitHub Desktop.
Save dcomartin/8752ffbd87a7a2189f9d9395c07d0a55 to your computer and use it in GitHub Desktop.
public class AddToCartHandler : ICancellableAsyncRequestHandler<AddToCart, Unit>
{
private readonly MusicStoreContext _dbContext;
private readonly ILogger<ShoppingCartController> _logger;
public AddToCartHandler(MusicStoreContext dbContext, ILogger<ShoppingCartController> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task<Unit> Handle(AddToCart message, CancellationToken cancellationToken)
{
// Retrieve the album from the database
var addedAlbum = await _dbContext.Albums
.SingleAsync(album => album.AlbumId == message.AlbumId, cancellationToken);
// Add it to the shopping cart
var cart = Models.ShoppingCart.GetCart(_dbContext, message.CartId);
await cart.AddToCart(addedAlbum);
await _dbContext.SaveChangesAsync(cancellationToken);
_logger.LogInformation("Album {albumId} was added to the cart.", addedAlbum.AlbumId);
return Unit.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment