Пример #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");
        }
Пример #2
0
        //Загрузка главной страницы
        public ActionResult Index()
        {
            IPostsContext postsContext = new PostsContext();
            IUsersContext usersContext = new UsersContext();
            ICategoriesContext catContext = new CategoriesContext();
            IEnumerable<Post> posts = postsContext.GetAllPosts();
            List<Categories> categories = new List<Categories>();
            if (catContext.GetAllCategories() != null)
            {
                categories = catContext.GetAllCategories().ToList();
                ViewBag.categories = categories;
            }
            ViewBag.usersContext = usersContext;
            ViewBag.catContext = catContext;
            User currUser = new User();
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                currUser = usersContext.GetUserByLogin(Session["login"].ToString());
            else currUser = null;
            ViewBag.currUser = currUser;

            IPostImageContext pic = new PostImageContext();
            List<Image> postImageList = new List<Image>();

            if (posts != null)
            {
                foreach (var p in posts)
                {
                    postImageList.Add(pic.GetImageByPostId(p.id));
                }
            }
            if (postImageList != null)
                ViewBag.postImageList = postImageList;

            return View();
        }
Пример #3
0
 //Поиск среди статей по ключевым словам
 public List<Post> SearchPosts(string words)
 {
     string __words = words.Trim();
     string[] _words = __words.Split(' ', ',', '.');
     List<Post> foundPosts = new List<Post>();
     IPostsContext postsContext = new PostsContext();
     IEnumerable<Post> posts = postsContext.GetAllPosts();
     foreach (var u in posts)
     {
         foreach (var w in _words)
             if (u.title.ToLower().Contains(w.ToLower()) || u.text.Contains(w)) foundPosts.Add(u);
     }
     return foundPosts;
 }
Пример #4
0
        public ActionResult AddComment()
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");

            IPostsContext postsContext = new PostsContext();
            IPostsCommentsContext postsCommentsContext = new PostsCommentsContext();
            IUsersContext usersContext = new UsersContext();
            PostsComments comment = new PostsComments();
            int postId = Convert.ToInt32(Request.Form["postId"]);
            if (postId == 0) return RedirectToAction("Index", "Home");
            comment.post_id = postId;
            comment.user_id = Convert.ToInt32(Request.Form["userId"]);
            comment.text = Request.Form["commentText"];
            string url = Request.Form["postURL"];
            if (Request.Form["commentText"] == null)
                if (url != null)
                    return Redirect(url);
                else
                    return RedirectToAction("Index", "Home");

            comment = postsCommentsContext.AddPostComment(comment);

            return Redirect(url);
        }
Пример #5
0
        public ActionResult UserProfile() // Загрузка представленя страницы пользователя
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");
            IUsersContext _user = new UsersContext();
            IPostsContext postsContext = new PostsContext();
            IUserImageContext userImageContext = new UserImageContext();
            ICategoriesContext catContext = new CategoriesContext();

            List<Categories> categories = new List<Categories>();
            if (catContext.GetAllCategories() != null)
            {
                categories = catContext.GetAllCategories().ToList();
                ViewBag.categories = categories;
            }
            ViewBag.catContext = catContext;

            int s = 0;
            if (Request.QueryString.Count == 0) s = 0;
            else
                s = Convert.ToInt32(Request.QueryString["user"]);
            if (s == null || s == 0) s = _user.GetUserByLogin(Convert.ToString(Session["login"])).id;
            User user = new User();
            user = _user.GetUserById(s);

            ViewBag.login = user.login;
            ViewBag.user = user;
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                ViewBag.sessionUser = _user.GetUserByLogin(Session["login"].ToString());

            IEnumerable<Post> userPosts;
            userPosts = postsContext.GetPostsByUserId(user.id);

            IPostImageContext pic = new PostImageContext();
            List<Image> postImageList = new List<Image>();
            Image userImage = new Image();
            userImage = userImageContext.GetImageByUserId(user.id);
            if (userImage != null)
                ViewBag.userImage = userImage.image_path;
            else
                ViewBag.userImage = "default.png";

            if (userPosts != null)
            {
                foreach (var p in userPosts)
                {
                    postImageList.Add(pic.GetImageByPostId(p.id));
                }
            }
            ViewBag.posts = userPosts;
            if (postImageList != null)
                ViewBag.postImageList = postImageList;
            return View();
        }
