public async Task <IActionResult> Edit(int id) { try { var ingredientDTO = await this.ingredientService.GetIngredientAsync(id); if (ingredientDTO == null) { this.toaster.AddWarningToastMessage(ToastrConsts.NotFound); return(RedirectToAction(nameof(Index))); } var editIngredientVM = new EditIngredientViewModel { Id = ingredientDTO.Id, Name = ingredientDTO.Name }; return(View(editIngredientVM)); } catch (Exception) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction(nameof(Index))); } }
public ActionResult Edit(EditIngredientViewModel viewModel) { if (viewModel == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (!User.IsInRole("Administrator")) { return(View("Error")); } var ingredientEntity = _ingredientsRepository.GetById(viewModel.Id); ingredientEntity.Name = viewModel.Name; TryValidateModel(ingredientEntity); if (!ModelState.IsValid) { this.AddNotification("Грешка при запазване на промените: Името на съставката не е валидно.", NotificationType.ERROR); return(RedirectToAction("Index", new { page = viewModel.CurrentPage, searchText = viewModel.SearchText })); } _ingredientsRepository.Update(ingredientEntity); if (!_ingredientsRepository.Save()) { this.AddNotification("Грешка при запазване на промените: Името на съставката не е валидно.", NotificationType.ERROR); return(RedirectToAction("Index", new { page = viewModel.CurrentPage, searchText = viewModel.SearchText })); } this.AddNotification("Съставката е променена.", NotificationType.SUCCESS); return(RedirectToAction("Index", new { page = viewModel.CurrentPage, searchText = viewModel.SearchText })); }
public IHttpActionResult Update(EditIngredientViewModel model) { var ingredient = _ingredientRepository.GetById(model.Id); ingredient.Name = model.Name; _ingredientRepository.Update(ingredient); return(Ok(ingredient)); }
public IngredientDTO MapToDTOFromVM(EditIngredientViewModel editIngredientVM) { if (editIngredientVM == null) { return(null); } var ingredientDTO = new IngredientDTO { Id = editIngredientVM.Id, Name = editIngredientVM.Name, }; if (editIngredientVM.File != null) { ingredientDTO.ImageData = editIngredientVM.ImageData; } return(ingredientDTO); }
public JsonResult EditRecipeIngredient(EditIngredientViewModel model) { bool isNewIngredient = false; var existingIngredient = _recipeService.GetIngredientByName(model.IngredientName); if (existingIngredient == null) { isNewIngredient = true; existingIngredient = _recipeService.AddIngredient(model.IngredientName); } var recipeIngredientToAdd = _recipeService.AddRecipeIngredient(model.RecipeId, model.Measurement, model.UnitOfMeasurement, existingIngredient.Id); return(Json(new { RecipeId = recipeIngredientToAdd.RecipeId, Measurement = recipeIngredientToAdd.Measurement, UnitOfMeasurement = recipeIngredientToAdd.UnitOfMeasurement, IngredientId = recipeIngredientToAdd.IngredientId, IngredientName = model.IngredientName, IsNewIngredient = isNewIngredient })); }
public async Task <IActionResult> Edit(int id, EditIngredientViewModel editIngredientVM) { var sanitizer = new HtmlSanitizer(); editIngredientVM.Name = sanitizer.Sanitize(editIngredientVM.Name); if (id != editIngredientVM.Id) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction(nameof(Index))); } if (ModelState.IsValid) { try { if (editIngredientVM.File != null && editIngredientVM.File.Length > 0) { MemoryStream ms = new MemoryStream(); await editIngredientVM.File.CopyToAsync(ms); editIngredientVM.ImageData = ms.ToArray(); ms.Close(); ms.Dispose(); } var ingredientDTO = this.ingredientDTOMapper.MapToDTOFromVM(editIngredientVM); var validationResult = this.ingredientService.ValidateIngredient(ingredientDTO); if (!validationResult.HasProperInputData) { this.toaster.AddWarningToastMessage(ToastrConsts.NullModel); return(RedirectToAction(nameof(Edit), new { id })); } if (!validationResult.HasProperNameLength) { this.toaster.AddWarningToastMessage(ToastrConsts.WrongNameLength); return(RedirectToAction(nameof(Edit), new { id })); } if (!validationResult.HasValidName) { this.toaster.AddWarningToastMessage(ToastrConsts.NameNotValid); return(RedirectToAction(nameof(Edit), new { id })); } await this.ingredientService.UpdateIngredientAsync(id, ingredientDTO); this.toaster.AddSuccessToastMessage(ToastrConsts.Success); return(RedirectToAction("Details", "Ingredients", new { id })); } catch (ArgumentException) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction(nameof(Index))); } } this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectInput); return(RedirectToAction(nameof(Edit), new { id })); }