-
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.
Merge pull request #47 from GeunH/feature/be-signup
[signup/be] baseresponse를 위한 interceptor 구현
- Loading branch information
Showing
8 changed files
with
114 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class BaseResponse<T> { | ||
constructor( | ||
public data: T, | ||
public message: string, | ||
public statusCode: number | ||
) {} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter, | ||
HttpException, | ||
} from "@nestjs/common"; | ||
import { BaseResponse } from "./baseResponse"; | ||
import { Response } from "express"; | ||
|
||
@Catch(HttpException) | ||
export class HttpExceptionFilter implements ExceptionFilter { | ||
catch(exception: HttpException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const status = exception.getStatus(); | ||
|
||
response | ||
.status(status) | ||
.json(new BaseResponse(null, exception.message, status)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
} from "@nestjs/common"; | ||
import { Observable } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
import { BaseResponse } from "./baseResponse"; | ||
|
||
@Injectable() | ||
export class TransformInterceptor<T> | ||
implements NestInterceptor<T, BaseResponse<T>> | ||
{ | ||
intercept( | ||
context: ExecutionContext, | ||
next: CallHandler<T> | ||
): Observable<BaseResponse<T>> { | ||
return next | ||
.handle() | ||
.pipe( | ||
map((data) => ({ data: data, message: "Success", statusCode: 200 })) | ||
); | ||
} | ||
} |
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,42 +1,41 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
DeleteDateColumn | ||
} from 'typeorm'; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn('increment') | ||
id: number; | ||
|
||
@Column({ type: 'varchar', length: 20, unique : true }) | ||
nickName: string; | ||
|
||
@Column({ type: 'varchar', length: 50 }) | ||
email: string; | ||
|
||
@Column({ type: 'int' }) | ||
age: number; | ||
|
||
@Column({ type: 'boolean' }) | ||
gender: boolean; | ||
|
||
@Column({ type: 'varchar', length: 50, nullable: true }) | ||
password: string | null; | ||
|
||
@Column({ type: 'varchar', length: 20, nullable: true }) | ||
social_provider: string | null; | ||
|
||
@CreateDateColumn({ type: 'timestamp' }) | ||
created_at: Date; | ||
|
||
@DeleteDateColumn({ type: 'timestamp', nullable: true}) | ||
deleted_at: Date | null; | ||
|
||
@UpdateDateColumn({ type: 'timestamp'}) | ||
updated_at: Date; | ||
} | ||
|
||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
DeleteDateColumn, | ||
} from "typeorm"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn("increment") | ||
id: number; | ||
|
||
@Column({ type: "varchar", length: 20, unique: true }) | ||
nickName: string; | ||
|
||
@Column({ type: "varchar", length: 50 }) | ||
email: string; | ||
|
||
@Column({ type: "int" }) | ||
age: number; | ||
|
||
@Column({ type: "boolean" }) | ||
gender: boolean; | ||
|
||
@Column({ type: "varchar", length: 50, nullable: true }) | ||
password: string | null; | ||
|
||
@Column({ type: "varchar", length: 20, nullable: true }) | ||
social_provider: string | null; | ||
|
||
@CreateDateColumn({ type: "timestamp" }) | ||
created_at: Date; | ||
|
||
@DeleteDateColumn({ type: "timestamp", nullable: true }) | ||
deleted_at: Date | null; | ||
|
||
@UpdateDateColumn({ type: "timestamp" }) | ||
updated_at: Date; | ||
} |
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,17 +1,15 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { UserInfoDto } from "./dto/userInfo.dto"; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { User } from './entities/user.entity'; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { UserRepository } from "./user.repository"; | ||
|
||
@Injectable() | ||
export class UserService { | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private usersRepository: UserRepository, | ||
) { } | ||
signup(userInfoDto: UserInfoDto) { | ||
return this.usersRepository.createUser(userInfoDto); | ||
} | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private usersRepository: UserRepository | ||
) {} | ||
signup(userInfoDto: UserInfoDto) { | ||
return this.usersRepository.createUser(userInfoDto); | ||
} | ||
} |