Beispiel #1
0
        public PartialViewResult CommentSection(int id_comment)
        {
            Comment        comment          = new Comment();
            List <Comment> listChildComment = new List <Comment>();

            comment = _commentOfPost.GetById(id_comment);
            Post             post             = _postService.GetById(comment.Id_Post);
            CommentViewModel commentViewModel = new CommentViewModel();

            commentViewModel.Id_UserPost = post.Id_User;
            ApplicationUser   user        = _service.GetUserById(comment.Id_User);
            CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(User.Identity.GetUserId(), comment.Id);

            if (voteComment != null)
            {
                commentViewModel.Vote = voteComment.Vote;
            }
            FieldHelper.CopyNotNullValue(commentViewModel, user);
            FieldHelper.CopyNotNullValue(commentViewModel, comment);
            listChildComment = _commentOfPost.getChildOfComment(comment.Id_Post, comment.Id);
            if (listChildComment.Count != 0)
            {
                foreach (var child in listChildComment)
                {
                    CommentViewModel commentChildViewModel = new CommentViewModel();
                    user = _service.GetUserById(child.Id_User);
                    FieldHelper.CopyNotNullValue(commentChildViewModel, user);
                    FieldHelper.CopyNotNullValue(commentChildViewModel, child);
                    commentViewModel.listChildComment.Add(commentChildViewModel);
                }
            }
            return(PartialView("_Comment", commentViewModel));
        }
Beispiel #2
0
        public ActionResult Index(int id, int?Comment)
        {
            string id_user = User.Identity.GetUserId();

            _viewModel = new PostViewModel();
            CreateLayoutView("Trả lời câu hỏi");
            Post            post = _postService.GetById(id);
            ApplicationUser user = _service.GetUserById(post.Id_User);

            FieldHelper.CopyNotNullValue(PostViewModel, user);
            FieldHelper.CopyNotNullValue(PostViewModel, post);
            PostViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name;
            PostVoteDetail   votePost         = _postVoteDetailService.getVoteByIdUser(id_user, post.Id);
            CommentViewModel commentCorrected = null;

            if (votePost != null)
            {
                PostViewModel.Vote = votePost.Vote;
            }
            List <Comment> listComment      = new List <Comment>();
            List <Comment> listChildComment = new List <Comment>();

            listComment = _commentOfPost.getCommentOfPost(post.Id);
            foreach (var parent in listComment)
            {
                CommentViewModel commentViewModel = new CommentViewModel();
                user = _service.GetUserById(parent.Id_User);
                CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(id_user, parent.Id);
                if (voteComment != null)
                {
                    commentViewModel.Vote = voteComment.Vote;
                }
                FieldHelper.CopyNotNullValue(commentViewModel, user);
                FieldHelper.CopyNotNullValue(commentViewModel, parent);
                listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id);
                if (listChildComment.Count != 0)
                {
                    foreach (var child in listChildComment)
                    {
                        CommentViewModel commentChildViewModel = new CommentViewModel();
                        user = _service.GetUserById(child.Id_User);
                        FieldHelper.CopyNotNullValue(commentChildViewModel, user);
                        FieldHelper.CopyNotNullValue(commentChildViewModel, child);
                        commentViewModel.listChildComment.Add(commentChildViewModel);
                    }
                }
                if (Comment != null && commentViewModel.Id.Equals(Comment))
                {
                    PostViewModel.listComment.Insert(0, commentViewModel);
                }
                else
                {
                    if (commentViewModel.Corrected)
                    {
                        commentCorrected = commentViewModel;
                    }
                    else
                    {
                        PostViewModel.listComment.Add(commentViewModel);
                    }
                }
            }
            if (commentCorrected != null)
            {
                PostViewModel.listComment.Insert(0, commentCorrected);
            }
            List <PostType>          listPostType          = _postTypeService.GetAll().ToList();
            List <PostTypeViewModel> listPostTypeViewModel = new List <PostTypeViewModel>();

            foreach (PostType item in listPostType)
            {
                PostTypeViewModel postType = new PostTypeViewModel();
                FieldHelper.CopyNotNullValue(postType, item);
                postType.Register = _detailUserTypeService.getRegisterPostType(id_user, item.Id);
                listPostTypeViewModel.Add(postType);
            }
            ViewBag.ListPostType = listPostTypeViewModel;
            return(View(PostViewModel));
        }
