public async Task <PageResult <BookResponseDto> > QueryAsync(BookQueryModel queryModel) { var query = DbSet.AsNoTracking(); if (!string.IsNullOrEmpty(queryModel.Keyword)) { query = query.Where(x => EF.Functions.Like(x.Title, LikeString(queryModel.Keyword))); } if (!string.IsNullOrWhiteSpace(queryModel.Tag)) { query = query.Where(x => x.BookTags.Any(bt => bt.Tag.Name == queryModel.Tag)); } if (queryModel.Status.HasValue) { query = query.Where(x => x.Status == queryModel.Status.Value); } var result = new PageResult <BookResponseDto>(); if (queryModel.EnableTotal) { result.Total = await query.CountAsync(); } query = query.Include(x => x.Links) .Include(x => x.BookTags).ThenInclude(x => x.Tag) .OrderByDescending(x => x.Id); var list = await PageAsync(query, queryModel); result.List = Mapper.Map <List <BookResponseDto> >(list); return(result); }
public Task <PageResult <BookResponseDto> > Get([FromQuery] BookQueryModel queryModel) { // 限制一页请求大小 if (queryModel.Size > 50) { throw new BadRequestException("一页最多请求50本书!"); } return(_bookRepository.QueryAsync(queryModel)); }