Skip to content

Commit 39e358a

Browse files
committed
refactor: validate query string by using dto class-validator
1 parent e5c843d commit 39e358a

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

Diff for: src/rotation/dto/find-rotation-query.dto.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsNumber, IsOptional } from 'class-validator';
2+
import { Transform } from 'class-transformer';
3+
import { IsNumber, IsOptional, Max, Min } from 'class-validator';
34

45
export class FindRotationQueryDto {
6+
private currentDate = new Date();
7+
8+
@Transform((params) => +params.value)
59
@ApiProperty({
10+
required: false,
611
example: 1,
712
description: '로테이션 정보를 찾는 기준이 되는 월. 기본값은 다음 달 월입니다.',
813
})
914
@IsOptional()
1015
@IsNumber()
11-
month?: number;
16+
@Min(1)
17+
@Max(12)
18+
month?: number = this.currentDate.getMonth() + 1;
1219

20+
@Transform((params) => +params.value)
1321
@ApiProperty({
22+
required: false,
1423
example: 2024,
1524
description: '로테이션 정보를 찾는 기준이 되는 연도. 기본값은 다음 달 연도입니다.',
1625
})
1726
@IsOptional()
1827
@IsNumber()
19-
year?: number;
28+
@Min(2020)
29+
@Max(2100)
30+
year?: number = this.currentDate.getFullYear();
2031
}

Diff for: src/rotation/rotations.controller.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ import {
4141
} from 'src/common/dto/error-response.dto';
4242
import { FindTodayRotationDto } from './dto/find-today-rotation.dto';
4343
import { FindRegistrationDto } from './dto/find-registration.dto';
44-
import { MonthValidationPipe } from './pipe/month-validation.pipe';
4544
import { FindAllRotationDto } from './dto/find-all-rotation.dto';
46-
import { YearValidationPipe } from './pipe/year-validation.pipe';
45+
import { FindRotationQueryDto } from './dto/find-rotation-query.dto';
4746

4847
@Controller('rotations')
4948
@ApiTags('rotations')
@@ -157,9 +156,9 @@ export class RotationsController {
157156
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
158157
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
159158
findAllRotation(
160-
@Query('year', new YearValidationPipe()) year: any,
161-
@Query('month', new MonthValidationPipe()) month: any,
159+
@Query() findRotationQueryDto: FindRotationQueryDto,
162160
): Promise<FindAllRotationDto[]> {
161+
const { year, month } = findRotationQueryDto;
163162
return this.rotationsService.findAllRotation(year, month);
164163
}
165164

Diff for: src/rotation/rotations.service.ts

+7-22
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,13 @@ export class RotationsService {
351351
* 기본적으로는 모든 로테이션을 반환.
352352
* 만약 parameter로 month와 year가 들어오면, 해당 스코프에 맞는 레코드를 반환.
353353
*/
354-
async findAllRotation(year?: number, month?: number): Promise<FindAllRotationDto[]> {
355-
let records: Promise<RotationEntity[]>;
356-
357-
if (!year && !month) {
358-
records = this.rotationRepository.find();
359-
} else {
360-
const currentDate = new Date();
361-
if (!year) {
362-
year = currentDate.getFullYear();
363-
} else if (!month) {
364-
month = currentDate.getMonth() + 1;
365-
} else {
366-
/* both inputs are exists */
367-
}
368-
369-
records = this.rotationRepository.find({
370-
where: {
371-
year: year,
372-
month: month,
373-
},
374-
});
375-
}
354+
async findAllRotation(year: number, month: number): Promise<FindAllRotationDto[]> {
355+
const records = this.rotationRepository.find({
356+
where: {
357+
year: year,
358+
month: month,
359+
},
360+
});
376361

377362
const modifiedRecords = await Promise.all(
378363
(await records).map(async (record) => {

0 commit comments

Comments
 (0)