public ShoppingList AddRecipeToShoppingList(int recipeId) { var shoppingList = new ShoppingList { Name = "Untitled" }; var recipe = _recipeRepository.Get(x => x.Id == recipeId, x => x.RecipeIngredients); var shoppingListRecipe = new ShoppingListRecipe { Recipe = recipe, ShoppingList = shoppingList }; shoppingList.ShoppingListRecipes.Add(shoppingListRecipe); foreach (var recipeIngredient in recipe.RecipeIngredients) { var shoppingListIngredient = new ShoppingListIngredient { Ingredient = recipeIngredient.Ingredient, Quantity = recipeIngredient.Quantity, UnitOfMeasurement = recipeIngredient.UnitOfMeasurement, Weight = recipeIngredient.Weight }; shoppingList.ShoppingListIngredients.Add(shoppingListIngredient); } _shoppingListRepository.Create(shoppingList); _shoppingListRepository.SaveChanges(); return(shoppingList); }
private void Save(object obj) { if (CurrentShoppingList.ShoppingListIngredients == null) { CurrentShoppingList.ShoppingListIngredients = new List <ShoppingListIngredient>(); } CurrentShoppingList.ShoppingListIngredients.Clear(); foreach (var addIngredientViewModel in AddIngredientControls) { addIngredientViewModel.SetIngredientIfNew(); var shopppingListIngredient = new ShoppingListIngredient { Ingredient = addIngredientViewModel.Ingredient, Quantity = addIngredientViewModel.Quantity, UnitOfMeasurement = addIngredientViewModel.UnitOfMeasurement, Weight = addIngredientViewModel.Weight }; CurrentShoppingList.ShoppingListIngredients.Add(shopppingListIngredient); } OnRecordUpdated <ShoppingList>(CurrentShoppingList.Name); Refresh(); _shoppingListService.Update(CurrentShoppingList.Id, CurrentShoppingList.Name, CurrentShoppingList.Description); }
public void AddRecipesToShoppingList(int shoppingListId, IEnumerable <Recipe> recipes) { var shoppingList = _shoppingListRepository.Get(x => x.Id == shoppingListId, x => x.ShoppingListRecipes, x => x.ShoppingListIngredients); foreach (var recipe in recipes) { _recipeRepository.Get(r => r.Id == recipe.Id, r => r.RecipeIngredients); var shoppingListRecipe = new ShoppingListRecipe { Recipe = recipe, ShoppingList = shoppingList }; shoppingList.ShoppingListRecipes.Add(shoppingListRecipe); } List <RecipeIngredient> allRecipeIngredients = recipes.SelectMany(r => r.RecipeIngredients).ToList(); foreach (var recipeIngredient in allRecipeIngredients) { var shoppingListIngredient = shoppingList.ShoppingListIngredients.FirstOrDefault(s => s.IngredientId == recipeIngredient.IngredientId); if (shoppingListIngredient == null) { shoppingListIngredient = new ShoppingListIngredient { Ingredient = recipeIngredient.Ingredient, Quantity = recipeIngredient.Quantity, UnitOfMeasurement = recipeIngredient.UnitOfMeasurement, Weight = recipeIngredient.Weight }; shoppingList.ShoppingListIngredients.Add(shoppingListIngredient); continue; } shoppingListIngredient.Quantity += recipeIngredient.Quantity; shoppingListIngredient.Weight += recipeIngredient.Weight; } _shoppingListRepository.Update(shoppingList); _shoppingListRepository.SaveChanges(); }