Skip to content

Commit 8537b05

Browse files
committed
feat: page sort support
1 parent b7b3271 commit 8537b05

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/modules/page/page.controller.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Paginator } from '~/common/decorator/http.decorator'
1515
import { ApiName } from '~/common/decorator/openapi.decorator'
1616
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
1717
import { MongoIdDto } from '~/shared/dto/id.dto'
18-
import { PagerDto } from '~/shared/dto/pager.dto'
18+
import { PageQueryDto } from './page.dto'
1919
import { PageModel, PartialPageModel } from './page.model'
2020
import { PageService } from './page.service'
2121

@@ -26,15 +26,16 @@ export class PageController {
2626

2727
@Get('/')
2828
@Paginator
29-
async getPagesSummary(@Query() query: PagerDto) {
30-
const { size, select, page } = query
29+
async getPagesSummary(@Query() query: PageQueryDto) {
30+
const { size, select, page, sortBy, sortOrder } = query
3131

3232
return await this.pageService.model.paginate(
3333
{},
3434
{
3535
limit: size,
3636
page,
3737
select,
38+
sort: sortBy ? { [sortBy]: sortOrder || -1 } : { modified: -1 },
3839
},
3940
)
4041
}

src/modules/page/page.dto.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Transform } from 'class-transformer'
2+
import { IsEnum, IsOptional, ValidateIf } from 'class-validator'
3+
import { PagerDto } from '~/shared/dto/pager.dto'
4+
5+
export class PageQueryDto extends PagerDto {
6+
@IsOptional()
7+
@IsEnum(['title', 'created', 'modified', 'order', 'subtitle'])
8+
readonly sortBy?: string
9+
10+
@IsOptional()
11+
@IsEnum([1, -1])
12+
@ValidateIf((o) => o.sortBy)
13+
@Transform(({ value: v }) => v | 0)
14+
readonly sortOrder?: 1 | -1
15+
}

0 commit comments

Comments
 (0)