in UnicornStore/Controllers/ShoppingCartController.cs [66:111]
public async Task<IActionResult> RemoveFromCart(
int id,
CancellationToken requestAborted)
{
// Retrieve the current user's shopping cart
var cart = ShoppingCart.GetCart(DbContext, HttpContext);
// Get the name of the blessing to display confirmation
var cartItem = await DbContext.CartItems
.Where(item => item.CartItemId == id)
.Include(c => c.Blessing)
.SingleOrDefaultAsync();
string message;
int itemCount;
if (cartItem != null)
{
// Remove from cart
itemCount = cart.RemoveFromCart(id);
await DbContext.SaveChangesAsync(requestAborted);
string removed = (itemCount > 0) ? " 1 copy of " : string.Empty;
message = removed + cartItem.Blessing.Title + " has been removed from your shopping cart.";
}
else
{
itemCount = 0;
message = "Could not find this item, nothing has been removed from your shopping cart.";
}
// Display the confirmation message
var results = new ShoppingCartRemoveViewModel
{
Message = message,
CartTotal = await cart.GetTotal(),
CartCount = await cart.GetCount(),
ItemCount = itemCount,
DeleteId = id
};
_logger.LogInformation("Blessing {id} was removed from a cart.", id);
return Json(results);
}