public async Task <CartDTO> UpdateAsync(ProductCartPutRequest model, ApiDbContext apiDbContext) { try { List <User_Product_Cart> userCartList = apiDbContext.Users_Products_Cart.Where(pc => !pc.IsCompleted && pc.UserId == model.UserId).ToList(); if (userCartList == null) { throw new Exception($"Error cargadndo el carro del usuario con id {model.UserId}"); } User_Product_Cart cartLine = await apiDbContext.Users_Products_Cart.FindAsync(model.Id); if (cartLine == null) { throw new Exception($"Error cargadndo la línea del producto con id {model.ProductId} del carro del usuario con id {model.UserId}"); } cartLine.Quantity = model.Quantity; if (cartLine.Quantity == 0) { apiDbContext.Users_Products_Cart.Remove(cartLine); } await apiDbContext.SaveChangesAsync(); return(ModelToDTO(apiDbContext.Users_Products_Cart.Where(pc => !pc.IsCompleted && pc.UserId == model.UserId).ToList())); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task <int> RemoveAsync(int id, ApiDbContext apiDbContext) { try { User_Product_Cart cartLine = await apiDbContext.Users_Products_Cart.FindAsync(id); if (cartLine == null) { throw new Exception($"Error cargadndo la línea con id {id}"); } apiDbContext.Users_Products_Cart.Remove(cartLine); await apiDbContext.SaveChangesAsync(); return(id); } catch (Exception e) { throw new Exception(e.Message); } }