Created
September 19, 2021 13:24
-
-
Save rskhan167/c67a87668d455bb632bbfe61a0a0d0bd to your computer and use it in GitHub Desktop.
Nestjs Series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { InjectRepository } from '@mikro-orm/nestjs'; | |
import { User } from './entities/user.entity'; | |
import { CreateUserDto } from './dtos/create-user.dto'; | |
import { UserRepository } from './user.repository'; | |
@Injectable() | |
export class UserService { | |
constructor( | |
@InjectRepository(User) | |
private readonly userRepository: UserRepository, | |
) {} | |
async create(createUserDto: CreateUserDto): Promise<User> { | |
const email = createUserDto.email; | |
const alreadyCreated = await this.userRepository.findOne({ email }); | |
if (!alreadyCreated) { | |
const user = new User( | |
createUserDto.name, | |
createUserDto.email, | |
createUserDto.password, | |
createUserDto.profile_image, | |
); | |
await this.userRepository.persistAndFlush(user); | |
return user; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment