增加DTO,开始做接口Controller层,设计接口文档

master
expressgy 2 years ago
parent 4bd562ef17
commit 3763a85613
  1. 10
      src/app.module.ts
  2. 8
      src/main.ts
  3. 11
      src/starlight/dto/create-starlight.dto.ts
  4. 113
      src/starlight/dto/register.dto.ts
  5. 66
      src/starlight/starlight.controller.ts
  6. 68
      src/starlight/starlight.service.ts
  7. 12
      tsconfig.json

@ -1,10 +1,10 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { GredisModule } from './Gservice/GREDIS/gredis.module'; import { GredisModule } from '@/Gservice/GREDIS/gredis.module';
import { GloggerModule } from './Gservice/GLOGGER/glogger.module'; import { GloggerModule } from '@/Gservice/GLOGGER/glogger.module';
import { GdatabaseModule } from './Gservice/GDATABASE/gdatabase.module'; import { GdatabaseModule } from '@/Gservice/GDATABASE/gdatabase.module';
import config from '../config'; import config from '@CFG/index';
import { StarlightModule } from './starlight/starlight.module'; import { StarlightModule } from '@/starlight/starlight.module';
@Module({ @Module({
imports: [ imports: [

@ -9,18 +9,18 @@ import { ValidationPipe } from '@nestjs/common';
// Fastify文件上传插件 // Fastify文件上传插件
import fastifyMultipart from '@fastify/multipart'; import fastifyMultipart from '@fastify/multipart';
// 日志服务 // 日志服务
import { GloggerService } from './Gservice/GLOGGER/glogger.service'; import { GloggerService } from '@/Gservice/GLOGGER/glogger.service';
// API文档 // API文档
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
// 配置文件 // 配置文件
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
// 异常捕获 // 异常捕获
import { GexceptionsfilterFilter } from './Gexceptions/gexceptionsfilter.filter'; import { GexceptionsfilterFilter } from '@/Gexceptions/gexceptionsfilter.filter';
// 拦截器 // 拦截器
// --全局开发拦截 // --全局开发拦截
import { GdevinterceptorInterceptor } from './Ginterceptor/gdevinterceptor.interceptor'; import { GdevinterceptorInterceptor } from '@/Ginterceptor/gdevinterceptor.interceptor';
// --全局响应拦截 // --全局响应拦截
import { GresponseinterceptorInterceptor } from './Ginterceptor/gresponseinterceptor.interceptor'; import { GresponseinterceptorInterceptor } from '@/Ginterceptor/gresponseinterceptor.interceptor';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>( const app = await NestFactory.create<NestFastifyApplication>(

@ -4,9 +4,16 @@ export class CreateStarlightDto {
@IsNotEmpty({ @IsNotEmpty({
message: 'name 不能为空', message: 'name 不能为空',
}) })
@IsString() @IsString({
message: '必须为字符串',
})
name: string; name: string;
@IsNumber() @IsNumber(
{},
{
message: '必须为数字',
},
)
age: number; age: number;
} }

@ -0,0 +1,113 @@
/* *
* * @name: register.dto.ts
* * @description:
* * @author: x7129
* * @date: 2023-03-23 15:01
* * */
import {
IsDate,
IsEmail,
IsEnum,
IsNotEmpty,
IsNumber,
IsOptional,
IsPhoneNumber,
IsString,
Length,
Max,
Min,
} from 'class-validator';
import { Type } from 'class-transformer';
// ? ?
// ? 函数名称: 性别枚举
// ? 函数用法:
// ? ?
export enum sex {
'男' = 1,
'女' = 2,
'其他' = 3,
}
// 此文件为参数验证演示
// https://github.com/typestack/class-validator#usage
// 这里有更多的验证方法
export class RegisterEmailCheckoutEmailDto {
// @ 邮箱
@Length(8, 128, { message: '请将邮箱长度控制在8到128位之间!' })
@IsEmail({}, { message: '邮箱格式错误!' })
@IsString({ message: '邮箱应为字符串格式!' })
email: string;
}
export class RegisterEmailSignUpDto extends RegisterEmailCheckoutEmailDto {
// @ 验证码
@Length(6, 6, { message: '验证码是6位字符!' })
code: string;
// @ 密码
@Length(8, 128, { message: '请将密码长度控制在8到128位之间!' })
password: string;
// @ 用户名
@Length(8, 128, { message: '请将用户名长度控制在8到128位之间!' })
usernmae: string;
// @ 真实姓名
@IsOptional()
@Length(2, 128, { message: '请将真实姓名长度控制在2到128位之间!' })
realname: string;
// @ 昵称
@IsOptional()
@Length(1, 128, { message: '请将昵称长度控制在1到128位之间!' })
nickname: string;
// @ 生日
@IsOptional()
@IsDate({ message: '生日是一个日期,如1999-12-24!' })
@Type(() => Date)
birthday: number;
// @ 性别
@IsOptional()
@IsEnum(sex, { message: '性别必须为:' + Object.keys(sex) })
sex: number;
// @ 住址
@IsOptional()
@Length(1, 128, { message: '请将住址长度控制在1到128位之间!' })
address: string;
// @ 国家/地区
@IsOptional()
@Length(1, 128, { message: '请将国家/地区长度控制在1到128位之间!' })
country: string;
// @ 个人简介
@IsOptional()
@Length(1, 128, { message: '请将个人简介长度控制在1到128位之间!' })
profile: string;
// @ github地址
@IsOptional()
@Length(1, 128, { message: '请将github地址长度控制在1到128位之间!' })
githubUrl: string;
// @ 个人主页地址
@IsOptional()
@Length(1, 128, { message: '请将个人主页地址长度控制在1到128位之间!' })
personalUrl: string;
// @ 阿里账号
@IsOptional()
@Length(1, 128, { message: '请将阿里账号长度控制在1到128位之间!' })
alibabaId: string;
// @ 抖音账号
@IsOptional()
@Length(1, 128, { message: '请将抖音账号长度控制在1到128位之间!' })
tiktokId: string;
// @ 微博账号
@IsOptional()
@Length(1, 128, { message: '请将微博账号长度控制在1到128位之间!' })
weiboId: string;
}

@ -1,3 +1,9 @@
/* *
* * @name: starlight.controller.ts
* * @description: starLight Controller层
* * @author: x7129
* * @date: 2023-03-23 16:09
* * */
import { import {
Controller, Controller,
Get, Get,
@ -18,23 +24,71 @@ import { StarlightService } from './starlight.service';
import { CreateStarlightDto } from './dto/create-starlight.dto'; import { CreateStarlightDto } from './dto/create-starlight.dto';
import { UpdateStarlightDto } from './dto/update-starlight.dto'; import { UpdateStarlightDto } from './dto/update-starlight.dto';
import { GloggerService } from '../Gservice/GLOGGER/glogger.service'; import { GloggerService } from '@/Gservice/GLOGGER/glogger.service';
import { GdatabaseService } from '../Gservice/GDATABASE/gdatabase.service'; import { GdatabaseService } from '@/Gservice/GDATABASE/gdatabase.service';
import { GredisService } from '../Gservice/GREDIS/gredis.service'; import { GredisService } from '@/Gservice/GREDIS/gredis.service';
import {
RegisterEmailCheckoutEmailDto,
RegisterEmailSignUpDto,
} from './dto/register.dto';
@ApiTags('starlight') // C C
@Controller('starlight') // C 类名称: StarlightController
// C 类描述: starLight Controller层入口
// C C
@ApiTags('starLight')
@Controller('starLight')
// 拦截器 // 拦截器
@UseGuards(StarlightGuard) @UseGuards(StarlightGuard)
export class StarlightController { export class StarlightController {
constructor( constructor(
// @ 配置文件服务
private readonly config: ConfigService, private readonly config: ConfigService,
// @ service层
private readonly starlightService: StarlightService, private readonly starlightService: StarlightService,
// @ 日志服务
private readonly logger: GloggerService, private readonly logger: GloggerService,
// @ 数据库服务
private readonly database: GdatabaseService, private readonly database: GdatabaseService,
// @ Redis服务
private readonly redis: GredisService, private readonly redis: GredisService,
) {} ) {}
//#region 邮箱注册
// ? ?
// ? 函数名称: registerEmailCheckoutEmail
// ? 函数描述: 查重邮箱注册
// ? ?
@Get('register/email/checkoutEmail/:email')
registerEmailCheckoutEmail(
@Param() param: RegisterEmailCheckoutEmailDto,
): Promise<object> {
return this.starlightService.registerEmailCheckoutEmail(param);
}
// ? ?
// ? 函数名称: registerEmailSendCode
// ? 函数描述: 发送邮箱注册验证码
// ? ?
@Get('register/email/sendCode/:email')
registerEmailSendCode(
@Param() param: RegisterEmailCheckoutEmailDto,
): Promise<object> {
return this.starlightService.registerEmailSendCode(param);
}
// ? ?
// ? 函数名称: registerEmailSignUp
// ? 函数描述:
// ? ?
//#endregion
@Post('/register/email/signUp')
registerEmailSignUpDto(
@Body() body: RegisterEmailSignUpDto,
): Promise<object> {
return this.starlightService.registerEmailSignUpDto(body);
}
//#region 测试
@Post() @Post()
create(@Body() createStarlightDto: CreateStarlightDto) { create(@Body() createStarlightDto: CreateStarlightDto) {
this.logger.debug(createStarlightDto); this.logger.debug(createStarlightDto);
@ -44,7 +98,6 @@ export class StarlightController {
}; };
return this.starlightService.create(createStarlightDto); return this.starlightService.create(createStarlightDto);
} }
@Get() @Get()
async findAll() { async findAll() {
this.logger.debug(this.config.get('master')); this.logger.debug(this.config.get('master'));
@ -90,4 +143,5 @@ export class StarlightController {
); );
return 'Hello World'; return 'Hello World';
} }
//#endregion
} }

@ -1,9 +1,76 @@
/* *
* * @name: starlight.service.ts
* * @description: starLight Service层
* * @author: x7129
* * @date: 2023-03-23 17:44
* * */
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { CreateStarlightDto } from './dto/create-starlight.dto'; import { CreateStarlightDto } from './dto/create-starlight.dto';
import { UpdateStarlightDto } from './dto/update-starlight.dto'; import { UpdateStarlightDto } from './dto/update-starlight.dto';
import {
RegisterEmailCheckoutEmailDto,
RegisterEmailSignUpDto,
} from './dto/register.dto';
import { ConfigService } from '@nestjs/config';
import { GloggerService } from '@/Gservice/GLOGGER/glogger.service';
import { GdatabaseService } from '@/Gservice/GDATABASE/gdatabase.service';
import { GredisService } from '@/Gservice/GREDIS/gredis.service';
// C C
// C 类名称: StarlightService
// C 类描述: 为StartController提供服务
// C C
@Injectable() @Injectable()
export class StarlightService { export class StarlightService {
constructor(
// @ 配置文件服务
private readonly config: ConfigService,
// @ 日志服务
private readonly logger: GloggerService,
// @ 数据库服务
private readonly database: GdatabaseService,
// @ Redis服务
private readonly redis: GredisService,
) {}
//#region 邮箱注册
// ? ?
// ? 函数名称: registerEmailCheckoutEmail
// ? 函数描述: 查重邮箱注册
// ? ?
// ? ?
async registerEmailCheckoutEmail(params: RegisterEmailCheckoutEmailDto) {
return {
data: { ok: 'ok' },
message: '',
success: true,
};
}
// ? 函数名称: registerEmailSendCode
// ? 函数描述: 发送邮箱注册验证码
// ? ?
async registerEmailSendCode(params: RegisterEmailCheckoutEmailDto) {
return {
data: { ok: 'ok' },
message: '',
success: true,
};
}
// ? ?
// ? 函数名称: registerEmailSignUpDto
// ? 函数描述: 邮箱注册
// ? ?
async registerEmailSignUpDto(body: RegisterEmailSignUpDto) {
return {
data: { ok: 'ok' },
message: '',
success: true,
};
}
//#endregion
//#region
create(createStarlightDto: CreateStarlightDto) { create(createStarlightDto: CreateStarlightDto) {
return 'This action adds a new starlight'; return 'This action adds a new starlight';
} }
@ -27,4 +94,5 @@ export class StarlightService {
remove(id: number) { remove(id: number) {
return `This action removes a #${id} starlight`; return `This action removes a #${id} starlight`;
} }
//#endredion
} }

@ -16,6 +16,14 @@
"noImplicitAny": false, "noImplicitAny": false,
"strictBindCallApply": false, "strictBindCallApply": false,
"forceConsistentCasingInFileNames": false, "forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false "noFallthroughCasesInSwitch": false,
} "paths": {
"@/*": [
"src/*"
],
"@CFG/*": [
"config/*"
]
}
},
} }

Loading…
Cancel
Save