Skip to content

Commit

Permalink
feat: ParseOptionalNumberPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Miensoap committed Nov 15, 2024
1 parent 3698512 commit b670bea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
20 changes: 20 additions & 0 deletions backend/src/common/pipe/ParseOptionalNumberPipe.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 3 additions & 4 deletions backend/src/course/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
8 changes: 3 additions & 5 deletions backend/src/map/map.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
8 changes: 3 additions & 5 deletions backend/src/place/place.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down

0 comments on commit b670bea

Please sign in to comment.