From b25a85bf06aa894a783ceaf76584fa64ec3cf0f7 Mon Sep 17 00:00:00 2001 From: liuyi Date: Wed, 21 May 2025 22:29:18 +0800 Subject: [PATCH] add controller --- .../controllers/category.controller.ts | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/modules/content/controllers/category.controller.ts diff --git a/src/modules/content/controllers/category.controller.ts b/src/modules/content/controllers/category.controller.ts new file mode 100644 index 0000000..2ebdd68 --- /dev/null +++ b/src/modules/content/controllers/category.controller.ts @@ -0,0 +1,96 @@ +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 { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '../dtos/category.dto'; +import { CategoryService } from '../services'; + +@UseInterceptors(AppInterceptor) +@Controller('category') +export class CategoryController { + constructor(protected service: CategoryService) {} + + @Get('tree') + @SerializeOptions({ groups: ['category-tree'] }) + async tree() { + return this.service.findTrees(); + } + + @Get() + @SerializeOptions({ groups: ['category-list'] }) + async list( + @Query( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidNonWhitelisted: true, + forbidUnknownValues: true, + validationError: { target: false }, + }), + ) + options: QueryCategoryDto, + ) { + return this.service.paginate(options); + } + + @Get(':id') + @SerializeOptions({ groups: ['category-detail'] }) + async detail(@Param('id', new ParseUUIDPipe()) id: string) { + return this.service.detail(id); + } + + @Post() + @SerializeOptions({ groups: ['category-detail'] }) + async store( + @Body( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidNonWhitelisted: true, + forbidUnknownValues: true, + validationError: { target: false }, + groups: ['create'], + }), + ) + data: CreateCategoryDto, + ) { + return this.service.create(data); + } + + @Patch() + @SerializeOptions({ groups: ['category-detail'] }) + async update( + @Body( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidNonWhitelisted: true, + forbidUnknownValues: true, + validationError: { target: false }, + groups: ['update'], + }), + ) + data: UpdateCategoryDto, + ) { + return this.service.update(data); + } + + @Delete(':id') + @SerializeOptions({ groups: ['category-detail'] }) + async delete(@Param('id', new ParseUUIDPipe()) id: string) { + return this.service.delete(id); + } +}