Exemple #1
0
 public async Task <CommonPageResultDto <BlogListViewDto> > BlogList([FromBody] ConditionParamDto condition, int pageIndex, int pageSize)
 {
     return(await _blogService.BlogList(condition, pageIndex, pageSize));
 }
Exemple #2
0
        /// <summary>
        /// 查询博客列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task <CommonPageResultDto <BlogListViewDto> > BlogList(ConditionParamDto condition, int pageIndex, int pageSize)
        {
            CommonPageResultDto <BlogListViewDto> rlt = new CommonPageResultDto <BlogListViewDto>();
            List <tbl_blog> list = new List <tbl_blog>();

            //if (!String.IsNullOrEmpty(condition.Keyword))
            //{
            //    list = await (from a in _context.tbl_blog.Where(i => i.Title.Contains(condition.Keyword))
            //                 join b in _context.tbl_blog_content.Where(i => i.Content.Contains(condition.Keyword))
            //                 on a.ContentId equals b.Id
            //                 select a).ToListAsync();
            //}
            if (!String.IsNullOrEmpty(condition.CategoryId))
            {
                list = _context.tbl_blog.Where(i => i.CategoryId == condition.CategoryId && i.DeleteAt == null).ToList();
            }
            else if (!String.IsNullOrEmpty(condition.TagId))
            {
                list = (from a in _context.tbl_blog.Where(i => i.DeleteAt == null)
                        join b in _context.tbl_blog_tag_relation.Where(i => i.TagId == condition.TagId)
                        on a.Id equals b.BlogId
                        select a).ToList();
            }
            else
            {
                list = await _context.tbl_blog.Where(i => i.DeleteAt == null).ToListAsync();
            }

            List <BlogListViewDto> commentGroup = await(from a in _context.tbl_comment
                                                        group a by a.BlogId into g
                                                        select new BlogListViewDto
            {
                Id           = g.Key,
                CommentTimes = g.Count()
            }).ToListAsync();

            List <BlogListViewDto> qry = (from a in list
                                          join b in _context.tbl_blog_content
                                          on a.ContentId equals b.Id
                                          join c in _context.tbl_category
                                          on a.CategoryId equals c.Id
                                          join d in commentGroup
                                          on a.Id equals d.Id into temp
                                          from g in temp.DefaultIfEmpty()
                                          select new BlogListViewDto
            {
                Id = a.Id,
                Title = a.Title,
                Content = b.Content,
                CreateAt = a.CreateAt,
                ViewTimes = a.ViewTimes,
                LikeTimes = a.LikeTimes,
                PicUrl = a.PicUrl,
                CommentTimes = g == null ? 0 : g.CommentTimes                             // _context.tbl_comment.Count(i=>i.BlogId == a.Id)
            }).ToList();

            IQueryable <TagViewDto> tagRlt = (from a in _context.tbl_blog_tag_relation
                                              join b in _context.tbl_tag
                                              on a.TagId equals b.Id
                                              select new TagViewDto {
                BlogId = a.BlogId, TagName = b.Name, TagId = b.Id, Sequence = b.Sequence
            });

            qry = String.IsNullOrEmpty(condition.Keyword) ? qry : qry.Where(i => i.Title.Contains(condition.Keyword) || i.Content.Contains(condition.Keyword)).ToList();
            foreach (var item in qry)
            {
                item.TagList = await tagRlt.Where(i => i.BlogId == item.Id).Select(i => i).ToListAsync();
            }

            rlt.DataCount = qry.Count;
            rlt.Page      = pageIndex;
            rlt.PageSize  = pageSize;
            rlt.PageCount = (qry.Count + pageSize - 1) / pageSize;
            rlt.Data      = qry.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return(rlt);
        }