Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created December 2, 2016 00:24
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/a8ad3cdd38c1518bdb88ceeda87e7b3b to your computer and use it in GitHub Desktop.
Save dcomartin/a8ad3cdd38c1518bdb88ceeda87e7b3b to your computer and use it in GitHub Desktop.
public class AddToCartHandler : ICancellableAsyncRequestHandler<AddToCart, Unit>
{
private readonly IMediator _mediator;
private readonly MusicStoreContext _dbContext;
public AddToCartHandler(IMediator mediator, MusicStoreContext dbContext)
{
_mediator = mediator;
_dbContext = dbContext;
}
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);
_mediator.Publish(new AlbumAddedToCart(addedAlbum.AlbumId));
return Unit.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment