完成后端登录dto验证

This commit is contained in:
2025-05-06 22:52:51 +08:00
parent cab4fdb6e1
commit 94cc8feda8
30 changed files with 7027 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserController } from './user.controller';
describe('UserController', () => {
let controller: UserController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
}).compile();
controller = module.get<UserController>(UserController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';
@Controller('user')
export class UserController {}

View File

@@ -0,0 +1,43 @@
import { Column, CreateDateColumn, DeleteDateColumn, Entity, Index, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column('uuid', { unique: true, default: () => 'gen_random_uuid()' })
userId: string;
@Column({ length: 32 })
@Index({ unique: true })
username: string;
@Column({ length: 30 })
nickname: string;
@Column({ nullable: true, type: 'char', length: 32 })
salt: string;
@Column({ nullable: true, type: 'char', length: 64 })
hashed_password: string;
@Column({ nullable: true, length: 254 })// RFC 5321
@Index({ unique: true })
email: string;
@Column({ nullable: true, length: 20 })// China Mainland
@Index({ unique: true })
phone: string;
@Column({ nullable: true })
avatar: string;
@CreateDateColumn({ precision: 3 })
created_at: Date;
@UpdateDateColumn({ precision: 3 })
updated_at: Date;
@DeleteDateColumn({ nullable: true, precision: 3 })
deleted_at: Date;
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { UserController } from './user.controller';
import { UserService } from './user.service';
@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UserController],
providers: [UserService]
})
export class UserModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserService } from './user.service';
describe('UserService', () => {
let service: UserService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UserService],
}).compile();
service = module.get<UserService>(UserService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './user.entity';
import { Repository } from 'typeorm';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) { }
async findOne(options: Partial<Pick<User, 'userId' | 'username' | 'phone' | 'email'>>): Promise<User | null> {
if (Object.keys(options).length === 0) {
return null;
}
return this.userRepository.findOne({ where: options });
}
async create(user: Partial<User>): Promise<User> {
const newUser = this.userRepository.create(user);
return this.userRepository.save(newUser);
}
}