// GET: AdminRecipes/Edit/5 public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } var recipe = _context.Recipe.Where(r => r.RecipeID == id).FirstOrDefault(); if (recipe == null) { return(NotFound()); } AdminRecipeEditViewModel model = new AdminRecipeEditViewModel(); model.RecipeID = recipe.RecipeID; model.ExistingImageUrl = recipe.ImageUrl; model.ExistingImagePath = Path.Combine("\\lib", "images", "recipes", recipe.ImageUrl).Replace('\\', '/'); model.UserID = recipe.UserID; model.RecipeName = recipe.RecipeName; model.RecipeSteps = _context.RecipeSteps.Where(rs => rs.RecipeID == recipe.RecipeID).ToList(); model.Servings = recipe.Servings; model.RecipeDescription = recipe.RecipeDescription; model.PrepareTime = recipe.PrepareTime; model.RecipeStatus = recipe.RecipeApprovalStatus; List <Ingredient> ingredients = _context.Ingredient.Where(i => i.RecipeID == recipe.RecipeID).ToList(); foreach (Ingredient ingredient in ingredients) { model.Ingredients.Add(new IngredientViewModel() { IngredientName = ingredient.IngredientName, ServingContent = ingredient.ServingContent }); } var statuses = from RecipeApprovalStatus s in Enum.GetValues(typeof(RecipeApprovalStatus)) select new { ID = s, Name = s.ToString() }; SelectList myList = new SelectList(statuses, "ID", "Name"); ViewBag.RecipeStatus = myList; return(View(model)); }
public async Task <IActionResult> Edit( [Bind("RecipeID", "ExistingImageUrl", "RecipeStatus", "RecipeName", "RecipeDescription", "Servings", "PrepareTime", "UserID", "Image", "Ingredients", "RecipeSteps")] AdminRecipeEditViewModel model) { Recipe recipe = _context.Recipe.Where(r => r.RecipeID == model.RecipeID).Include(r => r.RecipeSteps).Include(r => r.Ingredients).FirstOrDefault(); if (recipe == null) { return(RedirectToAction(nameof(Index))); } List <Ingredient> TempIngredient = await MatchRecipeProduct(model.Ingredients); if (counter >= TempIngredient.Count) { ModelState.AddModelError("", "Recipe is Required to Have Minimum of 1 Ingredient"); } else if (invalidProducts.Count > 0) { foreach (string invalidProduct in invalidProducts) { ModelState.AddModelError("", "Unable to find matched product for " + invalidProduct); } } if (ModelState.IsValid) { foreach (RecipeSteps steps in recipe.RecipeSteps) { _context.RecipeSteps.Remove(steps); } foreach (Ingredient ingredient in recipe.Ingredients) { _context.Ingredient.Remove(ingredient); } ApplicationUser user = await _UserManager.FindByIdAsync(model.UserID); recipe.RecipeID = model.RecipeID; recipe.RecipeApprovalStatus = model.RecipeStatus; if (model.RecipeStatus == RecipeApprovalStatus.Approved) { recipe.ApprovedDate = DateTime.Now; } recipe.AddedDate = DateTime.Now; recipe.RecipeName = model.RecipeName; recipe.UserID = model.UserID; recipe.PrepareTime = model.PrepareTime; recipe.Servings = model.Servings; recipe.RecipeDescription = model.RecipeDescription; recipe.RecipeSteps = model.RecipeSteps; recipe.Ingredients = TempIngredient; if (model.PrepareTime.Any(char.IsDigit)) { Regex regex = new Regex(@"^(?<NUMVALUE>\d+.?\d*)\s*(?<STRVALUE>[A-Za-z]*)$", RegexOptions.Singleline); Match match = regex.Match(recipe.PrepareTime); recipe.PrepareTimeDuration = match.Groups["NUMVALUE"].Value; recipe.PrepareTimeMeasurement = match.Groups["STRVALUE"].Value; } if (model.Image != null) { string filePath = Path.Combine(_HostingEnvironment.WebRootPath, "lib", "images", "recipes", recipe.ImageUrl); System.IO.File.Delete(filePath); recipe.ImageUrl = ProcessUploadedFile(model.Image); } else { recipe.ImageUrl = model.ExistingImageUrl; } _context.Update(recipe); await _context.SaveChangesAsync(); //Change the return URL LATER return(RedirectToAction(nameof(Index))); } var statuses = from RecipeApprovalStatus s in Enum.GetValues(typeof(RecipeApprovalStatus)) select new { ID = s, Name = s.ToString() }; SelectList myList = new SelectList(statuses, "ID", "Name"); model.ExistingImagePath = Path.Combine("\\", "lib", "images", "recipes", model.ExistingImageUrl).Replace('\\', '/'); return(View(model)); }