fix: pager dto and sort query
This commit is contained in:
14
package.json
14
package.json
@@ -15,8 +15,8 @@
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.{js,jsx,ts,tsx}": [
|
||||
"prettier --ignore-path ./.prettierignore --write ",
|
||||
"eslint --cache"
|
||||
"eslint --cache --fix",
|
||||
"prettier --ignore-path ./.prettierignore --write "
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
@@ -52,16 +52,6 @@
|
||||
"docs": "npx @compodoc/compodoc -p tsconfig.json -s -d docs",
|
||||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
|
||||
},
|
||||
"pnpm": {
|
||||
"peerDependencyRules": {
|
||||
"allowedVersions": {
|
||||
"class-transformer": "0.5.1"
|
||||
},
|
||||
"ignoreMissing": [
|
||||
"webpack"
|
||||
]
|
||||
}
|
||||
},
|
||||
"bump": {
|
||||
"before": [
|
||||
"git pull --rebase",
|
||||
|
||||
@@ -25,12 +25,12 @@ import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { CountingService } from '~/processors/helper/helper.counting.service'
|
||||
import { TextMacroService } from '~/processors/helper/helper.macro.service'
|
||||
import { IntIdOrMongoIdDto, MongoIdDto } from '~/shared/dto/id.dto'
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
import {
|
||||
addHidePasswordAndHideCondition,
|
||||
addYearCondition,
|
||||
} from '~/transformers/db-query.transformer'
|
||||
|
||||
import { PageQueryDto } from '../page/page.dto'
|
||||
import {
|
||||
ListQueryDto,
|
||||
NidType,
|
||||
@@ -279,11 +279,17 @@ export class NoteController {
|
||||
@HTTPDecorators.Paginator
|
||||
async getNotesByTopic(
|
||||
@Param() params: MongoIdDto,
|
||||
@Query() query: PageQueryDto,
|
||||
@Query() query: PagerDto,
|
||||
@IsMaster() isMaster: boolean,
|
||||
) {
|
||||
const { id } = params
|
||||
const { size, page, select = '_id title nid id created modified' } = query
|
||||
const {
|
||||
size,
|
||||
page,
|
||||
select = '_id title nid id created modified',
|
||||
sortBy,
|
||||
sortOrder,
|
||||
} = query
|
||||
const condition: FilterQuery<NoteModel> = isMaster
|
||||
? { $or: [{ hide: false }, { hide: true }] }
|
||||
: { hide: false }
|
||||
@@ -294,6 +300,7 @@ export class NoteController {
|
||||
page,
|
||||
limit: size,
|
||||
select,
|
||||
sort: sortBy ? { [sortBy]: sortOrder } : undefined,
|
||||
},
|
||||
{ ...condition },
|
||||
)
|
||||
|
||||
@@ -18,8 +18,8 @@ import { ApiName } from '~/common/decorator/openapi.decorator'
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { TextMacroService } from '~/processors/helper/helper.macro.service'
|
||||
import { MongoIdDto } from '~/shared/dto/id.dto'
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
|
||||
import { PageQueryDto } from './page.dto'
|
||||
import { PageModel, PartialPageModel } from './page.model'
|
||||
import { PageService } from './page.service'
|
||||
|
||||
@@ -33,7 +33,7 @@ export class PageController {
|
||||
|
||||
@Get('/')
|
||||
@Paginator
|
||||
async getPagesSummary(@Query() query: PageQueryDto) {
|
||||
async getPagesSummary(@Query() query: PagerDto) {
|
||||
const { size, select, page, sortBy, sortOrder } = query
|
||||
|
||||
return await this.pageService.model.paginate(
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { Transform } from 'class-transformer'
|
||||
import { IsEnum, IsOptional, ValidateIf } from 'class-validator'
|
||||
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
|
||||
export class PageQueryDto extends PagerDto {
|
||||
@IsOptional()
|
||||
@IsEnum(['title', 'created', 'modified', 'order', 'subtitle'])
|
||||
readonly sortBy?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum([1, -1])
|
||||
@ValidateIf((o) => o.sortBy)
|
||||
@Transform(({ value: v }) => v | 0)
|
||||
readonly sortOrder?: 1 | -1
|
||||
}
|
||||
@@ -24,10 +24,11 @@ import { VisitDocument } from '~/common/decorator/update-count.decorator'
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { CountingService } from '~/processors/helper/helper.counting.service'
|
||||
import { MongoIdDto } from '~/shared/dto/id.dto'
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
import { addYearCondition } from '~/transformers/db-query.transformer'
|
||||
|
||||
import { CategoryModel } from '../category/category.model'
|
||||
import { CategoryAndSlugDto, PostQueryDto } from './post.dto'
|
||||
import { CategoryAndSlugDto } from './post.dto'
|
||||
import { PartialPostModel, PostModel } from './post.model'
|
||||
import { PostService } from './post.service'
|
||||
|
||||
@@ -41,7 +42,7 @@ export class PostController {
|
||||
|
||||
@Get('/')
|
||||
@Paginator
|
||||
async getPaginate(@Query() query: PostQueryDto) {
|
||||
async getPaginate(@Query() query: PagerDto) {
|
||||
const { size, select, page, year, sortBy, sortOrder } = query
|
||||
|
||||
return await this.postService.findWithPaginator(
|
||||
|
||||
@@ -1,20 +1,5 @@
|
||||
import { Transform } from 'class-transformer'
|
||||
import { IsEnum, IsOptional, IsString, ValidateIf } from 'class-validator'
|
||||
|
||||
import { PagerDto } from '~/shared/dto/pager.dto'
|
||||
|
||||
export class PostQueryDto extends PagerDto {
|
||||
@IsOptional()
|
||||
@IsEnum(['categoryId', 'title', 'created', 'modified'])
|
||||
@Transform(({ value: v }) => (v === 'category' ? 'categoryId' : v))
|
||||
readonly sortBy?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum([1, -1])
|
||||
@ValidateIf((o) => o.sortBy)
|
||||
@Transform(({ value: v }) => v | 0)
|
||||
readonly sortOrder?: 1 | -1
|
||||
}
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CategoryAndSlugDto {
|
||||
@IsString()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Expose, Transform } from 'class-transformer'
|
||||
import {
|
||||
IsEnum,
|
||||
IsInt,
|
||||
IsMongoId,
|
||||
IsNotEmpty,
|
||||
@@ -42,6 +43,27 @@ export class PagerDto extends DbQueryDto {
|
||||
@ApiProperty({ required: false })
|
||||
select?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sortBy?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum([1, -1])
|
||||
@Transform(({ value: val }) => {
|
||||
// @ts-ignore
|
||||
const isStringNumber = typeof val === 'string' && !isNaN(val)
|
||||
|
||||
if (isStringNumber) {
|
||||
return parseInt(val)
|
||||
} else {
|
||||
return {
|
||||
asc: 1,
|
||||
desc: -1,
|
||||
}[val.toString()]
|
||||
}
|
||||
})
|
||||
sortOrder?: 1 | -1
|
||||
|
||||
@IsOptional()
|
||||
@Transform(({ value: val }) => parseInt(val))
|
||||
@Min(1)
|
||||
|
||||
@@ -69,12 +69,12 @@ export function BaseCrudFactory<
|
||||
@Get('/')
|
||||
@Paginator
|
||||
async gets(@Query() pager: PagerDto) {
|
||||
const { size, page, select, state } = pager
|
||||
const { size, page, select, state, sortBy, sortOrder } = pager
|
||||
// @ts-ignore
|
||||
return await this._model.paginate(state !== undefined ? { state } : {}, {
|
||||
limit: size,
|
||||
page,
|
||||
sort: { created: -1 },
|
||||
sort: sortBy ? { [sortBy]: sortOrder } : { created: -1 },
|
||||
select,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user