Exemplo n.º 1
0
 public Task <ServiceResult <PagedDto <CommentDto> > > GetListByArticleAsync([FromQuery] CommentPagingDto pagingDto)
 {
     return(_commentSvc.GetsByArticleAsync(pagingDto));
 }
Exemplo n.º 2
0
        public async Task <ServiceResult <PagedDto <CommentDto> > > GetsByArticleAsync(CommentPagingDto pagingDto)
        {
            List <CommentDto> comments = (await _commentRepo
                                          .Select
                                          .Include(r => r.UserInfo)
                                          .Include(r => r.RespUserInfo)
                                          .IncludeMany(r => r.Childs, t => t.Include(u => u.UserInfo).Include(u => u.RespUserInfo))
                                          .WhereCascade(x => x.IsDeleted == false)
                                          .WhereIf(pagingDto.ArticleId.HasValue, r => r.ArticleId == pagingDto.ArticleId)
                                          .Where(r => r.RootCommentId == pagingDto.RootCommentId) // && r.IsAudit == true
                                          .OrderByDescending(!pagingDto.RootCommentId.HasValue, r => r.CreateTime)
                                          .OrderBy(pagingDto.RootCommentId.HasValue, r => r.CreateTime)
                                          .Page(pagingDto.Page, pagingDto.Size).ToListAsync())
                                         .Select(r =>
            {
                CommentDto commentDto = Mapper.Map <CommentDto>(r);

                if (commentDto.UserInfo == null)
                {
                    commentDto.UserInfo = new UserDto {
                        Username = "******"
                    };
                }
                else
                {
                    commentDto.UserInfo = Mapper.Map <UserDto>(r.UserInfo);
                }


                commentDto.Childs = r.Childs.Select(u =>
                {
                    CommentDto childrenDto = Mapper.Map <CommentDto>(u);
                    return(childrenDto);
                }).ToList();
                return(commentDto);
            }).ToList();

            //计算一个文章多少个评论
            long totalCount = GetCommentCount(pagingDto);

            return(ServiceResult <PagedDto <CommentDto> > .Successed(new PagedDto <CommentDto>(comments, totalCount)));

            long GetCommentCount(CommentPagingDto page)
            {
                return(_commentRepo
                       .Select
                       .Where(r => r.IsDeleted == false && r.ArticleId == page.ArticleId).Count());
            }
        }