Example #1
0
        public ActionResult AddRecipe(RecipeModel recipe)
        {
            if (recipe.ChoseCategory == null || recipe.Ingredients == null || recipe.Name == null || recipe.Tags == null || recipe.Directions == null)
            {
                return(RedirectToAction("AddRecipe"));
            }
            UserModel user = Session["user"] as UserModel;

            // When a user logs in, Session[authorizationlevel] stores their auth level as 1, 2 ,3 or null.  From the Authorize class,
            // runs the Admin method, taking in Session cast as a int?
            // If the method returns true, only admins will be able to do this action, else returns redirect to another action.
            if (Authorize.Registered((int?)Session["authorizationlevel"]) == true || Authorize.Admin((int?)Session["authorizationlevel"]) == true)
            {
                List <string> tagArray = new List <string>();
                string        fileName = "";
                //try
                //{
                //    if (ImageName.ContentLength > 0)
                //    {
                //        fileName = Path.GetFileName(ImageName.FileName);
                //        string path = Path.Combine(Server.MapPath("~/Img/"), fileName);
                //        ImageName.SaveAs(path);
                //    }
                //    ViewBag.Message = "File Uploaded Successfully!";
                //}
                //catch
                //{
                //    ViewBag.Message = "File Upload Failed!";
                //}
                //recipe.ImageName = fileName;
                if (recipe.PublicOrPrivate != null)
                {
                    recipe.Publics = Int32.Parse(recipe.PublicOrPrivate);
                }
                int recipeId = recipeDal.NewRecipe(recipe);
                recipeDal.InsertRecipeIdAndUserId(user.UserID, recipeId);
                if (recipe.Tags != null)
                {
                    tagArray = recipe.Tags.Split(';').ToList <string>();
                    for (int i = 0; i < tagArray.Count; i++)
                    {
                        tagArray[i] = tagArray[i].ToLower();
                    }
                }

                List <int> exists = recipeDal.TagsExist(recipe.Tags);
                if (tagArray.Count != 0)
                {
                    for (int i = 0; i < tagArray.Count; i++)
                    {
                        if (exists[i] > 0)
                        {
                            int tagId = recipeDal.GetTagIdIfExists(tagArray[i].TrimStart(' '));
                            recipeDal.InsertRecipeIdAndTagId(recipeId, tagId);
                        }
                        else
                        {
                            int tagId = recipeDal.GetTagIdAfterInsert(tagArray[i].TrimStart(' '));
                            recipeDal.InsertRecipeIdAndTagId(recipeId, tagId);
                        }
                    }
                }
                foreach (KeyValuePair <string, bool> kvp in recipe.ChoseCategory)
                {
                    if (kvp.Value == true)
                    {
                        int catId = recipeDal.GetCategoryId(kvp.Key);
                        recipeDal.InsertRecipeAndCategoryId(recipeId, catId);
                    }
                }

                return(RedirectToAction("RecipeConfirmation"));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }