Example #1
0
        public ActionResult GetRecipe(int recipeId)
        {
            // call recipeDAO to get list of recipes, get recipe, then check parameters

            List <Recipe> privateListOfRecipe = recipeDAO.GetPrivateRecipes(this.UserId);

            List <RecipeIngredient> listOfRecipeIngredients = new List <RecipeIngredient>();

            List <Recipe> publicListOfRecipe = recipeDAO.GetPublicRecipes();


            foreach (Recipe recipe in privateListOfRecipe)
            {
                if (recipe.UserId == this.UserId && recipe.RecipeId == recipeId)
                {
                    listOfRecipeIngredients = recipeIngredientsDAO.GetMyRecipeIngredients(recipe.RecipeId, this.UserId);
                    return(Ok(listOfRecipeIngredients));
                }
            }

            foreach (Recipe recipe in publicListOfRecipe)
            {
                if (recipe.RecipeId == recipeId && recipe.IsShared == true)
                {
                    listOfRecipeIngredients = recipeIngredientsDAO.GetPublicRecipeIngredients(recipeId);
                    return(Ok(listOfRecipeIngredients));
                }
            }
            return(Forbid());



            //if (recipe.IsShared)
            //{
            //    List<RecipeIngredient> listOfRecipeIngredients = recipeIngredientsDAO.GetPublicRecipeIngredients(recipe.RecipeId);
            //    return Ok(listOfRecipeIngredients);
            //}
            //else if (recipe.UserId == this.UserId)
            //{
            //    List<RecipeIngredient> listOfRecipeIngredients = recipeIngredientsDAO.GetMyRecipeIngredients(recipe.RecipeId, this.UserId);
            //    return Ok(listOfRecipeIngredients);
            //}
            //return Forbid();
        }
Example #2
0
        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));
        }