public void RemoveCartItem(string email, int id) { var user = _dbContext.Users.FirstOrDefault(u => u.Email == email); if (user == null) { throw new ResourceNotFoundException($"User with email {email} not found"); } var cart = _dbContext.ShoppingCarts.FirstOrDefault(c => c.User.Email == email); if (cart == null) { throw new ResourceNotFoundException($"No cart for with {email} was found"); } var cartItem = _dbContext.ShoppingCartItems.FirstOrDefault(ci => ci.Id == id && ci.ShoppingCartId == cart.Id); if (cartItem == null) { throw new ResourceNotFoundException($"No item with id {id} found in cart"); } _dbContext.Remove(cartItem); _dbContext.SaveChanges(); }
public void DeleteCart(string email) { // Find user and cart var user = _dbContext.Users.FirstOrDefault(u => u.Email == email); var cart = _dbContext.ShoppingCarts.FirstOrDefault(s => s.UserId == user.Id); // Clear the cart and then delete it ClearCart(email); _dbContext.Remove(cart); _dbContext.SaveChanges(); }
public void DeleteAddress(string email, int addressId) { var user = _dbContext.Users.FirstOrDefault(u => u.Email == email); if (user == null) { throw new Exception("User not found"); } _dbContext.Remove(_dbContext.Addresses.FirstOrDefault(a => a.Id == addressId && a.UserId == user.Id)); _dbContext.SaveChanges(); }