Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: BE 리팩토링 (Recruit) & ResponseEntity 추가 #150

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
741 changes: 396 additions & 345 deletions server/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { CallHandler, ExecutionContext, Injectable, NestInterceptor, RequestTimeoutException } from "@nestjs/common";
import { Observable } from "rxjs";
import { CustomJwtService } from "src/common/modules/custom-jwt/custom-jwt.service";

Expand All @@ -13,7 +13,13 @@ export class HttpRequestBodyInterceptor implements NestInterceptor {
if (accessToken) {
try {
const { userIdx } = this.jwtService.verifyAccessToken(accessToken);
request.body.userId = userIdx;
if (request.method === "GET") {
request.params.userId = userIdx;
}

if (request.method === "POST") {
request.body.userId = userIdx;
}
} catch (err) {}
}

Expand Down
1 change: 1 addition & 0 deletions server/src/common/repositories/recruit.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class RecruitRepository extends Repository<Recruit> {
.innerJoinAndSelect("recruit.user", "user")
.select([
"recruit.title AS title",
"recruit.startTime AS startTime",
"recruit.maxPpl AS maxPpl",
"recruit.pace AS pace",
"recruit.userId AS authorId",
Expand Down
71 changes: 45 additions & 26 deletions server/src/common/response/response.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,75 @@ import { ApiProperty } from "@nestjs/swagger";
import { ResponseStatus } from "src/common/response/response.status";
import { Exclude, Expose } from "class-transformer";

export class ResponseEntity<T> {
@Exclude() private readonly _statusCode: string;
@Exclude() private readonly _message: string;
@Exclude() private readonly _data: T;
export class ResponseEntity {
@Exclude() private readonly _statusCode: number;
@Exclude() private readonly _data?: any;
@Exclude() private readonly _error?: string;
@Exclude() private readonly _message?: string;

private constructor(status: ResponseStatus, message: string, data: T) {
this._statusCode = ResponseStatus[status];
private constructor(status: ResponseStatus);
private constructor(status: ResponseStatus, data: any);
private constructor(status: ResponseStatus, data: any, error: string, message: string);
private constructor(status?: ResponseStatus, data?: any, error?: string, message?: string) {
this._statusCode = status;
this._data = Array.isArray(data) ? [...data] : typeof data === "object" ? { ...data } : undefined;
this._error = error;
this._message = message;
this._data = data;
}

static OK(): ResponseEntity<string> {
return new ResponseEntity<string>(ResponseStatus.OK, "", "");
static OK(): ResponseEntity {
return new ResponseEntity(ResponseStatus.OK);
}

static OK_WITH<T>(data: T): ResponseEntity<T> {
return new ResponseEntity<T>(ResponseStatus.OK, "", data);
static OK_WITH_DATA(data: any): ResponseEntity {
return new ResponseEntity(ResponseStatus.OK, data);
}

static ERROR(): ResponseEntity<string> {
return new ResponseEntity<string>(ResponseStatus.SERVER_ERROR, "서버 에러가 발생했습니다.", "");
static CREATED(): ResponseEntity {
return new ResponseEntity(ResponseStatus.CREATED);
}

static ERROR_WITH(message: string, code: ResponseStatus = ResponseStatus.SERVER_ERROR): ResponseEntity<string> {
return new ResponseEntity<string>(code, message, "");
static CREATED_WITH_DATA(data: any): ResponseEntity {
return new ResponseEntity(ResponseStatus.CREATED, data);
}

static ERROR_WITH_DATA<T>(
message: string,
code: ResponseStatus = ResponseStatus.SERVER_ERROR,
data: T,
): ResponseEntity<T> {
return new ResponseEntity<T>(code, message, data);
static BAD_REQUEST(message: string): ResponseEntity {
return new ResponseEntity(ResponseStatus.BAD_REQUEST, undefined, "Bad Request", message);
}

static UNAUTHORIZED(message = "로그인이 필요한 서비스입니다"): ResponseEntity {
return new ResponseEntity(ResponseStatus.UNAUTHORIZED, undefined, "UnAuthorized", message);
}

static NOT_FOUND(message: string): ResponseEntity {
return new ResponseEntity(ResponseStatus.NOT_FOUND, undefined, "Not Found", message);
}

static LOCKED(message: string): ResponseEntity {
return new ResponseEntity(ResponseStatus.LOCKED, undefined, "Locked", message);
}

@ApiProperty()
@Expose()
get statusCode(): string {
get statusCode(): number {
return this._statusCode;
}

@ApiProperty()
@Expose()
get message(): string {
return this._message;
get data(): any {
return this._data;
}

@ApiProperty()
@Expose()
get data(): T {
return this._data;
get error(): string {
return this._error;
}

@ApiProperty()
@Expose()
get message(): string {
return this._message;
}
}
6 changes: 4 additions & 2 deletions server/src/common/response/response.status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export enum ResponseStatus {
OK = 200,
CREATED = 201,
BAD_PARAMETER = 400,
SERVER_ERROR = 500,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
NOT_FOUND = 404,
LOCKED = 423,
}
4 changes: 2 additions & 2 deletions server/src/course/course.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Body, Controller, Get, NotFoundException, Param, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { CourseService } from "./course.service";
import { CreateCourseDto } from "./dto/create-course.dto";
import { GetCourseDto } from "./dto/get-course.dto";
import { CreateCourseDto } from "./dto/request/create-course.dto";
import { GetCourseDto } from "./dto/request/get-course.dto";

@Controller("course")
@ApiTags("코스 관리")
Expand Down
4 changes: 2 additions & 2 deletions server/src/course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Injectable } from "@nestjs/common";
import { Course } from "src/common/entities/course.entity";
import { HDong } from "src/common/entities/h_dong.entity";
import { CourseRepository } from "../common/repositories/course.repository";
import { CreateCourseDto } from "./dto/create-course.dto";
import { GetCourseDto } from "./dto/get-course.dto";
import { CreateCourseDto } from "./dto/request/create-course.dto";
import { GetCourseDto } from "./dto/request/get-course.dto";

@Injectable()
export class CourseService {
Expand Down
45 changes: 45 additions & 0 deletions server/src/course/dto/response/get-one.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { instanceToPlain, plainToInstance, Expose, Type } from "class-transformer";
import { Course } from "src/common/entities/course.entity";

class LatLng {
@Expose()
lat: number;

@Expose()
lng: number;
}

class HDong {
@Expose()
name: string;
}

export class CourseResponseDto {
@Expose()
id: number;

@Expose()
title: string;

@Expose()
@Type(() => LatLng)
path: LatLng[];

@Expose()
pathLength: number;

@Expose()
userId: string;

@Expose()
@Type(() => HDong)
hDong: HDong;

@Expose()
createdAt: Date;

static fromEntity(course: Course) {
const data = instanceToPlain(course);
return plainToInstance(CourseResponseDto, data, { excludeExtraneousValues: true });
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";
import { instanceToPlain, Expose, plainToInstance } from "class-transformer";
import { Recruit } from "../../../common/entities/recruit.entity";

export class CreateRecruitResDto {
export class CreateResponseDto {
@Expose({ name: "id" })
private recruitId: number;

static fromEntity(recruit: Recruit): CreateRecruitResDto {
static fromEntity(recruit: Recruit): CreateResponseDto {
const data = instanceToPlain(recruit);
return plainToInstance(CreateRecruitResDto, data, { excludeExtraneousValues: true });
return plainToInstance(CreateResponseDto, data, { excludeExtraneousValues: true });
}
}
37 changes: 37 additions & 0 deletions server/src/recruit/dto/response/get-many.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { instanceToPlain, Expose, plainToInstance, Type } from "class-transformer";
import { CourseResponseDto } from "src/course/dto/response/get-one.response";

export class GetManyResponseDto {
@Expose()
id: number;

@Expose()
title: string;

@Expose()
startTime: Date;

@Expose()
maxPpl: number;

@Expose()
currentPpl: string;

@Expose()
userId: number;

@Expose()
createdAt: Date;

@Expose()
pace: number;

@Expose()
@Type(() => CourseResponseDto)
course: CourseResponseDto;

static fromEntity(recruit: any): GetManyResponseDto {
const data = instanceToPlain(recruit);
return plainToInstance(GetManyResponseDto, data, { excludeExtraneousValues: true });
}
}
57 changes: 57 additions & 0 deletions server/src/recruit/dto/response/get-one.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { instanceToPlain, Expose, plainToInstance, Type } from "class-transformer";
import { CourseResponseDto } from "src/course/dto/response/get-one.response";

class LatLng {
@Expose()
lat: number;

@Expose()
lng: number;
}

class HDong {
@Expose()
name: string;
}

export class GetOneResponseDto {
@Expose()
title: string;

@Expose()
startTime: Date;

@Expose()
maxPpl: number;

@Expose()
currentPpl: number;

@Expose()
@Type(() => LatLng)
path: LatLng[];

@Expose()
pathLength: number;

@Expose()
pace: number;

@Expose()
@Type(() => HDong)
hDong: HDong;

@Expose()
userId: number;

@Expose()
isParticipating: boolean;

@Expose()
isAuthor: boolean;

static fromEntity(recruit: any): GetOneResponseDto {
const data = instanceToPlain(recruit);
return plainToInstance(GetOneResponseDto, data, { excludeExtraneousValues: true });
}
}
Loading