Exemple #1
0
        public async System.Threading.Tasks.Task <PartialViewResult> Post(PostViewModel data)
        {
            Post newPost = new Post();

            newPost.Post_Status = 0;
            _viewModel          = new PostViewModel();
            FieldHelper.CopyNotNullValue(newPost, data);
            newPost.CreatedDate = DateTime.Now;
            if (data.Option == 0 && newPost.Id_Type == 8)
            {
                //post to fb toiec
                var IdPost = await _fbService.PostingToGroupFB(newPost.Content);

                newPost.Id_PostFB = IdPost.id;
            }
            _postService.Add(newPost);
            _postService.Save();
            // string url = MaHoaMD5.Encrypt(newPost.Id + "#" + newPost.UpdatedDate);
            //Send notify
            ApplicationUser userPost = _service.GetUserById(newPost.Id_User);

            FieldHelper.CopyNotNullValue(PostViewModel, userPost);
            FieldHelper.CopyNotNullValue(PostViewModel, newPost);
            PostViewModel.TypeToString = _postTypeService.GetById(newPost.Id_Type).Name;
            PostVoteDetail vote = _postVoteDetailService.getVoteByIdUser(newPost.Id_User, newPost.Id);

            if (vote != null)
            {
                PostViewModel.Vote = vote.Vote;
            }
            //Send noti for all user register
            var userIDList = getUserIDListBasedOnType(newPost.Id_Type);

            foreach (var userID in userIDList)
            {
                var sendPostToUser = _service.GetUserById(userID);
                if (sendPostToUser.Id_Messenger != userPost.Id_Messenger && sendPostToUser.Id_Messenger != null && _eventService.IsFreeTime(sendPostToUser.Email))
                {
                    //Create json send............
                    FBPostNoti newNoti = new FBPostNoti();
                    newNoti.recipient.id = sendPostToUser.Id_Messenger;
                    newNoti.message.attachment.payload.text = "```\r\n" + "💬 Bạn có một câu hỏi: " + "\r\n" + '"' + newPost.Content + '"' + "\r\n```";
                    NotiButton button = new NotiButton();
                    button.payload = "REPLAY_" + newPost.Id;
                    newNoti.message.attachment.payload.buttons.Add(button);
                    //
                    ChatBotMessenger.sendRequest(JsonConvert.SerializeObject(newNoti));
                }
            }
            return(PartialView("_Post", PostViewModel));
        }
Exemple #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));
        }
Exemple #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",
             }));
         }
     }
 }
Exemple #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));
        }
Exemple #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));
        }