Пример #6
0
        public List<Post> getPostsByUploading(int startFrom, int category)
        {
            PostsContext _db = new PostsContext();
            List<Post> postList = new List<Post>();
            IEnumerable<Post> _currUserPosts;
            Post post = new Post();

            if (category != 0)
            {
                var tposts = _db.Database.SqlQuery(typeof(Post), "SELECT * FROM posts WHERE category_id = " + category + " ORDER BY id DESC OFFSET " + startFrom + " ROWS FETCH NEXT " + Config.pageItems + " ROWS ONLY ");
                return tposts.OfType<Post>().ToList();
            }
            else
            {
                var tposts = _db.Database.SqlQuery(typeof(Post), "SELECT * FROM posts ORDER BY id DESC OFFSET " + startFrom + " ROWS FETCH NEXT " + Config.pageItems + " ROWS ONLY ");
                return tposts.OfType<Post>().ToList();
            }

            return _currUserPosts.ToList();
        }
Пример #7
0
 public Post EditPost(Post newPost)
 {
     if (newPost.id_user != null && newPost.id_user != 0 && newPost.title.Trim() != "")
     {
         PostsContext _db = new PostsContext();
         _db.Entry(newPost).State = EntityState.Modified;
         _db.SaveChanges();
         return newPost;
     }
     return null;
 }
Пример #8
0
 public bool dellPost(int id)
 {
     PostsContext _db = new PostsContext();
     var comment = _db.posts.Where(p => p.id == id).FirstOrDefault();
     if (comment != null)
     {
         _db.posts.Remove(comment);
         _db.SaveChanges();
     }
     return false;
 }
Пример #9
0
 public Post AddPost(Post newPost)
 {
     if (newPost.id_user != null && newPost.id_user != 0 && newPost.title.Trim() != "")
     {
         PostsContext _db = new PostsContext();
         _db.posts.Add(newPost);
         _db.SaveChanges();
         var postId = _db.posts.Select(id => id.id).Max();
         newPost.id = postId;
         return newPost;
     }
     return null;
 }
Пример #10
0
        public ActionResult SearchResult(string words)
        {
            if (words != null && words != "")
            {
                Session["search"] = words;
            }
            else
            {
                if (Session["search"] == null)
                    if (Request.Form["search"] != null)
                        words = Request.Form["search"].ToString();
                    else words = "";
                else
                    words = Session["search"].ToString();
            }
            if (words == null) words = "";
            List<Post> foundPosts = SearchPosts(words);

            IPostsContext postsContext = new PostsContext();
            IUsersContext usersContext = new UsersContext();
            ICategoriesContext catContext = new CategoriesContext();
            ViewBag.usersContext = usersContext;
            ViewBag.catContext = catContext;
            ViewBag.posts = foundPosts;
            User currUser = new User();
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                currUser = usersContext.GetUserByLogin(Session["login"].ToString());
            else currUser = null;
            ViewBag.currUser = currUser;

            IPostImageContext pic = new PostImageContext();
            List<Image> postImageList = new List<Image>();

            if (foundPosts != null)
            {
                foreach (var p in foundPosts)
                {
                    postImageList.Add(pic.GetImageByPostId(p.id));
                }
            }
            if (postImageList != null)
                ViewBag.postImageList = postImageList;

            ViewBag.words = words;
            ViewBag.foundPosts = foundPosts;
            return View();
        }
Пример #11
0
        public ActionResult PostPage()
        {
            if (Request.QueryString["post"] == null) RedirectToAction("Index", "Home");
            ICategoriesContext catContext = new CategoriesContext();
            IPostsContext postsContext = new PostsContext();
            IPostsCommentsContext postsCommentsContext = new PostsCommentsContext();
            IUsersContext usersContext = new UsersContext();
            User currentUser = new User();
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                currentUser = usersContext.GetUserByLogin(Session["login"].ToString());
            else
                currentUser = null;
            ViewBag.currentUser = currentUser;
            ViewBag.usersContext = usersContext;
            Post post = new Post();
            if (Request.QueryString["post"] == null) return RedirectToAction("Index", "Home");
            post = postsContext.GetPostById(Convert.ToInt32(Request.QueryString["post"]));
            if (post == null) return RedirectToAction("Index", "Home");
            User postUser = usersContext.GetUserById(post.id_user);
            ViewBag.postUser = postUser;
            ViewBag.categories = catContext.GetAllCategories().ToList();
            ViewBag.catContext = catContext;

            IPostImageContext postImageContext = new PostImageContext();
            if (postImageContext.GetImageByPostId(post.id) != null)
                ViewBag.postImage = postImageContext.GetImageByPostId(post.id).image_path;
            ViewBag.post = post;
            IEnumerable<PostsComments> postComments = postsCommentsContext.GetPostsCommentsByPostId(post.id);

            int page = 1;
            if (Request.QueryString["page"] != null)
                page = Convert.ToInt32(Request.QueryString["page"]);

            if (postComments != null)
            {
                List<PostsComments> _postsComments = new List<PostsComments>();
                _postsComments = postComments.ToList();
                List<PostsComments> currComments = new List<PostsComments>(Config.pageItems);
                int pagination = GetPagination(_postsComments.Count);
                int start = 0, end = 0;

                start = (page - 1) * Config.pageItems;
                if (_postsComments.Count == 1) end = 0;
                else
                    if (_postsComments.Count < Config.pageItems) end = _postsComments.Count - 1;
                    else
                        if (page == pagination && _postsComments.Count % Config.pageItems > 0)
                            end = _postsComments.Count - 1;
                        else
                            end = page * Config.pageItems - 1;

                for (int i = start; i <= end; i++)
                {
                    currComments.Add(_postsComments[i]);
                }
                ViewBag.currentPage = page;
                ViewBag.pagination = pagination;
                ViewBag.postComments = currComments;
            }
            else
            {
                page = 0;
            }
            return View();
        }