Beispiel #3
0
 public JsonResult VotePost(VoteViewModel data)
 {
     if (data.Id_Comment == 0)
     {
         Post           post = _postService.GetById(data.Id_Post);
         PostVoteDetail vote;
         vote = _postVoteDetailService.getVoteByIdUser(data.Id_User, data.Id_Post);
         if (vote != null)
         {
             if (data.Vote == vote.Vote)
             {
                 vote.Vote = 0;
             }
             else
             {
                 vote.Vote = data.Vote;
             }
             post.UpVote   += data.UpVote;
             post.DownVote += data.DownVote;
             if (post.UpVote < 0)
             {
                 post.UpVote = 0;
             }
             if (post.DownVote < 0)
             {
                 post.DownVote = 0;
             }
             _postVoteDetailService.Update(vote);
         }
         else
         {
             vote = new PostVoteDetail();
             FieldHelper.CopyNotNullValue(vote, data);
             _postVoteDetailService.Add(vote);
             if (vote.Vote > 0)
             {
                 post.UpVote++;
             }
             else
             {
                 post.DownVote++;
             }
         }
         _postService.Update(post);
         try
         {
             _postVoteDetailService.Save();
             _postService.Save();
             return(Json(new
             {
                 result = "success"
             }));
         }
         catch (Exception e)
         {
             return(Json(new
             {
                 result = "failed",
             }));
         }
     }
     else
     {
         Comment           comment = _commentOfPost.GetById(data.Id_Comment);
         CommentVoteDetail vote;
         vote = _commentVoteDetailService.getVoteByIdUser(data.Id_User, data.Id_Comment);
         if (vote != null)
         {
             if (data.Vote == vote.Vote)
             {
                 vote.Vote = 0;
             }
             else
             {
                 vote.Vote = data.Vote;
             }
             comment.UpVote   += data.UpVote;
             comment.DownVote += data.DownVote;
             if (comment.UpVote < 0)
             {
                 comment.UpVote = 0;
             }
             if (comment.DownVote < 0)
             {
                 comment.DownVote = 0;
             }
             _commentVoteDetailService.Update(vote);
         }
         else
         {
             vote = new CommentVoteDetail();
             FieldHelper.CopyNotNullValue(vote, data);
             _commentVoteDetailService.Add(vote);
             if (vote.Vote > 0)
             {
                 comment.UpVote++;
             }
             else
             {
                 comment.DownVote++;
             }
         }
         _commentOfPost.Update(comment);
         try
         {
             _commentVoteDetailService.Save();
             _commentOfPost.Save();
             return(Json(new
             {
                 result = "success"
             }));
         }
         catch (Exception e)
         {
             return(Json(new
             {
                 result = "failed",
             }));
         }
     }
 }
