- dto.hbs: 移除 hasQueryDto 条件,始终生成 QueryDto 类 - controller.hbs: 始终导入使用 QueryDto 而非 PaginationQueryDto - hasQueryDto 现只控制是否提取查询字段,不影响类型声明 便于后续扩展查询参数,无需修改 controller 导入 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
180 lines
6.1 KiB
Handlebars
180 lines
6.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';
|
|
{{#if hasQueryDto}}
|
|
import { Prisma } from '@prisma/client';
|
|
{{/if}}
|
|
|
|
import {
|
|
Create{{pascalCase name}}Dto,
|
|
Update{{pascalCase name}}Dto,
|
|
{{pascalCase name}}ResponseDto,
|
|
{{#if needsDetailDto}}
|
|
{{pascalCase name}}DetailResponseDto,
|
|
{{/if}}
|
|
Paginated{{pascalCase name}}ResponseDto,
|
|
{{pascalCase name}}QueryDto,
|
|
{{#each manyToMany}}
|
|
Assign{{pascalCase name}}Dto,
|
|
{{targetModel}}BriefDto,
|
|
{{/each}}
|
|
} from './dto/{{kebabCase name}}.dto';
|
|
import { {{pascalCase name}}Service } from './{{kebabCase name}}.service';
|
|
|
|
import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard';
|
|
import { RequirePermission } from '@/permission/decorators/require-permission.decorator';
|
|
import { PermissionGuard } from '@/permission/guards/permission.guard';
|
|
|
|
@ApiTags('{{chineseName}}')
|
|
@Controller('{{kebabCase pluralName}}')
|
|
@UseGuards(JwtAuthGuard, PermissionGuard)
|
|
@ApiBearerAuth()
|
|
export class {{pascalCase name}}Controller {
|
|
constructor(private readonly {{camelCase name}}Service: {{pascalCase name}}Service) {}
|
|
|
|
@Post()
|
|
@RequirePermission('{{kebabCase name}}:create')
|
|
@ApiOperation({ summary: '创建{{chineseName}}' })
|
|
@ApiCreatedResponse({ type: {{pascalCase name}}ResponseDto, description: '创建成功' })
|
|
create(@Body() dto: Create{{pascalCase name}}Dto) {
|
|
return this.{{camelCase name}}Service.create(dto);
|
|
}
|
|
|
|
@Get()
|
|
@RequirePermission('{{kebabCase name}}:read')
|
|
@ApiOperation({ summary: '获取所有{{chineseName}}(分页)' })
|
|
@ApiOkResponse({ type: Paginated{{pascalCase name}}ResponseDto, description: '{{chineseName}}列表' })
|
|
findAll(@Query() query: {{pascalCase name}}QueryDto) {
|
|
{{#if hasQueryDto}}
|
|
{{#if (eq serviceType 'CrudService')}}
|
|
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}}
|
|
return this.{{camelCase name}}Service.findAllWithRelations(query);
|
|
{{/if}}
|
|
{{else}}
|
|
{{#if (eq serviceType 'CrudService')}}
|
|
return this.{{camelCase name}}Service.findAll(query);
|
|
{{else}}
|
|
return this.{{camelCase name}}Service.findAllWithRelations(query);
|
|
{{/if}}
|
|
{{/if}}
|
|
}
|
|
|
|
{{#if softDelete}}
|
|
@Get('deleted')
|
|
@RequirePermission('{{kebabCase name}}:read')
|
|
@ApiOperation({ summary: '获取已删除的{{chineseName}}列表(分页)' })
|
|
@ApiOkResponse({ type: Paginated{{pascalCase name}}ResponseDto, description: '已删除{{chineseName}}列表' })
|
|
findDeleted(@Query() query: {{pascalCase name}}QueryDto) {
|
|
{{#if hasQueryDto}}
|
|
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}}
|
|
return this.{{camelCase name}}Service.findDeleted(query);
|
|
{{/if}}
|
|
}
|
|
|
|
{{/if}}
|
|
@Get(':id')
|
|
@RequirePermission('{{kebabCase name}}:read')
|
|
@ApiOperation({ summary: '根据 ID 获取{{chineseName}}' })
|
|
{{#if needsDetailDto}}
|
|
@ApiOkResponse({ type: {{pascalCase name}}DetailResponseDto, description: '{{chineseName}}详情' })
|
|
findById(@Param('id') id: string) {
|
|
return this.{{camelCase name}}Service.findByIdWithRelations(id);
|
|
}
|
|
{{else if needsResponseDto}}
|
|
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '{{chineseName}}详情' })
|
|
findById(@Param('id') id: string) {
|
|
return this.{{camelCase name}}Service.findByIdWithRelations(id);
|
|
}
|
|
{{else}}
|
|
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '{{chineseName}}详情' })
|
|
findById(@Param('id') id: string) {
|
|
return this.{{camelCase name}}Service.findById(id);
|
|
}
|
|
{{/if}}
|
|
|
|
@Patch(':id')
|
|
@RequirePermission('{{kebabCase name}}:update')
|
|
@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')
|
|
@RequirePermission('{{kebabCase name}}:delete')
|
|
@ApiOperation({ summary: '删除{{chineseName}}' })
|
|
@ApiOkResponse({ description: '删除成功' })
|
|
delete(@Param('id') id: string) {
|
|
return this.{{camelCase name}}Service.delete(id);
|
|
}
|
|
|
|
{{#if softDelete}}
|
|
@Patch(':id/restore')
|
|
@RequirePermission('{{kebabCase name}}:update')
|
|
@ApiOperation({ summary: '恢复已删除的{{chineseName}}' })
|
|
@ApiOkResponse({ type: {{pascalCase name}}ResponseDto, description: '恢复后的{{chineseName}}信息' })
|
|
restore(@Param('id') id: string) {
|
|
return this.{{camelCase name}}Service.restore(id);
|
|
}
|
|
{{/if}}
|
|
{{#if hasManyToMany}}
|
|
{{#each manyToMany}}
|
|
|
|
@Get(':id/{{name}}')
|
|
@RequirePermission('{{kebabCase ../name}}:read')
|
|
@ApiOperation({ summary: '获取{{../chineseName}}的{{name}}列表' })
|
|
@ApiOkResponse({ type: [{{targetModel}}BriefDto], description: '{{name}}列表' })
|
|
get{{pascalCase name}}(@Param('id') id: string) {
|
|
return this.{{camelCase ../name}}Service.getManyToManyTargets(id, '{{name}}');
|
|
}
|
|
|
|
@Put(':id/{{name}}')
|
|
@RequirePermission('{{kebabCase ../name}}:update')
|
|
@ApiOperation({ summary: '分配{{../chineseName}}的{{name}}' })
|
|
@ApiOkResponse({ type: {{pascalCase ../name}}DetailResponseDto, description: '分配成功' })
|
|
assign{{pascalCase name}}(@Param('id') id: string, @Body() dto: Assign{{pascalCase name}}Dto) {
|
|
return this.{{camelCase ../name}}Service.assignManyToMany(id, '{{name}}', { targetIds: dto.{{target}}Ids });
|
|
}
|
|
{{/each}}
|
|
{{/if}}
|
|
}
|