public async Task <IActionResult> Edit(int id, IngredientViewModel editIngredientViewModel) { if (id != editIngredientViewModel.Ingredient.IngredientId) { return(RedirectToAction("Error", "Home")); } Ingredient checkIngredientName = await _context.Ingredients.SingleOrDefaultAsync(x => x.Name == editIngredientViewModel.Ingredient.Name && x.IngredientId == editIngredientViewModel.Ingredient.IngredientId); var result = new FluentValidation.Results.ValidationResult();//Bypass the name validator and didn't know how to do it any other way if (checkIngredientName != null) { var validator = new EditIngredientValidator(); result = validator.Validate(editIngredientViewModel.Ingredient); } else { var validator = new IngredientValidator(_context.Ingredients); result = validator.Validate(editIngredientViewModel.Ingredient); } if (result.IsValid) { Ingredient ingredient = await _context.Ingredients.SingleOrDefaultAsync(x => x.IngredientId == id); if (ingredient != null) { ingredient.Name = editIngredientViewModel.Ingredient.Name; ingredient.EnergyKcal = editIngredientViewModel.Ingredient.EnergyKcal; ingredient.EnergyKj = editIngredientViewModel.Ingredient.EnergyKj; ingredient.Water = editIngredientViewModel.Ingredient.Water; ingredient.EggWhite = editIngredientViewModel.Ingredient.EggWhite; ingredient.Carbohydrates = editIngredientViewModel.Ingredient.Carbohydrates; ingredient.Sugar = editIngredientViewModel.Ingredient.Sugar; ingredient.Fat = editIngredientViewModel.Ingredient.Fat; ingredient.SaturatedFat = editIngredientViewModel.Ingredient.SaturatedFat; ingredient.SingleSaturatedFat = editIngredientViewModel.Ingredient.SingleSaturatedFat; ingredient.MultiSaturatedFat = editIngredientViewModel.Ingredient.MultiSaturatedFat; ingredient.Cholesterol = editIngredientViewModel.Ingredient.Cholesterol; ingredient.Fiber = editIngredientViewModel.Ingredient.Fiber; _context.Update(ingredient); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError(string.Empty, "Could not edit the ingredient. Try again later."); } } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } return(View(editIngredientViewModel)); }
public async Task <IActionResult> Edit(int id, [Bind("LevelPreperationId,Level")] LevelPreperation levelPreperation) { if (id != levelPreperation.LevelPreperationId) { return(RedirectToAction("Error", "Home")); } if (ModelState.IsValid) { _context.Update(levelPreperation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(levelPreperation)); }
public async Task <IActionResult> Edit(int id, [Bind("UnitId,Name")] Unit unit) { if (id != unit.UnitId) { return(RedirectToAction("Error", "Home")); } if (ModelState.IsValid) { _context.Update(unit); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(unit)); }
public async Task <IActionResult> Edit(int id, [Bind("EndUserId,Name,LastName,UserName,Email,PasswordHash,Avatar,ApplicationUserId")] EndUser endUser) { if (id != endUser.EndUserId) { return(RedirectToAction("Error", "Home")); } if (ModelState.IsValid) { _context.Update(endUser); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(endUser)); }
public async Task <IActionResult> Edit(int id, [Bind("PreperationTimeId,RangeMinutes")] PreperationTime preperationTime) { if (id != preperationTime.PreperationTimeId) { return(RedirectToAction("Error", "Home")); } if (ModelState.IsValid) { _context.Update(preperationTime); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(preperationTime)); }
public async Task <IActionResult> Edit(int id, MealIngredientViewModel mealIngredientViewModel) { if (id != mealIngredientViewModel.MealIngredient.MealIngredientId) { return(RedirectToAction("Error", "Home")); } if (ModelState.IsValid) { _context.Update(mealIngredientViewModel.MealIngredient); await _context.SaveChangesAsync(); return(RedirectToAction("Create", new { id = mealIngredientViewModel.MealIngredient.MealId })); } mealIngredientViewModel.Ingredients = new SelectList(_context.Ingredients, "IngredientId", "Name", mealIngredientViewModel.MealIngredient.MealIngredientId); mealIngredientViewModel.Units = new SelectList(_context.Units, "UnitId", "Name", mealIngredientViewModel.MealIngredient.UnitId); return(View(mealIngredientViewModel)); }
public async Task <IActionResult> Edit(int id, CategoryViewModel categoryViewModel) { if (id != categoryViewModel.Category.CategoryId) { return(RedirectToAction("Error", "Home")); } Category checkCategoryName = await _context.Categories.SingleOrDefaultAsync(x => x.Name == categoryViewModel.Category.Name && x.CategoryId == categoryViewModel.Category.CategoryId); var result = new FluentValidation.Results.ValidationResult();//Bypass the name validator and didn't know how to do it any other way if (checkCategoryName != null) { var validator = new EditCategoryValidator(); result = validator.Validate(categoryViewModel.Category); } else { var validator = new CategoryValidator(_context.Categories); result = validator.Validate(categoryViewModel.Category); } if (result.IsValid) { _context.Update(categoryViewModel.Category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } categoryViewModel.Parents = new SelectList(_context.Categories.Where(x => x.ParentId == null), "CategoryId", "Name", categoryViewModel.Category.ParentId); return(View(categoryViewModel)); }
public async Task <IActionResult> Create(CreateMealViewModel model) { var userId = _userManager.GetUserId(User); EndUser endUser = await _context.EndUsers.FirstOrDefaultAsync(x => x.ApplicationUserId == userId); string uploadedFile = UploadedFile(model.MealImage); if (string.IsNullOrEmpty(uploadedFile)) { ModelState.AddModelError(string.Empty, "Image is not selected"); } if (endUser != null && ModelState.IsValid) { model.Meal.EndUserId = endUser.EndUserId; model.Meal.PictureMeal = uploadedFile; var validator = new MealValidator(_context.Meals); var 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 }); } _context.Add(model.Meal); await _context.SaveChangesAsync(); Meal meal = await _context.Meals .Include(mc => mc.MealCategories) .SingleOrDefaultAsync(meal => meal.MealId == model.Meal.MealId); meal.MealCategories.AddRange(newCategories); _context.Update(meal); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError(string.Empty, "Select at least one category!"); } } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } } 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 = new List <int>(); return(View(model)); }