Skip to content

Commit

Permalink
style: change User to UserEntity
Browse files Browse the repository at this point in the history
for other modules, have to change entity name User to UserEntity

#18
  • Loading branch information
seo-wo committed Dec 1, 2023
1 parent 0e0a149 commit 2a0fb0d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/auth/role/role.guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import UserRole from 'src/user/enum/user.enum';
import { User } from 'src/user/entity/user.entity';
import { UserEntity } from 'src/user/entity/user.entity';

@Injectable()
export class RoleGuard implements CanActivate {
Expand All @@ -12,14 +12,14 @@ export class RoleGuard implements CanActivate {
if (!role) {
return true;
}
const user: Partial<User> = context.switchToHttp().getRequest().user;
const user: Partial<UserEntity> = context.switchToHttp().getRequest().user;
if (!user) {
return false;
}
return this.matchRoles(role, user);
}

matchRoles(roles: UserRole[], user: Partial<User>): boolean {
matchRoles(roles: UserRole[], user: Partial<UserEntity>): boolean {
return roles.includes(user.role);
}
}
4 changes: 2 additions & 2 deletions src/decorator/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { User } from '../user/entity/user.entity';
import { UserEntity } from '../user/entity/user.entity';

export const GetUser = createParamDecorator(
(data: unknown, context: ExecutionContext): User => {
(data: unknown, context: ExecutionContext): UserEntity => {
const req = context.switchToHttp().getRequest();
return req.user;
},
Expand Down
4 changes: 2 additions & 2 deletions src/user/entity/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
} from 'typeorm';
import UserRole from '../enum/user.enum';

@Entity({})
export class User {
@Entity({ name: 'user' })
export class UserEntity {
@PrimaryGeneratedColumn()
id: number;

Expand Down
35 changes: 22 additions & 13 deletions src/user/repository/user.repository.ts
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,
},
});
}
}
4 changes: 2 additions & 2 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { UserRepository } from './repository/user.repository';
import { User } from './entity/user.entity';
import { UserEntity } from './entity/user.entity';

@Module({
imports: [TypeOrmModule.forFeature([User])],
imports: [TypeOrmModule.forFeature([UserEntity])],
controllers: [UserController],
providers: [UserService, UserRepository],
exports: [UserService, UserRepository],
Expand Down
18 changes: 12 additions & 6 deletions src/user/user.service.ts
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);
}
}

0 comments on commit 2a0fb0d

Please sign in to comment.