public async Task DeleteCartItem(DeleteProductFromCartDto item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } var cart = await GetCartById(item.CartId); if (cart == null) { throw new EntityNotFoundException($"Cart with id:{item.CartId} doesn't exist"); } var cartItem = cart.CartItems.FirstOrDefault(c => c.ProductId == item.ProductId); if (cartItem == null) { throw new EntityNotFoundException($"Cart item with product id:{item.ProductId} doesn't exist"); } cartItem.Quantity--; if (cartItem.Quantity == 0) { _context.CartItems.Remove(cartItem); } else { _context.CartItems.Update(cartItem); } await _context.SaveChangesAsync(); }
public async Task <IActionResult> Delete(DeleteProductFromCartDto productCartDto) { try { await _cartCrudService.DeleteCartItem(productCartDto); return(Ok()); } catch (ArgumentNullException e) { return(BadRequest(e.Message)); } catch (EntityNotFoundException e) { return(BadRequest(e.Message)); } }