// GET: Meals/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var meal = await _context.Meals.SingleOrDefaultAsync(m => m.ID == id); if (meal == null) { return(NotFound()); } EditMealViewModel editMealViewModel = new EditMealViewModel( meal.ID, meal.Name, meal.Description, meal.UserID, _context.WeatherTypes.ToList(), meal.WeatherTypeID, _context.CookingMethods.ToList(), meal.CookingMethodID, _context.CookingMethods.ToList(), meal.AltCookingMethodID, _context.CookingTimes.ToList(), meal.CookingTimeID, _context.PrepTimes.ToList(), meal.PrepTimeID); _context.SaveChanges(); return(View(editMealViewModel)); }
public async Task <IActionResult> Edit(int id, EditMealViewModel editMealViewModel) { if (id != editMealViewModel.mealID) { return(NotFound()); } if (ModelState.IsValid) { try { WeatherType newWeatherType = _context.WeatherTypes.Single(w => w.ID == editMealViewModel.WeatherTypeID); CookingMethod newCookingMethod = _context.CookingMethods.Single(m => m.ID == editMealViewModel.CookingMethodID); CookingMethod newAltCookingMethod = _context.CookingMethods.Single(a => a.ID == editMealViewModel.AltCookingMethodID); CookingTime newCookingTime = _context.CookingTimes.Single(t => t.ID == editMealViewModel.CookingTimeID); PrepTime newPrepTime = _context.PrepTimes.Single(p => p.ID == editMealViewModel.PrepTimeID); //Add the new default meal to the default meal table Meal editMeal = new Meal { ID = editMealViewModel.mealID, Name = editMealViewModel.Name, Description = editMealViewModel.Description, Location = editMealViewModel.Location, UserID = editMealViewModel.UserID, WeatherType = newWeatherType, CookingMethod = newCookingMethod, AltCookingMethod = newAltCookingMethod, CookingTime = newCookingTime, PrepTime = newPrepTime }; _context.Update(editMeal); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MealExists((editMealViewModel.mealID))) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index")); } return(View(editMealViewModel)); }
public async Task <IActionResult> EditMeal(EditMealViewModel model) { if (this.ModelState.IsValid) { await this.diaryService.EditMeal(model); return(RedirectToAction("GetMealsForEditing", new { model.CurrentPage, model.SearchText })); } var foodItemNames = this.contentService.GetFoodItemsNames(); var recipesNames = this.contentService.GetRecipesNames(); this.ViewData["FoodItemsNames"] = foodItemNames.ToList(); this.ViewData["RecipesNames"] = recipesNames.ToList(); return(View("OpenMealForEditing", model)); }
public EditMealViewModel GetMealForEditing(string mealId) { var meal = this.context.Meals.Include(m => m.Location).Include(m => m.MealFoodItems).ThenInclude(mfi => mfi.FoodItem) .Include(m => m.MealRecipes).ThenInclude(mr => mr.Recipe).FirstOrDefault(m => m.Id == mealId); if (meal == null) { return(null); } var recipes = new List <EditMealViewModel.RecipeInEditMealViewModel>(); var foodItems = new List <EditMealViewModel.FoodItemInEditMealViewModel>(); foreach (var mealRecipe in meal.MealRecipes) { var recipeInEditMealViewModel = new EditMealViewModel.RecipeInEditMealViewModel { Name = mealRecipe.Recipe.Name, AmountInGrams = mealRecipe.AmountInGrams }; recipes.Add(recipeInEditMealViewModel); } foreach (var mealFoodItem in meal.MealFoodItems) { var foodItemInEditMealViewModel = new EditMealViewModel.FoodItemInEditMealViewModel { Name = mealFoodItem.FoodItem.Name, AmountInGrams = mealFoodItem.AmountInGrams }; foodItems.Add(foodItemInEditMealViewModel); } var editMealViewModel = new EditMealViewModel { Id = meal.Id, Note = meal.Note, Location = meal.Location == null ? null : meal.Location.Name, TimeOfConsumption = meal.TimeOfConsumption, Recipes = recipes, FoodItems = foodItems }; return(editMealViewModel); }
// GET: Meal/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(RedirectToAction("Error", "Home")); } Meal meal = await _context.Meals.Include(c => c.MealCategories).SingleOrDefaultAsync(x => x.MealId == id); if (meal == null) { return(RedirectToAction("Error", "Home")); } EditMealViewModel model = new EditMealViewModel(); model.Meal = meal; model.LevelPreperations = new SelectList(_context.LevelPreperations, "LevelPreperationId", "Level", model.Meal.LevelPreperationId); model.PreperationTimes = new SelectList(_context.PreperationTimes, "PreperationTimeId", "RangeMinutes", model.Meal.PreperationTimeId); model.Category = new SelectList(_context.Categories, "CategoryId", "Name"); model.SelectedCategories = meal.MealCategories.Select(mc => mc.CategoryId); return(View(model)); }
public async Task <IActionResult> Edit(int id, EditMealViewModel model) { if (id != model.Meal.MealId) { return(RedirectToAction("Error", "Home")); } Meal meal = await _context.Meals.Include(c => c.MealCategories).SingleOrDefaultAsync(x => x.MealId == id); if (meal != null) { meal.Name = model.Meal.Name; meal.ShortDiscription = model.Meal.ShortDiscription; meal.PreperationDiscribtion = model.Meal.PreperationDiscribtion; meal.NumberOfPeople = model.Meal.NumberOfPeople; if (meal.PictureMeal != model.Meal.PictureMeal) { meal.PictureMeal = UploadedFile(model.MealImage); } Meal checkMealName = await _context.Meals.SingleOrDefaultAsync(x => x.Name == model.Meal.Name && x.MealId == model.Meal.MealId); var result = new FluentValidation.Results.ValidationResult();//Bypass the name validator and didn't know how to do it any other way if (checkMealName != null) { var validator = new EditMealValidator(); result = validator.Validate(model.Meal); } else { var validator = new MealValidator(_context.Meals); result = validator.Validate(model.Meal); } if (result.IsValid) { if (model.SelectedCategories != null) { List <MealCategory> newCategories = new List <MealCategory>(); foreach (int CategoryId in model.SelectedCategories) { newCategories.Add(new MealCategory { MealId = model.Meal.MealId, CategoryId = CategoryId }); } meal.MealCategories.RemoveAll(mc => !newCategories.Contains(mc)); meal.MealCategories.AddRange(newCategories.Where(nc => !meal.MealCategories.Contains(nc))); _context.Update(meal); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { model.ErrorMessage += "Select at least one categorie"; } } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } } else { model.ErrorMessage += "Select at least one categorie"; } return(View(model)); }
public async Task <Meal> EditMeal(EditMealViewModel model) { var meal = this.context.Meals.Include(m => m.Location).Include(m => m.FoodyUser).Include(m => m.MealFoodItems).ThenInclude(mfi => mfi.FoodItem) .Include(m => m.MealRecipes).ThenInclude(mr => mr.Recipe) .FirstOrDefault(m => m.Id == model.Id); if (meal == null) { return(null); } var caloriesChange = false; if (model.FoodItems != null) { foreach (var foodItemViewModel in model.FoodItems) { var foodItem = this.context.FoodItems.FirstOrDefault(fi => fi.Name == foodItemViewModel.Name); if (foodItem == null) { continue; } var existingMealFoodItem = meal.MealFoodItems.FirstOrDefault(mfi => mfi.FoodItem.Name == foodItemViewModel.Name); if (foodItemViewModel.AmountInGrams > 0) { if (existingMealFoodItem == null) { var newMealFoodItem = new MealFoodItem { Meal = meal, FoodItem = foodItem, AmountInGrams = foodItemViewModel.AmountInGrams }; meal.MealFoodItems.Add(newMealFoodItem); caloriesChange = true; } else { if (!existingMealFoodItem.AmountInGrams.Equals(foodItemViewModel.AmountInGrams)) { existingMealFoodItem.AmountInGrams = foodItemViewModel.AmountInGrams; caloriesChange = true; } } } else { if (existingMealFoodItem != null) { meal.MealFoodItems.Remove(existingMealFoodItem); caloriesChange = true; } } } } if (model.Recipes != null) { foreach (var recipeViewModel in model.Recipes) { var recipe = this.context.Recipes.FirstOrDefault(r => r.Name == recipeViewModel.Name); if (recipe == null) { continue; } var existingMealRecipe = meal.MealRecipes.FirstOrDefault(mr => mr.Recipe.Name == recipeViewModel.Name); if (recipeViewModel.AmountInGrams > 0) { if (existingMealRecipe == null) { var newMealRecipe = new MealRecipe { Meal = meal, Recipe = recipe, AmountInGrams = recipeViewModel.AmountInGrams }; meal.MealRecipes.Add(newMealRecipe); caloriesChange = true; } else { if (!existingMealRecipe.AmountInGrams.Equals(recipeViewModel.AmountInGrams)) { existingMealRecipe.AmountInGrams = recipeViewModel.AmountInGrams; caloriesChange = true; } } } else { if (existingMealRecipe != null) { meal.MealRecipes.Remove(existingMealRecipe); caloriesChange = true; } } } } meal.Note = model.Note; if (meal.Location == null || meal.Location.Name != model.Location) { meal.Location = this.HandleLocation(model.Location, meal.FoodyUserId); } meal.TimeOfConsumption = model.TimeOfConsumption; context.SaveChanges(); if (caloriesChange) { Thread.Sleep(500); await this.SetMealCaloriesAsync(meal.Id); } return(meal); }