Beispiel #4
0
        public PartialViewResult MorePost(int page, int?type)
        {
            MorePostViewModel morePostViewModel = new MorePostViewModel();
            string            id_user           = User.Identity.GetUserId();
            List <Post>       listPost          = new List <Post>();

            if (type == 0)
            {
                listPost = _postService.getPostByUserType(id_user).Skip(page * 5).Take(5).ToList();
            }
            else
            {
                listPost = _postService.getPostByType(type.GetValueOrDefault()).Skip(page * 5).Take(5).ToList();
            }
            foreach (Post post in listPost)
            {
                PostViewModel   postViewModel = new PostViewModel();
                ApplicationUser user          = _service.GetUserById(post.Id_User);
                FieldHelper.CopyNotNullValue(postViewModel, user);
                FieldHelper.CopyNotNullValue(postViewModel, post);

                postViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name;
                PostVoteDetail votePost = _postVoteDetailService.getVoteByIdUser(id_user, post.Id);
                if (votePost != null)
                {
                    postViewModel.Vote = votePost.Vote;
                }
                List <Comment> listComment      = new List <Comment>();
                List <Comment> listChildComment = new List <Comment>();
                listComment = _commentOfPost.getCommentOfPost(post.Id);
                foreach (var parent in listComment)
                {
                    CommentViewModel commentViewModel = new CommentViewModel();
                    user = _service.GetUserById(parent.Id_User);
                    CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(id_user, parent.Id);
                    if (voteComment != null)
                    {
                        commentViewModel.Vote = voteComment.Vote;
                    }
                    FieldHelper.CopyNotNullValue(commentViewModel, user);
                    FieldHelper.CopyNotNullValue(commentViewModel, parent);
                    listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id);
                    if (listChildComment.Count != 0)
                    {
                        foreach (var child in listChildComment)
                        {
                            CommentViewModel commentChildViewModel = new CommentViewModel();
                            user = _service.GetUserById(child.Id_User);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, user);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, child);
                            commentViewModel.listChildComment.Add(commentChildViewModel);
                        }
                    }
                    if (commentViewModel.Corrected)
                    {
                        postViewModel.listComment.Insert(0, commentViewModel);
                    }
                    else
                    {
                        postViewModel.listComment.Add(commentViewModel);
                    }
                }
                morePostViewModel.ListPost.Add(postViewModel);
                ApplicationUser userT = _service.GetUserById(id_user);
                morePostViewModel.UserId   = userT.Id;
                morePostViewModel.Degree   = userT.Degree;
                morePostViewModel.UserName = userT.UserName;
                morePostViewModel.Avatar   = userT.Avatar;
            }
            return(PartialView("_MorePost", morePostViewModel));
        }
