add controller

This commit is contained in:
liuyi 2025-05-21 22:47:16 +08:00
parent 619a59e33e
commit 552cbf2e57
4 changed files with 82 additions and 38 deletions

View File

@ -10,3 +10,11 @@ export enum PostOrder {
COMMENTCOUNT = 'commentCount', COMMENTCOUNT = 'commentCount',
CUSTOM = 'custom', CUSTOM = 'custom',
} }
export const DEFAULT_VALIDATION_CONFIG = Object.freeze({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
});

View File

@ -15,6 +15,7 @@ import {
import { AppInterceptor } from '@/modules/core/providers/app.interceptor'; import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
import { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '../dtos/category.dto'; import { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '../dtos/category.dto';
import { CategoryService } from '../services'; import { CategoryService } from '../services';
@ -32,15 +33,7 @@ export class CategoryController {
@Get() @Get()
@SerializeOptions({ groups: ['category-list'] }) @SerializeOptions({ groups: ['category-list'] })
async list( async list(
@Query( @Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
}),
)
options: QueryCategoryDto, options: QueryCategoryDto,
) { ) {
return this.service.paginate(options); return this.service.paginate(options);
@ -57,11 +50,7 @@ export class CategoryController {
async store( async store(
@Body( @Body(
new ValidationPipe({ new ValidationPipe({
transform: true, ...DEFAULT_VALIDATION_CONFIG,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['create'], groups: ['create'],
}), }),
) )
@ -75,11 +64,7 @@ export class CategoryController {
async update( async update(
@Body( @Body(
new ValidationPipe({ new ValidationPipe({
transform: true, ...DEFAULT_VALIDATION_CONFIG,
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
validationError: { target: false },
groups: ['update'], groups: ['update'],
}), }),
) )

View File

@ -17,6 +17,8 @@ import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dt
import { PostService } from '@/modules/content/services/post.service'; import { PostService } from '@/modules/content/services/post.service';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor'; import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
@UseInterceptors(AppInterceptor) @UseInterceptors(AppInterceptor)
@Controller('posts') @Controller('posts')
export class PostController { export class PostController {
@ -25,15 +27,7 @@ export class PostController {
@Get() @Get()
@SerializeOptions({ groups: ['post-list'] }) @SerializeOptions({ groups: ['post-list'] })
async list( async list(
@Query( @Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
new ValidationPipe({
transform: true,
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
validationError: { target: false },
}),
)
options: QueryPostDto, options: QueryPostDto,
) { ) {
return this.postService.paginate(options); return this.postService.paginate(options);
@ -50,11 +44,7 @@ export class PostController {
async store( async store(
@Body( @Body(
new ValidationPipe({ new ValidationPipe({
transform: true, ...DEFAULT_VALIDATION_CONFIG,
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
validationError: { target: false },
groups: ['create'], groups: ['create'],
}), }),
) )
@ -68,11 +58,7 @@ export class PostController {
async update( async update(
@Body( @Body(
new ValidationPipe({ new ValidationPipe({
transform: true, ...DEFAULT_VALIDATION_CONFIG,
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
validationError: { target: false },
groups: ['update'], groups: ['update'],
}), }),
) )

View File

@ -0,0 +1,65 @@
import {
Body,
Controller,
Delete,
Get,
Param,
ParseUUIDPipe,
Patch,
Post,
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
import { CreateTagDto, QueryTagDto, UpdateTagDto } from '../dtos/tag.dto';
import { TagService } from '../services';
@Controller('tag')
@UseInterceptors(AppInterceptor)
export class TagController {
constructor(protected service: TagService) {}
@Get()
@SerializeOptions({})
async list(
@Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
options: QueryTagDto,
) {
return this.service.paginate(options);
}
@Get(':id')
@SerializeOptions({})
async detail(@Param('id', new ParseUUIDPipe()) id: string) {
return this.service.detail(id);
}
@Post()
@SerializeOptions({})
async store(
@Body(new ValidationPipe({ ...DEFAULT_VALIDATION_CONFIG, groups: ['create'] }))
data: CreateTagDto,
) {
return this.service.create(data);
}
@Patch()
@SerializeOptions({})
async update(
@Body(new ValidationPipe({ ...DEFAULT_VALIDATION_CONFIG, groups: ['update'] }))
date: UpdateTagDto,
) {
return this.service.update(date);
}
@Delete(':id')
@SerializeOptions({})
async delete(@Param('id', new ParseUUIDPipe()) id: string) {
return this.service.delete(id);
}
}