-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for other modules, have to change entity name User to UserEntity #18
- Loading branch information
Showing
6 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { User } from '../entity/user.entity'; | ||
import { UserEntity } from '../entity/user.entity'; | ||
import { CreateUserDto } from '../dto/create-user.dto'; | ||
|
||
export class UserRepository extends Repository<User> { | ||
export class UserRepository extends Repository<UserEntity> { | ||
constructor( | ||
@InjectRepository(User) | ||
@InjectRepository(UserEntity) | ||
private readonly dataSource: DataSource, | ||
) { | ||
super(User, dataSource.manager); | ||
super(UserEntity, dataSource.manager); | ||
} | ||
|
||
async createUser(user: any): Promise<User> { | ||
const newUser = new User(); | ||
newUser.googleEmail = user.email; | ||
newUser.nickname = user.name; | ||
newUser.googleID = await bcrypt.hash(user.sub, 10); | ||
async createUser(user: CreateUserDto): Promise<UserEntity> { | ||
const newUser = this.create({ | ||
googleEmail: user.email, | ||
nickname: user.nickname, | ||
googleID: await bcrypt.hash(user.googleId, 10), | ||
slackMemberId: user.slackId, | ||
}); | ||
await this.save(newUser); | ||
return newUser; | ||
} | ||
|
||
async findOneByEmail( | ||
email: string, | ||
): Promise<User | undefined | Partial<User>> { | ||
return await this.findOne({ | ||
async findOneByEmail(email: string): Promise<UserEntity | null> { | ||
return this.findOne({ | ||
where: { | ||
googleEmail: email, | ||
}, | ||
}); | ||
} | ||
|
||
async findOneByUid(id: number): Promise<UserEntity | null> { | ||
return this.findOne({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { User } from './entity/user.entity'; | ||
import { UserEntity } from './entity/user.entity'; | ||
import { UserRepository } from './repository/user.repository'; | ||
import { CreateUserDto } from './dto/create-user.dto'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
private readonly logger = new Logger(UserService.name); | ||
|
||
constructor(private readonly userRepository: UserRepository) {} | ||
|
||
async findOneByEmail(email: string): Promise<User | null | Partial<User>> { | ||
async findOneByEmail(email: string): Promise<UserEntity | null> { | ||
this.logger.debug(`findOneByEmail [email: ${email}]`); | ||
return await this.userRepository.findOneByEmail(email); | ||
return this.userRepository.findOneByEmail(email); | ||
} | ||
|
||
async findOneByUid(id: number): Promise<UserEntity | null> { | ||
this.logger.debug(`findOneByUid [id: ${id}]`); | ||
return this.userRepository.findOneByUid(id); | ||
} | ||
|
||
async createUser(user: Partial<User>): Promise<User> { | ||
this.logger.debug(`createUser [user: ${JSON.stringify(user)}]`); | ||
return await this.userRepository.createUser(user); | ||
async createUser(user: CreateUserDto): Promise<UserEntity> { | ||
return this.userRepository.createUser(user); | ||
} | ||
} |