Пример #12
0
        public JsonResult GetPosts()
        {
            IPostsContext postsContext = new PostsContext();
            IPostImageContext postImageContext = new PostImageContext();
            IUsersContext usersContext = new UsersContext();
            ICategoriesContext catContext = new CategoriesContext();
            IEnumerable<Post> posts = postsContext.GetAllPosts();
            ViewBag.usersContext = usersContext;
            ViewBag.catContext = catContext;
            ViewBag.posts = posts.ToList();
            User currUser = new User();
            if (Session != null && Session["isAuth"] != null && (bool)Session["isAuth"] != false)
                currUser = usersContext.GetUserByLogin(Session["login"].ToString());
            else currUser = null;

            List<Post> sortedPosts = new List<Post>();

            int category = Convert.ToInt32(Request.Form["category"]);
            int startFrom = Convert.ToInt32(Request.Form["startFrom"]);
            sortedPosts = postsContext.GetPostsByUploading(startFrom, category);

            List<HTPosts> htPosts = new List<HTPosts>();

            foreach (var p in sortedPosts)
            {
                HTPosts hp;
                hp.id = p.id;
                hp.user_id = p.id_user;
                if (currUser == null)
                    hp.currUser_id = 0;
                else hp.currUser_id = currUser.id;

                hp.title = p.title;
                hp.text = p.text;
                hp.category = catContext.GetCategoryById(p.category_id).value;
                if (postImageContext.GetImageByPostId(p.id) != null)
                    hp.image_name = postImageContext.GetImageByPostId(p.id).image_path;
                else
                    hp.image_name = "";
                hp.userName = usersContext.GetUserById(p.id_user).name;
                hp.userLastName = usersContext.GetUserById(p.id_user).last_name;

                htPosts.Add(hp);
            }

            JsonResult data = Json(htPosts);

            return data;
        }
Пример #13
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);
        }
Пример #14
0
        public ActionResult EditPost()
        {
            IPostsContext postsContext = new PostsContext();
            IPostImageContext postImageContext = new PostImageContext();
            IUsersContext usersContext = new UsersContext();
            Post post = new Post();
            string url = "~/Post/PostPage?post=";
            post = postsContext.GetPostById(Convert.ToInt32(Request.QueryString["post"]));
            ViewBag.post = post;
            if (post.id_user != usersContext.GetUserByLogin(Session["login"].ToString()).id) return Redirect(url + post.id);

            ICategoriesContext catContext = new CategoriesContext();
            List<Categories> categories = new List<Categories>();
            categories = catContext.GetAllCategories().ToList();
            ViewBag.categories = categories;

            return View();
        }
Пример #15
0
        public ActionResult DellPost()
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");

            IPostsContext postsContext = new PostsContext();
            IPostsCommentsContext postsCommentsContext = new PostsCommentsContext();
            IUsersContext usersContext = new UsersContext();
            Post post = new Post();
            post = postsContext.GetPostById(Convert.ToInt32(Request.Form["postId"]));
            if (post == null || post.id_user != usersContext.GetUserByLogin(Session["login"].ToString()).id) return RedirectToAction("Index", "Home");
            postsContext.DellPost(post.id);
            string url = "~/Post/PostPage?post=" + post.id;
            return RedirectToAction("Index", "Home");
        }
Пример #16
0
        public ActionResult DellComment()
        {
            if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account");

            IPostsContext postsContext = new PostsContext();
            IPostsCommentsContext postsCommentsContext = new PostsCommentsContext();
            IUsersContext usersContext = new UsersContext();
            PostsComments comment = new PostsComments();
            comment = postsCommentsContext.GetPostCommentById(Convert.ToInt32(Request.Form["commentId"]));
            if (comment == null) return RedirectToAction("Index", "Home");
            postsCommentsContext.DellComment(comment.id);
            string url = "~/Post/PostPage?post=" + comment.post_id;
            return RedirectToAction("Index", "Home");
        }