public async Task AddCurrentMealPlan() { var mealsTask = _mealsService.GetShoppingMealsWithIngredients(); var maxOrderTask = GetMaxItemOrder(); await Task.WhenAll(mealsTask, maxOrderTask); var meals = mealsTask.Result; var maxOrder = maxOrderTask.Result; //Get the max display order of the existing shopping list items var recipeItems = MealsService.GetIngredients(meals); //aggregates all ingredients from all meals and sides var list = GenerateShoppingList(recipeItems); //groups like ingredients and returns unordered list using MealPlannerContext context = new MealPlannerContext(_dbOptions); foreach (ShoppingItem item in list) { context.ShoppingListItems.Add(new ShoppingListItem { Name = $"{item.IngredientName} ({item.Quantity} {item.Unit})", Order = maxOrder + item.IngredientOrder, //append any new items to the end of the existing shopping list Notes = string.Join("<br/>", await _mealsService.GetMealsByIngredientInfo(item.IngredientId, item.UnitId)) //Show in the notes field what meals the ingredient is used in }); } await context.SaveChangesAsync(); }
public ShoppingService(MealsService mealsService, DbContextOptions <MealPlannerContext> dbOptions) { _mealsService = mealsService; _dbOptions = dbOptions; }