diff --git a/backend/src/common/pipe/ParseOptionalNumberPipe.ts b/backend/src/common/pipe/ParseOptionalNumberPipe.ts new file mode 100644 index 00000000..55015de1 --- /dev/null +++ b/backend/src/common/pipe/ParseOptionalNumberPipe.ts @@ -0,0 +1,20 @@ +import { PipeTransform, Injectable } from '@nestjs/common'; + +@Injectable() +export class ParseOptionalNumberPipe implements PipeTransform { + constructor(private readonly defaultValue: number) {} + + transform(value: any): number { + if (value === undefined || value === null) return this.defaultValue; + + const parsedValue = parseInt(value, 10); + if (isNaN(parsedValue)) { + // Todo. class-transformer 가 먼저 적용돼 이미 NaN 으로 변환됨. -> undefined 인 경우와 구분 불가 + // throw new BadRequestException( + // `${metadata.data} 에 올바른 유효한 숫자 값을 입력해주세요.`, + // ); + return this.defaultValue; + } + return parsedValue; + } +} diff --git a/backend/src/course/course.controller.ts b/backend/src/course/course.controller.ts index 4ca0d16d..a80d7432 100644 --- a/backend/src/course/course.controller.ts +++ b/backend/src/course/course.controller.ts @@ -17,6 +17,7 @@ import { SetPlacesOfCourseRequest } from './dto/AddPlaceToCourseRequest'; import { JwtAuthGuard } from '../auth/JwtAuthGuard'; import { AuthUser } from '../auth/AuthUser.decorator'; import { CoursePermissionGuard } from './guards/CoursePermissionGuard'; +import { ParseOptionalNumberPipe } from '@src/common/pipe/ParseOptionalNumberPipe'; @Controller('/courses') export class CourseController { @@ -25,11 +26,9 @@ export class CourseController { @Get() async getCourseList( @Query('query') query?: string, - @Query('page') page?: number, - @Query('limit') limit?: number, + @Query('page', new ParseOptionalNumberPipe(1)) page?: number, + @Query('limit', new ParseOptionalNumberPipe(10)) limit?: number, ) { - if (isNaN(page)) page = 1; // Todo. number 타입 선택적 매개변수일 때 NaN 으로 처리되어 추가. 다른 방법? - if (isNaN(limit)) limit = 10; return await this.courseService.searchPublicCourses(query, page, limit); } diff --git a/backend/src/map/map.controller.ts b/backend/src/map/map.controller.ts index 186168df..4308dad8 100644 --- a/backend/src/map/map.controller.ts +++ b/backend/src/map/map.controller.ts @@ -12,6 +12,7 @@ import { MapService } from './map.service'; import { CreateMapRequest } from './dto/CreateMapRequest'; import { UpdateMapInfoRequest } from './dto/UpdateMapInfoRequest'; import { AddPlaceToMapRequest } from './dto/AddPlaceToMapRequest'; +import { ParseOptionalNumberPipe } from '@src/common/pipe/ParseOptionalNumberPipe'; @Controller('/maps') export class MapController { @@ -20,12 +21,9 @@ export class MapController { @Get() async getMapList( @Query('query') query?: string, - @Query('page') page?: number, - @Query('limit') limit?: number, + @Query('page', new ParseOptionalNumberPipe(1)) page?: number, + @Query('limit', new ParseOptionalNumberPipe(10)) limit?: number, ) { - if (isNaN(page)) page = 1; // Todo. number 타입 선택적 매개변수일 때 NaN 으로 처리되어 추가. 다른 방법? - if (isNaN(limit)) limit = 10; - return await this.mapService.searchMap(query, page, limit); } diff --git a/backend/src/place/place.controller.ts b/backend/src/place/place.controller.ts index 6fcb8117..39891603 100644 --- a/backend/src/place/place.controller.ts +++ b/backend/src/place/place.controller.ts @@ -10,6 +10,7 @@ import { import { PlaceService } from './place.service'; import { CreatePlaceRequest } from './dto/CreatePlaceRequest'; import { JwtAuthGuard } from '../auth/JwtAuthGuard'; +import { ParseOptionalNumberPipe } from '@src/common/pipe/ParseOptionalNumberPipe'; @Controller('places') export class PlaceController { @@ -24,12 +25,9 @@ export class PlaceController { @Get() async getPlaces( @Query('query') query?: string, - @Query('page') page?: number, - @Query('limit') limit?: number, + @Query('page', new ParseOptionalNumberPipe(1)) page?: number, + @Query('limit', new ParseOptionalNumberPipe(5)) limit?: number, ) { - if (isNaN(page)) page = 1; // Todo. number 타입 선택적 매개변수일 때 NaN 으로 처리되어 추가. 다른 방법? - if (isNaN(limit)) limit = 5; - return this.placeService.getPlaces(query, page, limit); }