public ActionResult EditItem(ShoppingCartItem item) { var cart = GetCartForVisitor(); if (cart == null) { return RedirectToAction("Index"); } ShoppingCartItem inCartItem = cart.ShoppingCartItems.First(i => i.ProductId == item.ProductId); if (inCartItem.Quantity != item.Quantity) { ICommand command; if (item.Quantity == 0) { command = new RemoveProductFromShoppingCart(cart.Id, item.ProductId); } else { command = new ChangeProductItemQuantityInShoppingCart(cart.Id, item.ProductId, item.Quantity); } MyShopWebApplication.CommandService.Execute(command); } return RedirectToAction("Index"); }
public async Task <ActionResult> RemoveProductFromShoppingCartAsync(RemoveProductFromShoppingCart request, int userId) { var shoppingCart = await _cartRepository.GetShoppingCartAsync(request.ShoppingCartId); if (shoppingCart.UserId != userId) { return(new UnauthorizedResult()); } if (!shoppingCart.IsActive) { throw new Exception("Shopping Cart is not active."); } var product = await _productRepository.GetProductAsync(request.ProductId); await _cartRepository.RemoveShoppingCartProductAsync(shoppingCart, product); return(new NoContentResult()); }
public async Task <ActionResult> RemoveProduct([FromBody] RemoveProductFromShoppingCart request) { var userId = GetUserId(); return(await _cartService.RemoveProductFromShoppingCartAsync(request, userId)); }