diff --git a/package.json b/package.json index dafddf4..280217a 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@nestjs/common": "^10.0.3", "@nestjs/core": "^10.0.3", "@nestjs/platform-fastify": "^10.0.3", - "@nestjs/swagger": "^7.0.4", + "@nestjs/swagger": "^7.4.2", "@nestjs/typeorm": "^11.0.0", "better-sqlite3": "^11.10.0", "class-transformer": "^0.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 041512c..b0aef06 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18,7 +18,7 @@ importers: specifier: ^10.0.3 version: 10.4.17(@nestjs/common@10.4.17(class-transformer@0.5.1)(class-validator@0.14.2)(reflect-metadata@0.1.14)(rxjs@7.8.2))(@nestjs/core@10.4.17) '@nestjs/swagger': - specifier: ^7.0.4 + specifier: ^7.4.2 version: 7.4.2(@nestjs/common@10.4.17(class-transformer@0.5.1)(class-validator@0.14.2)(reflect-metadata@0.1.14)(rxjs@7.8.2))(@nestjs/core@10.4.17)(class-transformer@0.5.1)(class-validator@0.14.2)(reflect-metadata@0.1.14) '@nestjs/typeorm': specifier: ^11.0.0 diff --git a/src/modules/content/dtos/post.dto.ts b/src/modules/content/dtos/post.dto.ts new file mode 100644 index 0000000..5e27ea5 --- /dev/null +++ b/src/modules/content/dtos/post.dto.ts @@ -0,0 +1,94 @@ +import { PartialType } from '@nestjs/swagger'; +import { Transform } from 'class-transformer'; + +import { + IsBoolean, + IsDefined, + IsEnum, + IsNotEmpty, + IsNumber, + IsOptional, + IsUUID, + MaxLength, + Min, + ValidateIf, +} from 'class-validator'; + +import { isNil, toNumber } from 'lodash'; + +import { PostOrder } from '@/modules/content/constants'; +import { toBoolean } from '@/modules/core/helpers'; +import { PaginateOptions } from '@/modules/database/types'; + +export class QueryPostDto implements PaginateOptions { + @Transform(({ value }) => toBoolean(value)) + @IsBoolean() + @IsOptional() + isPublished?: boolean; + + @IsEnum(PostOrder, { message: `` }) + @IsOptional() + orderBy: PostOrder; + + @Transform(({ value }) => toNumber(value)) + @Min(1, { message: '' }) + @IsNumber() + @IsOptional() + page = 1; + + @Transform(({ value }) => toNumber(value)) + @Min(1, { message: '' }) + @IsNumber() + @IsOptional() + limit = 10; +} + +export class CreatePostDto { + @MaxLength(255, { + always: true, + message: 'The maximum length of the article title is $constraint1', + }) + @IsNotEmpty({ groups: ['create'], message: 'The article title must be filled in.' }) + @IsOptional({ groups: ['update'] }) + title: string; + + @IsNotEmpty({ groups: ['create'], message: 'The content of the article must be filled in.' }) + @IsOptional({ groups: ['update'] }) + body: string; + + @MaxLength(500, { + always: true, + message: 'The maximum length of the article description is $constraint1', + }) + @IsOptional({ always: true }) + summary?: string; + + @Transform(({ value }) => toBoolean(value)) + @IsBoolean({ always: true }) + @ValidateIf((value) => !isNil(value.publish)) + @IsOptional({ always: true }) + publish?: boolean; + + @MaxLength(20, { + always: true, + each: true, + message: 'The maximum length of each keyword is $constraint1', + }) + @IsOptional({ always: true }) + keywords?: string[]; + + @Transform(({ value }) => toNumber(value)) + @Min(0, { message: 'The sorted value must be greater than 0.' }) + @IsNumber(undefined, { always: true }) + @IsOptional({ always: true }) + customOrder?: number; +} + +export class UpdatePostDto extends PartialType(CreatePostDto) { + @IsUUID(undefined, { + groups: ['update'], + message: 'The format of the article ID is incorrect.', + }) + @IsDefined({ groups: ['update'], message: 'The article ID must be specified' }) + id: string; +}