Files
seclusion/plop/templates/api/controller.hbs
charilezhou 473c2c1510 feat: 添加 plop 代码生成器模板
添加组件和模块的代码生成器模板,提高开发效率。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-17 14:08:56 +08:00

131 lines
4.1 KiB
Handlebars

import {
Controller,
Get,
Post,
Patch,
Delete,
Param,
Body,
Query,
UseGuards,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiBearerAuth,
ApiOkResponse,
ApiCreatedResponse,
} from '@nestjs/swagger';
import { Prisma } from '@prisma/client';
import {
Create{{pascalCase name}}Dto,
Update{{pascalCase name}}Dto,
{{pascalCase name}}ResponseDto,
Paginated{{pascalCase name}}ResponseDto,
{{#if hasQueryDto}}
{{pascalCase name}}QueryDto,
{{/if}}
} from './dto/{{kebabCase name}}.dto';
import { {{pascalCase name}}Service } from './{{kebabCase name}}.service';
import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard';
{{#unless hasQueryDto}}
import { PaginationQueryDto } from '@/common/crud';
{{/unless}}
@ApiTags('{{chineseName}}')
@Controller('{{kebabCase pluralName}}')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class {{pascalCase name}}Controller {
constructor(private readonly {{camelCase name}}Service: {{pascalCase name}}Service) {}
@Post()
@ApiOperation({ summary: '创建{{chineseName}}' })
@ApiCreatedResponse({ type: {{pascalCase name}}ResponseDto, description: '创建成功' })
create(@Body() dto: Create{{pascalCase name}}Dto) {
return this.{{camelCase name}}Service.create(dto);
}
@Get()
@ApiOperation({ summary: '获取所有{{chineseName}}(分页)' })
@ApiOkResponse({ type: Paginated{{pascalCase name}}ResponseDto, description: '{{chineseName}}列表' })
{{#if hasQueryDto}}
findAll(@Query() query: {{pascalCase name}}QueryDto) {
const { {{#each queryFields}}{{name}}, {{/each}}...pagination } = query;
const where: Prisma.{{pascalCase name}}WhereInput = {};
{{#each queryFields}}
if ({{name}}) {
{{#if (eq type 'string')}}
where.{{name}} = { contains: {{name}}, mode: 'insensitive' };
{{else}}
where.{{name}} = {{name}};
{{/if}}
}
{{/each}}
return this.{{camelCase name}}Service.findAll({ ...pagination, where });
}
{{else}}
findAll(@Query() query: PaginationQueryDto) {
return this.{{camelCase name}}Service.findAll(query);
}
{{/if}}
{{#if softDelete}}
@Get('deleted')
@ApiOperation({ summary: '获取已删除的{{chineseName}}列表(分页)' })
@ApiOkResponse({ type: Paginated{{pascalCase name}}ResponseDto, description: '已删除{{chineseName}}列表' })
{{#if hasQueryDto}}
findDeleted(@Query() query: {{pascalCase name}}QueryDto) {
const { {{#each queryFields}}{{name}}, {{/each}}...pagination } = query;
const where: Prisma.{{pascalCase name}}WhereInput = {};
{{#each queryFields}}
if ({{name}}) {
{{#if (eq type 'string')}}
where.{{name}} = { contains: {{name}}, mode: 'insensitive' };
{{else}}
where.{{name}} = {{name}};
{{/if}}
}
{{/each}}
return this.{{camelCase name}}Service.findDeleted({ ...pagination, where });
}
{{else}}
findDeleted(@Query() query: PaginationQueryDto) {
return this.{{camelCase name}}Service.findDeleted(query);
}
{{/if}}
{{/if}}
@Get(':id')
@ApiOperation({ summary: '根据 ID 获取{{chineseName}}' })
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '{{chineseName}}详情' })
findById(@Param('id') id: string) {
return this.{{camelCase name}}Service.findById(id);
}
@Patch(':id')
@ApiOperation({ summary: '更新{{chineseName}}信息' })
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '更新后的{{chineseName}}信息' })
update(@Param('id') id: string, @Body() dto: Update{{pascalCase name}}Dto) {
return this.{{camelCase name}}Service.update(id, dto);
}
@Delete(':id')
@ApiOperation({ summary: '删除{{chineseName}}' })
@ApiOkResponse({ description: '删除成功' })
delete(@Param('id') id: string) {
return this.{{camelCase name}}Service.delete(id);
}
{{#if softDelete}}
@Patch(':id/restore')
@ApiOperation({ summary: '恢复已删除的{{chineseName}}' })
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '恢复后的{{chineseName}}信息' })
restore(@Param('id') id: string) {
return this.{{camelCase name}}Service.restore(id);
}
{{/if}}
}