public ActionResult Update(int id, Recipe recipe) { recipe.RecipeId = id; bool updated = recipeDAO.UpdateRecipe(recipe); if (updated) { return(NoContent()); } else { return(NotFound()); } }
public ActionResult ModifyRecipe(Recipe editRecipe) { // get my original recipe with no updates List <Recipe> myRecipes = recipeDAO.GetPrivateRecipes(this.UserId); Recipe originalRecipe = new Recipe(); foreach (Recipe recipe in myRecipes) { if (recipe.RecipeId == editRecipe.RecipeId) { originalRecipe = recipe; } } originalRecipe.IngredientList = recipeIngredientDAO.GetMyRecipeIngredients(originalRecipe.RecipeId, this.UserId); //original ingredient Id's List <int> originalIngredientIds = new List <int>(); foreach (RecipeIngredient ingredient in originalRecipe.IngredientList) { originalIngredientIds.Add(ingredient.IngredientId); } //update recipe details recipeDAO.UpdateRecipe(editRecipe); //add new ingredients into ingredients table if (editRecipe.NewIngredients.Count() > 0) { ingredientDAO.AddIngredients(editRecipe.NewIngredients); } //insert the new ingredients into recipes_ingredients foreach (RecipeIngredient eIngredient in editRecipe.IngredientList) { if (editRecipe.NewIngredients.Contains(eIngredient.IngredientName) || !originalIngredientIds.Contains(eIngredient.IngredientId)) { recipeIngredientDAO.AddIngredient(eIngredient, editRecipe.RecipeId); } } //edit quantity, unit, or delete ingredients foreach (RecipeIngredient oIngredient in originalRecipe.IngredientList) { bool notFound = true; for (int i = 0; i < editRecipe.IngredientList.Count; i++) { if (editRecipe.IngredientList[i].IngredientId == oIngredient.IngredientId) { notFound = false; if (editRecipe.IngredientList[i].Quantity != oIngredient.Quantity) { //update quantity recipeIngredientDAO.UpdateQuantity(editRecipe.IngredientList[i]); } if (editRecipe.IngredientList[i].Unit != oIngredient.Unit) { //update unit recipeIngredientDAO.UpdateUnit(editRecipe.IngredientList[i]); } } if (i == editRecipe.IngredientList.Count - 1 && notFound) { // delete the ingredient recipeIngredientDAO.DeleteIngredient(oIngredient); } } } return(Ok(editRecipe)); }