public async Task <IActionResult> UpdateIngredient([FromRoute] int id, [FromBody] IngredientRespondModel ingredient) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingIngredient = await _context.Ingredients.FindAsync(id); existingIngredient.Title = ingredient.Title; existingIngredient.Description = ingredient.Description; _context.Entry(existingIngredient).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IngredientExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> UpdateRecipe([FromRoute] int id, [FromBody] RecipesRespondModel recipeRespondModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (string.IsNullOrEmpty(recipeRespondModel.Title)) { return(BadRequest("Invalid Title")); } var recipe = await _context.Recipes.FindAsync(id); recipe.Title = recipeRespondModel.Title; recipe.Catagory = _context.Catagories.First(a => recipeRespondModel != null && a.Name.ToLower().Equals(recipeRespondModel.Category.ToLower())); recipe.Description = recipeRespondModel.Description; // delete IngredientInfoes DeleteRelevantIngredentInfosDetails(id); var recipeIngredentsInfo = new List <IngredientInfo>(); foreach (var kvp in recipeRespondModel.Ingredients) { var ingredient = _context.Ingredients.FirstOrDefault(a => a.Title.ToLower().Equals(kvp.Key.ToLower())); var ingredientInfo = new IngredientInfo() { Ingredient = ingredient, Qty = kvp.Value, Recipe = recipe }; recipeIngredentsInfo.Add(ingredientInfo); } recipe.IngredientInfoes = recipeIngredentsInfo; await _context.IngredientInfoes.AddRangeAsync(recipeIngredentsInfo); /// /// _context.Entry(recipe).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }