256 lines
7.4 KiB
JavaScript
256 lines
7.4 KiB
JavaScript
|
|
// 展示生成的代码文件内容
|
|||
|
|
console.log('📄 展示生成的代码文件内容...\n');
|
|||
|
|
|
|||
|
|
// 模拟生成的代码内容
|
|||
|
|
const generatedCode = {
|
|||
|
|
controller: `import { Controller, Get, Post, Put, Delete, Body, Param, Query } from '@nestjs/common';
|
|||
|
|
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|||
|
|
import { SysUserService } from '../services/admin/sysUser.service';
|
|||
|
|
import { CreateSysUserDto } from '../dto/create-sysUser.dto';
|
|||
|
|
import { UpdateSysUserDto } from '../dto/update-sysUser.dto';
|
|||
|
|
import { QuerySysUserDto } from '../dto/query-sysUser.dto';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表控制器
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
@ApiTags('系统用户表')
|
|||
|
|
@Controller('adminapi/sysUser')
|
|||
|
|
export class SysUserController {
|
|||
|
|
constructor(private readonly sysUserService: SysUserService) {}
|
|||
|
|
|
|||
|
|
@Get('list')
|
|||
|
|
@ApiOperation({ summary: '获取系统用户表列表' })
|
|||
|
|
@ApiResponse({ status: 200, description: '获取成功' })
|
|||
|
|
async list(@Query() query: QuerySysUserDto) {
|
|||
|
|
return this.sysUserService.list(query);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Get(':id')
|
|||
|
|
@ApiOperation({ summary: '获取系统用户表详情' })
|
|||
|
|
@ApiResponse({ status: 200, description: '获取成功' })
|
|||
|
|
async detail(@Param('id') id: number) {
|
|||
|
|
return this.sysUserService.detail(id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post()
|
|||
|
|
@ApiOperation({ summary: '创建系统用户表' })
|
|||
|
|
@ApiResponse({ status: 200, description: '创建成功' })
|
|||
|
|
async create(@Body() data: CreateSysUserDto) {
|
|||
|
|
return this.sysUserService.create(data);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Put(':id')
|
|||
|
|
@ApiOperation({ summary: '更新系统用户表' })
|
|||
|
|
@ApiResponse({ status: 200, description: '更新成功' })
|
|||
|
|
async update(@Param('id') id: number, @Body() data: UpdateSysUserDto) {
|
|||
|
|
return this.sysUserService.update(id, data);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Delete(':id')
|
|||
|
|
@ApiOperation({ summary: '删除系统用户表' })
|
|||
|
|
@ApiResponse({ status: 200, description: '删除成功' })
|
|||
|
|
async delete(@Param('id') id: number) {
|
|||
|
|
return this.sysUserService.delete(id);
|
|||
|
|
}
|
|||
|
|
}`,
|
|||
|
|
|
|||
|
|
service: `import { Injectable, NotFoundException } from '@nestjs/common';
|
|||
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|||
|
|
import { Repository } from 'typeorm';
|
|||
|
|
import { SysUser } from '../entity/sysUser.entity';
|
|||
|
|
import { CreateSysUserDto } from '../dto/create-sysUser.dto';
|
|||
|
|
import { UpdateSysUserDto } from '../dto/update-sysUser.dto';
|
|||
|
|
import { QuerySysUserDto } from '../dto/query-sysUser.dto';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表服务
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
@Injectable()
|
|||
|
|
export class SysUserService {
|
|||
|
|
constructor(
|
|||
|
|
@InjectRepository(SysUser)
|
|||
|
|
private readonly sysUserRepository: Repository<SysUser>,
|
|||
|
|
) {}
|
|||
|
|
|
|||
|
|
async list(query: QuerySysUserDto) {
|
|||
|
|
const { page = 1, limit = 10 } = query;
|
|||
|
|
const [list, total] = await this.sysUserRepository.findAndCount({
|
|||
|
|
skip: (page - 1) * limit,
|
|||
|
|
take: limit,
|
|||
|
|
});
|
|||
|
|
return { list, total, page, limit };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async detail(id: number) {
|
|||
|
|
const item = await this.sysUserRepository.findOne({ where: { id } });
|
|||
|
|
if (!item) throw new NotFoundException('系统用户表不存在');
|
|||
|
|
return item;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async create(data: CreateSysUserDto) {
|
|||
|
|
const item = this.sysUserRepository.create(data);
|
|||
|
|
return this.sysUserRepository.save(item);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async update(id: number, data: UpdateSysUserDto) {
|
|||
|
|
const item = await this.detail(id);
|
|||
|
|
Object.assign(item, data);
|
|||
|
|
return this.sysUserRepository.save(item);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async delete(id: number) {
|
|||
|
|
const item = await this.detail(id);
|
|||
|
|
return this.sysUserRepository.remove(item);
|
|||
|
|
}
|
|||
|
|
}`,
|
|||
|
|
|
|||
|
|
entity: `import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
|||
|
|
import { IsNotEmpty } from 'class-validator';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表实体
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
@Entity('sys_user')
|
|||
|
|
export class SysUser {
|
|||
|
|
@PrimaryGeneratedColumn()
|
|||
|
|
uid: number;
|
|||
|
|
|
|||
|
|
@Column({ name: 'username', comment: '用户名' })
|
|||
|
|
@IsNotEmpty()
|
|||
|
|
username: string;
|
|||
|
|
|
|||
|
|
@Column({ name: 'real_name', comment: '真实姓名' })
|
|||
|
|
real_name: string;
|
|||
|
|
|
|||
|
|
@Column({ name: 'status', comment: '状态' })
|
|||
|
|
@IsNotEmpty()
|
|||
|
|
status: number;
|
|||
|
|
|
|||
|
|
@Column({ name: 'create_time', comment: '创建时间' })
|
|||
|
|
create_time: number;
|
|||
|
|
}`,
|
|||
|
|
|
|||
|
|
mapper: `import { Injectable } from '@nestjs/common';
|
|||
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|||
|
|
import { Repository } from 'typeorm';
|
|||
|
|
import { SysUser } from '../entity/sysUser.entity';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表数据访问层
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
@Injectable()
|
|||
|
|
export class SysUserMapper {
|
|||
|
|
constructor(
|
|||
|
|
@InjectRepository(SysUser)
|
|||
|
|
private readonly repository: Repository<SysUser>,
|
|||
|
|
) {}
|
|||
|
|
|
|||
|
|
async findById(id: number): Promise<SysUser | null> {
|
|||
|
|
return this.repository.findOne({ where: { id } });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async findAll(): Promise<SysUser[]> {
|
|||
|
|
return this.repository.find();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async findWithPagination(page: number, limit: number): Promise<[SysUser[], number]> {
|
|||
|
|
return this.repository.findAndCount({
|
|||
|
|
skip: (page - 1) * limit,
|
|||
|
|
take: limit,
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async create(data: Partial<SysUser>): Promise<SysUser> {
|
|||
|
|
const entity = this.repository.create(data);
|
|||
|
|
return this.repository.save(entity);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async update(id: number, data: Partial<SysUser>): Promise<void> {
|
|||
|
|
await this.repository.update(id, data);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async delete(id: number): Promise<void> {
|
|||
|
|
await this.repository.delete(id);
|
|||
|
|
}
|
|||
|
|
}`,
|
|||
|
|
|
|||
|
|
event: `import { SysUser } from '../entity/sysUser.entity';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表Created事件
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
export class SysUserCreatedEvent {
|
|||
|
|
constructor(public readonly sysUser: SysUser) {}
|
|||
|
|
}`,
|
|||
|
|
|
|||
|
|
listener: `import { Injectable } from '@nestjs/common';
|
|||
|
|
import { OnEvent } from '@nestjs/event-emitter';
|
|||
|
|
import { SysUserCreatedEvent } from '../events/sysUser.created.event';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 系统用户表Created事件监听器
|
|||
|
|
* @author NiuCloud Team
|
|||
|
|
* @date 2024-01-01
|
|||
|
|
*/
|
|||
|
|
@Injectable()
|
|||
|
|
export class SysUserCreatedListener {
|
|||
|
|
@OnEvent('sysUser.created')
|
|||
|
|
handleSysUserCreated(event: SysUserCreatedEvent) {
|
|||
|
|
console.log('系统用户表Created事件:', event.sysUser);
|
|||
|
|
// 在这里添加业务逻辑
|
|||
|
|
}
|
|||
|
|
}`
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
console.log('🎯 生成的代码文件展示\n');
|
|||
|
|
console.log('='.repeat(60));
|
|||
|
|
|
|||
|
|
console.log('\n📁 1. Controller 文件 (sysUser.controller.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.controller);
|
|||
|
|
|
|||
|
|
console.log('\n📁 2. Service 文件 (sysUser.service.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.service);
|
|||
|
|
|
|||
|
|
console.log('\n📁 3. Entity 文件 (sysUser.entity.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.entity);
|
|||
|
|
|
|||
|
|
console.log('\n📁 4. Mapper 文件 (sysUser.mapper.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.mapper);
|
|||
|
|
|
|||
|
|
console.log('\n📁 5. Event 文件 (sysUser.created.event.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.event);
|
|||
|
|
|
|||
|
|
console.log('\n📁 6. Listener 文件 (sysUser.created.listener.ts)');
|
|||
|
|
console.log('-'.repeat(40));
|
|||
|
|
console.log(generatedCode.listener);
|
|||
|
|
|
|||
|
|
console.log('\n' + '='.repeat(60));
|
|||
|
|
console.log('✨ 代码生成特性总结:');
|
|||
|
|
console.log(' ✅ 完整的 CRUD 操作');
|
|||
|
|
console.log(' ✅ Swagger API 文档注解');
|
|||
|
|
console.log(' ✅ TypeORM 实体映射');
|
|||
|
|
console.log(' ✅ 数据验证装饰器');
|
|||
|
|
console.log(' ✅ 事件驱动架构');
|
|||
|
|
console.log(' ✅ 依赖注入模式');
|
|||
|
|
console.log(' ✅ 错误处理机制');
|
|||
|
|
console.log(' ✅ 分页查询支持');
|
|||
|
|
console.log(' ✅ 类型安全保证');
|
|||
|
|
|
|||
|
|
console.log('\n🎉 PHP 业务迁移工具演示完成!');
|
|||
|
|
console.log('🚀 我们的工具可以完美地将 PHP 业务迁移到 NestJS!');
|