public async Task <Recipe> CreateAsync(RecipeCreateInputModel model) { if (model.CategoryId == 0 || model.CookingTimeId == 0) { return(null); } var recipe = AutoMapper.Mapper.Map <Recipe>(model); if (model.IngredientQuantities != null) { if (model.IngredientQuantities.IngredientNames.Count() != model.IngredientQuantities.RecipeIngredientQuantity.Count()) { throw new ArgumentNullException(); } for (int i = 0; i < model.IngredientQuantities.IngredientNames.Count(); i++) { var ingredient = this.dbContext.Ingredients.FirstOrDefault(x => x.Name == model.IngredientQuantities.IngredientNames[i]); if (ingredient == null) { return(null); } RecipeIngredient recipeIngredient = new RecipeIngredient { Recipe = recipe, Ingredient = ingredient, Quantity = model.IngredientQuantities.RecipeIngredientQuantity[i], }; if (this.dbContext.IngredientAllergen.Any(x => x.Ingredient.Name == ingredient.Name && !recipe.RecipeAllergens.Any(y => y.AllergenId == x.AllergenId))) { RecipeAllergen recipeAllergen = new RecipeAllergen { Recipe = recipe, Allergen = this.dbContext.IngredientAllergen.Where(x => x.Ingredient.Name == model.IngredientQuantities.IngredientNames[i]).Select(x => x.Allergen).FirstOrDefault() }; recipeAllergen = this.dbContext.RecipeAllergen.Add(recipeAllergen).Entity; recipe.RecipeAllergens.Add(recipeAllergen); } recipeIngredient = this.dbContext.RecipeIngredient.Add(recipeIngredient).Entity; recipe.RecipeIngredient.Add(recipeIngredient); } } recipe = this.dbContext.Recipes.Add(recipe).Entity; await this.dbContext.SaveChangesAsync(); return(recipe); }
public async Task <Recipe> EditAsync(RecipeEditInputModel model) { var recipeFromDb = GetById(model.Id); recipeFromDb.Name = model.Name; recipeFromDb.Description = model.Description; recipeFromDb.Category = this.dbContext.Categories.Where(x => x.Name == model.CategoryName).FirstOrDefault(); recipeFromDb.CookingTime = this.dbContext.CookingTimes.Where(x => x.Name == model.CookingTimeName).FirstOrDefault(); recipeFromDb.ImagePath = model.ImagePath; recipeFromDb.ModifiedOn = DateTime.UtcNow; int counter = 0; foreach (var currentIngredient in recipeFromDb.RecipeIngredient) { if (model.Quantity[counter] == null) { this.dbContext.RecipeIngredient.Remove(currentIngredient); } else { currentIngredient.Quantity = model.Quantity[counter]; } counter++; } if (model.IngredientQuantities != null) { if (model.IngredientQuantities.IngredientNames.Count() != model.IngredientQuantities.RecipeIngredientQuantity.Count()) { throw new ArgumentNullException(); } for (int i = 0; i < model.IngredientQuantities.IngredientNames.Count(); i++) { var ingredient = this.dbContext.Ingredients.FirstOrDefault(x => x.Name == model.IngredientQuantities.IngredientNames[i]); if (ingredient == null) { return(null); } RecipeIngredient recipeIngredient = new RecipeIngredient { Recipe = recipeFromDb, Ingredient = ingredient, Quantity = model.IngredientQuantities.RecipeIngredientQuantity[i], }; if (this.dbContext.IngredientAllergen.Any(x => x.Ingredient.Name == ingredient.Name && !recipeFromDb.RecipeAllergens.Any(y => y.AllergenId == x.AllergenId))) { RecipeAllergen recipeAllergen = new RecipeAllergen { Recipe = recipeFromDb, Allergen = this.dbContext.IngredientAllergen.Where(x => x.Ingredient.Name == model.IngredientQuantities.IngredientNames[i]).Select(x => x.Allergen).FirstOrDefault() }; recipeAllergen = this.dbContext.RecipeAllergen.Add(recipeAllergen).Entity; recipeFromDb.RecipeAllergens.Add(recipeAllergen); } recipeIngredient = this.dbContext.RecipeIngredient.Add(recipeIngredient).Entity; recipeFromDb.RecipeIngredient.Add(recipeIngredient); } } await this.dbContext.SaveChangesAsync(); return(recipeFromDb); }