add users
This commit is contained in:
30
src/users/user.service.ts
Normal file
30
src/users/user.service.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import User from './user.entity';
|
||||
import CreateUserDto from './dto/createUser.dto';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private usersRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
async getByEmail(email: string) {
|
||||
const user = await this.usersRepository.findOne({ where: { email } });
|
||||
if (user) {
|
||||
return user;
|
||||
}
|
||||
throw new HttpException(
|
||||
'User with this email does not exist',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
async create(userData: CreateUserDto) {
|
||||
const newUser = this.usersRepository.create(userData);
|
||||
await this.usersRepository.save(newUser);
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user