public IActionResult Put(int id, [FromBody] Ingredient input) { var oldIngredient = _ingredientData.GetById(input.Id); if (oldIngredient == null) { return(NotFound()); } _ingredientData.Update(input); _ingredientData.Commit(); return(Ok()); }
public IActionResult OnPost(int recipeId) { if (!ModelState.IsValid) { return(Page()); } if (Ingredient.Id > 0) { ingredientData.Update(Ingredient); } else { Ingredient.RecipeId = recipeId; ingredientData.Add(Ingredient); } ingredientData.Commit(); return(RedirectToPage("./Detail", new { IngredientId = Ingredient.Id })); }
public async Task <IActionResult> OnPostAsync() { var userId = userManager.GetUserId(User); if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } if (NewImage != null) { var image = await imageData.UploadImageAsync(NewImage); Recipe.ImageUrl = image; } // Update existing Ingredients var savedIngredients = new List <int>(); foreach (var ingredient in Recipe.Ingredients) { if (ingredient.Id > 0) { savedIngredients.Add(ingredient.Id); ingredientData.Update(ingredient); } } // Delete removed ingredients var originalRecipe = recipeData.GetById(Recipe.Id); var deletedIngredients = originalRecipe.Ingredients.Where(i => !savedIngredients.Contains(i.Id)); foreach (var deletedIngredient in deletedIngredients) { ingredientData.Delete(deletedIngredient.Id); } // Update Recipe recipeData.Update(Recipe); TempData["Message"] = "Recipe Updated."; recipeData.Commit(); return(RedirectToPage("./Detail", new { recipeId = Recipe.Id })); }
public IActionResult Edit(Ingredient ingredient) { if (!ModelState.IsValid) { var viewModel = new IngredientViewModel { Ingredient = ingredient }; return(View(viewModel)); } if (ingredient.Id > 0) { _ingredientData.Update(ingredient); } else { _ingredientData.Add(ingredient); } _ingredientData.Commit(); return(RedirectToAction("Detail", new { area = "Common", ingredientId = ingredient.Id })); }