public override async Task <RemoveItemFromShoppingCartResponse> RemoveItemFromShoppingCart( RemoveItemFromShoppingCartRequest request, ServerCallContext context) { var shoppingCart = await _shoppingCartContext .ShoppingCarts .FirstOrDefaultAsync(r => r.UserName == request.Username); if (shoppingCart == null) { throw new RpcException(new Status(StatusCode.NotFound, "There is no shoppingCart")); } var removeCartItems = shoppingCart.Items .FirstOrDefault(r => r.ProductId == request.RemoveCartItem.ProductId); if (removeCartItems == null) { throw new RpcException(new Status(StatusCode.NotFound, $"CartItem {request.Username} with productid - {removeCartItems.ProductId} not found")); } shoppingCart.Items.Remove(removeCartItems); var removeCount = await _shoppingCartContext.SaveChangesAsync(); var response = new RemoveItemFromShoppingCartResponse { Success = removeCount > 0 }; return(response); }
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); }