public async Task <IActionResult> Create(MealIngredientViewModel mealIngredientViewModel) { var validator = new MealIngredientValidator(); var result = validator.Validate(mealIngredientViewModel.MealIngredient); List <MealIngredient> mealIngredientsCheck = new List <MealIngredient>(_context.MealIngredients .Include(x => x.Ingredient) .Where(x => x.MealId == mealIngredientViewModel.MealIngredient.MealId)); if (mealIngredientsCheck.Where(x => x.Ingredient.IngredientId == mealIngredientViewModel.MealIngredient.IngredientId).Count() > 0) { ModelState.AddModelError(string.Empty, "The meal already contains the meal ingredient"); } if (result.IsValid && ModelState.IsValid) { _context.Add(mealIngredientViewModel.MealIngredient); await _context.SaveChangesAsync(); return(RedirectToAction("Create", mealIngredientViewModel.MealId)); } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } mealIngredientViewModel.MealIngredients = new List <MealIngredient>(_context.MealIngredients.Where(x => x.MealId == mealIngredientViewModel.MealIngredient.MealId).Include(x => x.Meal)); mealIngredientViewModel.Ingredients = new SelectList(_context.Ingredients, "IngredientId", "Name", mealIngredientViewModel.MealIngredient.IngredientId); mealIngredientViewModel.Units = new SelectList(_context.Units, "UnitId", "Name", mealIngredientViewModel.MealIngredient.UnitId); return(View(mealIngredientViewModel)); }
public async Task <IActionResult> Create([Bind("EndUserId,Name,LastName,UserName,Email,PasswordHash,Avatar,ApplicationUserId")] EndUser endUser) { if (ModelState.IsValid) { _context.Add(endUser); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(endUser)); }
public async Task <IActionResult> Create([Bind("UnitId,Name")] Unit unit) { if (ModelState.IsValid) { _context.Add(unit); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(unit)); }
public async Task <IActionResult> Create([Bind("LevelPreperationId,Level")] LevelPreperation levelPreperation) { if (ModelState.IsValid) { _context.Add(levelPreperation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(levelPreperation)); }
public async Task <IActionResult> Create([Bind("PreperationTimeId,RangeMinutes")] PreperationTime preperationTime) { if (ModelState.IsValid) { _context.Add(preperationTime); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(preperationTime)); }
public async Task <IActionResult> Create(IngredientViewModel createIngredientViewModel) { var validator = new IngredientValidator(_context.Ingredients); var result = validator.Validate(createIngredientViewModel.Ingredient); if (result.IsValid) { _context.Add(createIngredientViewModel.Ingredient); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { foreach (var item in result.Errors) { ModelState.AddModelError(string.Empty, item.ErrorMessage); } } return(View(createIngredientViewModel)); }
public async Task <IActionResult> Create(CategoryViewModel categoryViewModel) { var validator = new CategoryValidator(_context.Categories); var result = validator.Validate(categoryViewModel.Category); if (result.IsValid) { _context.Add(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"); 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)); }