Building the user module

master
Mahdi 4 months ago
parent 528523e5c9
commit fc37b598ec
  1. 6
      src/modules/users/dto/user.dto.ts
  2. 30
      src/modules/users/user.entity.ts
  3. 9
      src/modules/users/users.module.ts
  4. 9
      src/modules/users/users.providers.ts
  5. 23
      src/modules/users/users.service.ts

@ -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…
Cancel
Save