Beispiel #5
0
        public ActionResult Index(PostViewModel data, string userType, int?type)
        {
            _viewModel = new HomeViewModel();
            string      id_user  = User.Identity.GetUserId();
            List <Post> listPost = new List <Post>();

            if (type == null)
            {
                CreateLayoutView("Trang chủ");
                listPost = _postService.getPostByUserType(id_user).Take(5).ToList();
            }
            else
            {
                CreateLayoutView("Thể loại: " + _postTypeService.GetById(type.GetValueOrDefault()).Name);
                listPost = _postService.getPostByType(type.GetValueOrDefault()).Take(5).ToList();
            }
            List <PostType> listPostType = _postTypeService.GetAll().ToList();

            foreach (PostType item in listPostType)
            {
                PostTypeViewModel postType = new PostTypeViewModel();
                FieldHelper.CopyNotNullValue(postType, item);
                postType.Register = _detailUserTypeService.getRegisterPostType(id_user, item.Id);
                HomeViewModel.ListPostType.Add(postType);
            }
            ViewBag.newMember = userType;
            ViewBag.type      = type.GetValueOrDefault();

            foreach (Post post in listPost)
            {
                PostViewModel   postViewModel = new PostViewModel();
                ApplicationUser user          = _service.GetUserById(post.Id_User);
                FieldHelper.CopyNotNullValue(postViewModel, user);
                FieldHelper.CopyNotNullValue(postViewModel, post);

                postViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name;
                PostVoteDetail votePost = _postVoteDetailService.getVoteByIdUser(id_user, post.Id);
                if (votePost != null)
                {
                    postViewModel.Vote = votePost.Vote;
                }
                List <Comment> listComment      = new List <Comment>();
                List <Comment> listChildComment = new List <Comment>();
                listComment = _commentOfPost.getCommentOfPost(post.Id);
                foreach (var parent in listComment)
                {
                    CommentViewModel commentViewModel = new CommentViewModel();
                    user = _service.GetUserById(parent.Id_User);
                    CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(id_user, parent.Id);
                    if (voteComment != null)
                    {
                        commentViewModel.Vote = voteComment.Vote;
                    }
                    FieldHelper.CopyNotNullValue(commentViewModel, user);
                    FieldHelper.CopyNotNullValue(commentViewModel, parent);
                    listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id);
                    if (listChildComment.Count != 0)
                    {
                        foreach (var child in listChildComment)
                        {
                            CommentViewModel commentChildViewModel = new CommentViewModel();
                            user = _service.GetUserById(child.Id_User);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, user);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, child);
                            commentViewModel.listChildComment.Add(commentChildViewModel);
                        }
                    }
                    if (commentViewModel.Corrected)
                    {
                        postViewModel.listComment.Insert(0, commentViewModel);
                    }
                    else
                    {
                        postViewModel.listComment.Add(commentViewModel);
                    }
                }
                HomeViewModel.ListPost.Add(postViewModel);
            }
            return(View(HomeViewModel));
        }
        public ActionResult NewsFeed(string username)
        {
            _viewModel = new NewsFeedViewModel();
            CreateLayoutView("Trang cá nhân");
            if (username == "false")
            {
                username = _viewModel.User.UserName;
            }
            ApplicationUser userCur = _service.GetUserByUserName(username);

            FieldHelper.CopyNotNullValue(NewsFeedViewModel, userCur);

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

            listPost = _postService.getPostByUser(userCur.Id).Take(5).ToList();
            NewsFeedViewModel.ListPostType = _postTypeService.GetAll().ToList();

            foreach (Post post in listPost)
            {
                PostViewModel   postViewModel = new PostViewModel();
                ApplicationUser user          = _service.GetUserById(post.Id_User);
                FieldHelper.CopyNotNullValue(postViewModel, user);
                FieldHelper.CopyNotNullValue(postViewModel, post);

                postViewModel.TypeToString = _postTypeService.GetById(post.Id_Type).Name;
                PostVoteDetail votePost = _postVoteDetailService.getVoteByIdUser(NewsFeedViewModel.Id, post.Id);
                if (votePost != null)
                {
                    postViewModel.Vote = votePost.Vote;
                }
                List <Comment> listComment      = new List <Comment>();
                List <Comment> listChildComment = new List <Comment>();
                listComment = _commentOfPost.getCommentOfPost(post.Id);
                foreach (var parent in listComment)
                {
                    CommentViewModel commentViewModel = new CommentViewModel();
                    user = _service.GetUserById(parent.Id_User);
                    CommentVoteDetail voteComment = _commentVoteDetailService.getVoteByIdUser(NewsFeedViewModel.Id, parent.Id);
                    if (voteComment != null)
                    {
                        commentViewModel.Vote = voteComment.Vote;
                    }
                    FieldHelper.CopyNotNullValue(commentViewModel, user);
                    FieldHelper.CopyNotNullValue(commentViewModel, parent);
                    listChildComment = _commentOfPost.getChildOfComment(parent.Id_Post, parent.Id);
                    if (listChildComment.Count != 0)
                    {
                        foreach (var child in listChildComment)
                        {
                            CommentViewModel commentChildViewModel = new CommentViewModel();
                            user = _service.GetUserById(child.Id_User);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, user);
                            FieldHelper.CopyNotNullValue(commentChildViewModel, child);
                            commentViewModel.listChildComment.Add(commentChildViewModel);
                        }
                    }
                    if (commentViewModel.Corrected)
                    {
                        postViewModel.listComment.Insert(0, commentViewModel);
                    }
                    else
                    {
                        postViewModel.listComment.Add(commentViewModel);
                    }
                }
                NewsFeedViewModel.ListPost.Add(postViewModel);
            }
            List <String> listFriend = _friendService.GetAllFriend(userCur.Id);

            foreach (String friend in listFriend)
            {
                FriendViewModel friendViewModel = new FriendViewModel();
                ApplicationUser user            = _service.GetUserById(friend);
                FieldHelper.CopyNotNullValue(friendViewModel, user);
                NewsFeedViewModel.ListFriend.Add(friendViewModel);
            }
            return(PartialView("_NewsFeed", NewsFeedViewModel));
        }