public async Task AddToShoppingList(Guid shoppingListId, AddProductToShoppingListRequest request) { try { var json = JsonConvert.SerializeObject(request); var data = new StringContent(json, Encoding.UTF8, "application/json"); var client = new HttpClient(); var response = await client.PostAsync(new Uri($"http://localhost:5005/api/shoppinglist/{ShoppingListId}"), data); string result = response.Content.ReadAsStringAsync().Result; client.Dispose(); } catch (Exception) { } }
public void AddRecipeIngredientsToShoppingList(Guid recipeId, int numberOfPersons, Guid shoppingListId, bool onlyUnavailable) { var recipe = recipeAdapter.GetRecipeById(recipeId); foreach (var ingredient in recipe.Ingredients) { var request = new AddProductToShoppingListRequest { MeasurementAmount = ingredient.UnitQuantity * numberOfPersons, MeasurementType = ingredient.UnitQuantityType, NrOfPersons = numberOfPersons, ProductId = ingredient.ProductId, Reason = EShoppingListReason.Recipe, RecipeId = recipeId, RecipeOnlyAvailable = onlyUnavailable }; myShoppingListService.AddProductToShoppingList(shoppingListId, request); } }
public void AddProductToShoppingList(Guid shoppingListId, AddProductToShoppingListRequest request) { using var context = new HomeAppDbContext(myDbOptions); var existingItem = context.ShoppingListItems.FirstOrDefault(o => o.ProductId == request.ProductId); bool addNew = false; if (existingItem == null) { addNew = true; existingItem = new DbShoppingListItem { IsChecked = false, Product = context.Products.First(p => p.Id == request.ProductId), ShoppingList = context.ShoppingLists.First() }; } var newItemInfo = new DbShoppingListItemInfo { MeasurementType = request.MeasurementType, MeasurementAmount = request.MeasurementAmount, Reason = request.Reason, ShoppingListItem = existingItem }; if (request.RecipeId != null && request.RecipeId != Guid.Empty) { newItemInfo.RecipeItem = new DbRecipeShoppingListItem { RecipeId = request.RecipeId, NrOfPersons = request.NrOfPersons, IsOnlyUnavailable = request.RecipeOnlyAvailable }; } if (addNew) { context.ShoppingListItems.Add(existingItem); } context.ShoppingListItemInfos.Add(newItemInfo); context.SaveChanges(); // var list = context.ShoppingLists.FirstOrDefault(s => s.Id == shoppingListId); // var productQuan = context.ProductQuantities.FirstOrDefault(s => s.Id == productQuantyId); // if (list == null) // { // throw new KeyNotFoundException($"{nameof(DbShoppingList)} with id {shoppingListId} not found"); // } // if (productQuan == null) // { // throw new KeyNotFoundException($"{nameof(DbProductQuantity)} with id {productQuantyId} not found"); // } // var item = context.ShoppingListItems.Include(i => i.ShoppingList).Include(i => i.ProductQuantities) // .FirstOrDefault(s => s.ShoppingList.Id == shoppingListId && s.ProductQuantities.Id == productQuantyId); // if (item != null) // { // item.Count += count; // context.SaveChanges(); // return; // } // var newItem = new DbShoppingListItem // { // ShoppingList = list, // Count = count, // ProductQuantities = productQuan // }; // context.ShoppingListItems.Add(newItem); // context.SaveChanges(); }
public void Post(Guid shoppingListID, [FromBody] AddProductToShoppingListRequest request) { myShoppingListService.AddProductToShoppingList(shoppingListID, request); }
public void AddProductToShoppingList(Guid shoppingListId, AddProductToShoppingListRequest request) { myShoppingListDataAdapter.AddProductToShoppingList(shoppingListId, request); }