public async Task EditAsync(RecipeEditServiceModel recipe) { var articleForEdit = this.db.Recipes.Where(a => a.Id == recipe.Id).FirstOrDefault(); articleForEdit.Title = recipe.Title; articleForEdit.Content = recipe.Content; if (!string.IsNullOrEmpty(recipe.ImageName)) { articleForEdit.ImageName = recipe.ImageName; } this.db.Recipes.Update(articleForEdit); await this.db.SaveChangesAsync(); }
public async Task <IActionResult> Edit(RecipeEditServiceModel recipe, IFormFile Image) { if (!ModelState.IsValid) { return(View(recipe)); } recipe.Content = this.html.Sanitize(recipe.Content); if (Image != null) { recipe.ImageName = await SaveImage(Image); } await this.recipeService.EditAsync(recipe); TempData[WebConstants.TempDataSuccessMessageKey] = ($"Article {recipe.Title} successfuly updated."); return(RedirectToAction(nameof(RecipeController.Index), new { page = 1 })); }
public void Edit(RecipeEditServiceModel model) { if (String.IsNullOrWhiteSpace(model.Name) || String.IsNullOrEmpty(model.Name) || model.Name.Length > 30) { throw new ArgumentException("Recipe name cannot be null or more thsn 30 characters."); } if (model.CookingTime <= 0) { throw new ArgumentException("Cooking time be negative number or equal to zero."); } if (model.Portion <= 0) { throw new ArgumentException("Portion cannot be negative number or equal to zero."); } if (String.IsNullOrEmpty(model.ContentIngredients) || String.IsNullOrWhiteSpace(model.ContentIngredients)) { throw new ArgumentException("Content Ingredients property cannot be null."); } var recipe = this.context.Recipes .Where(x => x.IsDeleted == false) .Where(x => x.Id == model.Id) .FirstOrDefault(); if (recipe == null) { throw new ArgumentException("This recipe is probably deleted."); } recipe.Name = model.Name; recipe.CookingTime = model.CookingTime; recipe.CategoryId = model.CategoryId; recipe.Level = model.Level; recipe.Portion = model.Portion; recipe.ContentIngredients = model.ContentIngredients; recipe.PreparationMethod = model.PreparationMehtod; this.context.SaveChanges(); }