public override async Task <RemoveItemFromShoppingCartResponse> RemoveItemFromShoppingCart(RemoveItemFromShoppingCartRequest request, ServerCallContext context) { var shoppingCart = await _shoppingCartContext.ShoppingCarts.FirstOrDefaultAsync(x => x.UserName == request.Username); if (shoppingCart == null) { throw new RpcException(new Status(StatusCode.NotFound, "Invalid Request.")); } var removeCartItem = shoppingCart.Items.FirstOrDefault(x => x.ProductId == request.RemoveCartItem.ProductId); if (removeCartItem == null) { throw new RpcException(new Status(StatusCode.NotFound, "Invalid Product Item.")); } _shoppingCartContext.Remove(removeCartItem); var removeCount = await _shoppingCartContext.SaveChangesAsync(); var response = new RemoveItemFromShoppingCartResponse { Success = removeCount > 0 }; return(response); }
public IActionResult ReduceItem(string productID) { string sessionId = Request.Cookies["sessionId"]; Session session = dbcontext.Sessions.Find(sessionId); Product product = dbcontext.Products.Find(productID); Cart cart_check = dbcontext.Carts.Where(x => x.ProductID == productID && x.SessionID == session.SessionID).FirstOrDefault(); if (cart_check.CartQuantity > 1) { cart_check.CartQuantity = (int)cart_check.CartQuantity - 1; dbcontext.Update(cart_check); dbcontext.SaveChanges(); } else { dbcontext.Remove(cart_check); dbcontext.SaveChanges(); } return(new JsonResult(new { success = "Success" })); }