Exemplo n.º 1
0
        public ActionResult AddPost()
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");
            IPostsContext postsContext = new PostsContext();
            IPostImageContext postImageContext = new PostImageContext();
            IUsersContext usersContext = new UsersContext();
            IImagesContext imageContext = new ImagesContext();
            ICategoriesContext categoriesContext = new CategoriesContext();

            User currentUser = new User();
            currentUser = usersContext.GetUserByLogin((string)Session["login"]);

            Post newPost = new Post();
            newPost.id_user = currentUser.id;
            newPost.title = Request.Form["posttitle"];
            newPost.text = Request.Form["posttext"];

            if (Request.Form["menu-val"] == "newCat")
            {
                Categories cat = new Categories();
                cat.value = Request.Form["catText"];
                newPost.category_id = categoriesContext.CreateCategory(cat).id;
            }
            else if(Convert.ToInt32(Request.Form["menu-val"]) == -1)
            {
                if (categoriesContext.GetCategoryByValue("Общая") == null)
                {
                    Categories cat = new Categories();
                    cat.value = "Общая";
                    newPost.category_id = categoriesContext.CreateCategory(cat).id;
                }
                else
                {
                    Categories cat = new Categories();
                    cat = categoriesContext.GetCategoryByValue("Общая");
                    newPost.category_id = cat.id;
                }

            }
            else
            {
                newPost.category_id = Convert.ToInt32(Request.Form["menu-val"]);
            }

            newPost = postsContext.AddPost(newPost);

            var file = Request.Files["post_image"];

            if (file.ContentLength != 0)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "images/posts/";
                string filename = newPost.id.ToString() + Path.GetExtension(file.FileName);
                if (filename != null) file.SaveAs(path + filename);
                Image image = new Image();
                image.image_path = filename;
                image = imageContext.AddImage(image.image_path);
                postImageContext.AddPostImage(newPost.id, image.id);
            }
            return RedirectToAction("userProfile", "Account");
        }
Exemplo n.º 2
0
        public ActionResult EditPostF()
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");
            IPostsContext postsContext = new PostsContext();
            IPostImageContext postImageContext = new PostImageContext();
            IUsersContext usersContext = new UsersContext();
            IImagesContext imageContext = new ImagesContext();
            string url = "~/Post/PostPage?post=";

            User currentUser = new User();
            currentUser = usersContext.GetUserByLogin((string)Session["login"]);

            Post newPost = new Post();
            newPost.id = Convert.ToInt32(Request.Form["postId"]);
            newPost.id_user = currentUser.id;
            newPost.title = Request.Form["posttitle"];
            newPost.text = Request.Form["posttext"];
            newPost.category_id = Convert.ToInt32(Request.Form["menu-val"]);

            newPost = postsContext.EditPost(newPost);

            var file = Request.Files["post_image"];

            if (file.ContentLength != 0)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "images/posts/";
                string filename = newPost.id.ToString() + Path.GetExtension(file.FileName);
                if (filename != null) file.SaveAs(path + filename);
                if(postImageContext.GetImageByPostId(newPost.id) == null)
                {
                    Image image = new Image();
                    image.image_path = filename;
                    image = imageContext.AddImage(image.image_path);
                    postImageContext.AddPostImage(newPost.id, image.id);
                }
                else
                {
                    Image image = new Image();
                    image = postImageContext.GetImageByPostId(newPost.id);
                    image.image_path = filename;
                    image = imageContext.EditImage(image);
                }
            }
            return Redirect(url + newPost.id);
        }