Exemplo n.º 1
0
        public async Task <PostsWithCommentsAndVotesModel> GetPostWithCommentsAndVotes(int postPage, int commentPage)
        {
            var posts = await _repository.GetAllPosts((postPage - 1) * 20, 20);

            var totalPosts = await _repository.AllPostCount();

            var postIds  = posts.Select(s => s.Id).ToArray();
            var comments = await _commentService.GetAllComments(postIds, (commentPage - 1) * 5, 5);

            var votes = await _voteService.GetVote(comments.Select(s => s.Id).ToArray());

            return(new PostsWithCommentsAndVotesModel
            {
                TotalPost = totalPosts,
                Posts = posts.Select(s => new Post
                {
                    PostData = s.Post,
                    PostedDate = s.InsertedOn,
                    TotalComment = _commentService.GetCommentCount(s.Id).Result,
                    PostId = s.Id,
                    UserName = s.InsertedBy.ToString(),
                    Comments = comments.Where(w => w.PostId == s.Id).Select(c => new Comment
                    {
                        CommentData = c.Comment,
                        CommentId = c.Id,
                        PostedDate = c.InsertedOn,
                        UpVotes = votes.Where(w => w.IsUpVoted && w.CommentId == c.Id).ToArray().Count(),
                        DownVotes = votes.Where(w => !w.IsUpVoted && w.CommentId == c.Id).ToArray().Count(),
                        UserName = c.InsertedBy.ToString()
                    }).ToList()
                }).ToList()
            });
        }