parent
528523e5c9
commit
fc37b598ec
5 changed files with 77 additions and 0 deletions
@ -0,0 +1,6 @@ |
|||||||
|
export class UserDto { |
||||||
|
readonly name: string; |
||||||
|
readonly email: string; |
||||||
|
readonly password: string; |
||||||
|
readonly gender: string; |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
import { Table, Column, Model, DataType } from 'sequelize-typescript'; |
||||||
|
|
||||||
|
@Table |
||||||
|
export class User extends Model<User> { |
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
name: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
unique: true, |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
email: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
password: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.ENUM, |
||||||
|
values: ['male', 'female'], |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
gender: string; |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
import { Module } from '@nestjs/common'; |
||||||
|
import { UsersService } from './users.service'; |
||||||
|
import { usersProviders } from './users.providers'; |
||||||
|
|
||||||
|
@Module({ |
||||||
|
providers: [UsersService, ...usersProviders], |
||||||
|
exports: [UsersService], |
||||||
|
}) |
||||||
|
export class UsersModule {} |
@ -0,0 +1,9 @@ |
|||||||
|
import { User } from './user.entity'; |
||||||
|
import { USER_REPOSITORY } from '../../core/constants'; |
||||||
|
|
||||||
|
export const usersProviders = [ |
||||||
|
{ |
||||||
|
provide: USER_REPOSITORY, |
||||||
|
useValue: User, |
||||||
|
}, |
||||||
|
]; |
@ -0,0 +1,23 @@ |
|||||||
|
import { Injectable, Inject } from '@nestjs/common'; |
||||||
|
import { User } from './user.entity'; |
||||||
|
import { UserDto } from './dto/user.dto'; |
||||||
|
import { USER_REPOSITORY } from '../../core/constants'; |
||||||
|
|
||||||
|
@Injectable() |
||||||
|
export class UsersService { |
||||||
|
constructor( |
||||||
|
@Inject(USER_REPOSITORY) private readonly userRepository: typeof User, |
||||||
|
) {} |
||||||
|
|
||||||
|
async create(user: UserDto): Promise<User> { |
||||||
|
return await this.userRepository.create<User>(user); |
||||||
|
} |
||||||
|
|
||||||
|
async findOneByEmail(email: string): Promise<User> { |
||||||
|
return await this.userRepository.findOne<User>({ where: { email } }); |
||||||
|
} |
||||||
|
|
||||||
|
async findOneById(id: number): Promise<User> { |
||||||
|
return await this.userRepository.findOne<User>({ where: { id } }); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue