用户模块

23 阅读1分钟

用户模块

创建

nest g res user /module
prisma下entity不用

DTO

// DTO
export class CreateUserDto {
    name:string
}

ValidationPipe

DTO参数校验
npm install --save class-validator class-transformer
class CreateUserDto {
    @ApiProperty({ example: 'coldpants', description: '用户名' })
    @IsNotEmpty({
        message: '用户名不能为空'
    })
    @MinLength(5)
    @MaxLength(16)
    username: string
    
    @ApiProperty({ example: '123456', description: '密码' })
    @IsNotEmpty({
        message: '密码不能为空'
    })
    @MinLength(8)
    password: string

    @ApiProperty({ example: 'coldpants@qq.com', description: '邮箱' })
    @IsNotEmpty({
        message: '邮箱不能为空'
    })
    @IsEmail({}, { message: '邮箱格式不合法' })
    email: string

    @ApiProperty({ example: '1.jpg', description: '用户头像' })
    @IsOptional()
    avatar?: string

    @ApiProperty({ example: '13788888888', description: '手机号' })
    @IsOptional()
    telephone: string
}
@ApiPropert为swagger接口文档
@IsOptional()可选

crud

用户注册、登录、修改信息、获取列表

await this.prismaService.user.create({ data: newUser })

await this.prismaService.user.update({
    where: { id: userId },
    data: { username, email, telephone, avatar }
})

const findUser = await this.prismaService.user.findUnique({
    where: { id: uicd.userId }
})

this.prismaService.user.delete({ where: { id: dud.id } });