typescript-express-starter
Version:
Quick and Easy TypeScript Express Starter
51 lines (39 loc) • 1.83 kB
text/typescript
import { hash } from 'bcrypt';
import { Service } from 'typedi';
import { DB } from '@database';
import { CreateUserDto } from '@dtos/users.dto';
import { HttpException } from '@/exceptions/httpException';
import { User } from '@interfaces/users.interface';
()
export class UserService {
public async findAllUser(): Promise<User[]> {
const allUser: User[] = await DB.Users.findAll();
return allUser;
}
public async findUserById(userId: number): Promise<User> {
const findUser: User = await DB.Users.findByPk(userId);
if (!findUser) throw new HttpException(409, "User doesn't exist");
return findUser;
}
public async createUser(userData: CreateUserDto): Promise<User> {
const findUser: User = await DB.Users.findOne({ where: { email: userData.email } });
if (findUser) throw new HttpException(409, `This email ${userData.email} already exists`);
const hashedPassword = await hash(userData.password, 10);
const createUserData: User = await DB.Users.create({ ...userData, password: hashedPassword });
return createUserData;
}
public async updateUser(userId: number, userData: CreateUserDto): Promise<User> {
const findUser: User = await DB.Users.findByPk(userId);
if (!findUser) throw new HttpException(409, "User doesn't exist");
const hashedPassword = await hash(userData.password, 10);
await DB.Users.update({ ...userData, password: hashedPassword }, { where: { id: userId } });
const updateUser: User = await DB.Users.findByPk(userId);
return updateUser;
}
public async deleteUser(userId: number): Promise<User> {
const findUser: User = await DB.Users.findByPk(userId);
if (!findUser) throw new HttpException(409, "User doesn't exist");
await DB.Users.destroy({ where: { id: userId } });
return findUser;
}
}