public async Task <int> EditAndSaveRecipe(RecipesEditViewModel viewModel) { HttpPostedFileBase file = viewModel.UploadedFile; bool ImageRefExists = !(viewModel.ImageReference == null); if (UploadedFileExists(file)) { if (IsImage(file)) { if (!ImageRefExists) //No existing ImageReference { viewModel.ImageReference = new ImageHandler() .SaveInitialImageAndGetReference(viewModel.UploadedFile); } else //Existing ImageReference { viewModel.ImageReference = new ImageHandler() .SaveEditedImageAndGetReference(viewModel.ImageReference, viewModel.UploadedFile); } } } Recipe recipe = new Recipe(viewModel); repository.Update(recipe); return(await repository.SaveChangesAsync()); }
public IActionResult Edit(RecipesEditViewModel recipesEditViewModel) { if (ModelState.IsValid) { var recipeModel = recipesEditViewModel.RecipeModel; // Check for existing ingredient if (recipeModel.RecipeIngredientModels != null) { foreach (var recipeIngredientModel in recipeModel.RecipeIngredientModels) { IngredientModel existingIngredient = _db.Ingredients .FirstOrDefault(x => x.Ingredient == recipeIngredientModel.IngredientsModel.Ingredient); if (existingIngredient != null) { recipeIngredientModel.IngredientsModel = existingIngredient; } } } RecipeModel recipe = GetRecipe(recipeModel.Id); recipe.RecipeName = recipeModel.RecipeName; recipe.RecipeDescription = recipeModel.RecipeDescription; recipe.RecipeType = recipeModel.RecipeType; recipe.RecipeIngredientModels = recipeModel.RecipeIngredientModels; recipe.InstructionModels = recipeModel.InstructionModels; recipe.UpdatedDated = DateTime.Now; string FileName = null; if (recipesEditViewModel.Photo != null) { if (recipeModel.RecipeInfoModel.PhotoPath != null) { DeleteOldPhotoPath(recipeModel); } var imageFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images", "uploadedImages"); FileName = $"{Guid.NewGuid()}_{recipesEditViewModel.Photo.FileName}"; var path = Path.Combine(imageFolder, FileName); using (var fileStream = new FileStream(path, FileMode.Create)) { recipesEditViewModel.Photo.CopyTo(fileStream); } recipeModel.RecipeInfoModel.PhotoPath = FileName; } recipe.RecipeInfoModel = recipeModel.RecipeInfoModel; _db.Update(recipe); _db.SaveChanges(); return(RedirectToAction("Details", new { id = recipe.Id })); } return(View(recipesEditViewModel.RecipeModel)); }
public Recipe(RecipesEditViewModel viewModel) { RecipeId = viewModel.RecipeId; RecipeName = viewModel.RecipeName; Ingredients = viewModel.Ingredients; Instructions = viewModel.Instructions; IsPublic = viewModel.IsPublic; OwnerId = viewModel.OwnerId; ImageReference = viewModel.ImageReference; Description = viewModel.Description; CookBooksContainingRecipe = viewModel.CookBooksContainingRecipe; }
public IActionResult Edit(int?id) { RecipeModel recipe = GetRecipe(id); if (recipe == null) { return(ErrorStatusCode404(id)); } var recipesEditViewModel = new RecipesEditViewModel { RecipeModel = recipe }; return(View(recipesEditViewModel)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Recipe recipe = db.Recipe.Find(id); if (recipe == null) { throw new HttpException(404, "Not found"); } var userId = HttpContext.User.Identity.Name; if (userId != recipe.User.UserName) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var vm = new RecipesEditViewModel { TypeOfDisches = db.Typeofdish.Select (x => new SelectListItem { Value = x.TypeofdishID.ToString(), Text = x.Type } ), TypeOfMeals = db.Typeofmeal.Select (x => new SelectListItem { Value = x.TypeofmealID.ToString(), Text = x.Mealstype } ), Recipe = recipe }; ViewBag.UserID = new SelectList(db.Users, "Id", "Email", recipe.UserID); return(View(vm)); }
public async Task <ActionResult> Edit([Bind(Include = "RecipeId,RecipeName,Ingredients,Instructions,IsPublic,OwnerId,UploadedFile,ImageReference,Description,CookBooksContainingRecipe")] RecipesEditViewModel viewModel) { if (ModelState.IsValid) { try { await new RecipeHandler().EditAndSaveRecipe(viewModel); return(RedirectToAction("Index", "CookBooks")); } catch (DbUpdateConcurrencyException) { ViewBag.Message = "It seems someone changed this recipe while you were editing it!"; return(View(viewModel)); } } return(View(viewModel)); }