From 86fae1ed41d9732668a8e3e1fb95343a74d998cb Mon Sep 17 00:00:00 2001 From: Chooooooo Date: Fri, 23 Feb 2024 21:43:40 +0900 Subject: [PATCH] =?UTF-8?q?Refact:=20=ED=86=A0=EC=9D=B5=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=A1=B0=ED=9A=8C=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 토익 문제 조회 API에서 Query 파라미터 옵션 추가 - 기본으로 공개된 문제만 조회하도록 변경 Related to #35 --- .../src/toeic/dto/req-toeic-query.dto.ts | 24 +++++++++++++++++++ .../backend/src/toeic/toeic.controller.ts | 6 +++-- packages/backend/src/toeic/toeic.interface.ts | 4 ++++ packages/backend/src/toeic/toeic.service.ts | 6 ++--- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 packages/backend/src/toeic/dto/req-toeic-query.dto.ts diff --git a/packages/backend/src/toeic/dto/req-toeic-query.dto.ts b/packages/backend/src/toeic/dto/req-toeic-query.dto.ts new file mode 100644 index 0000000..1e2e426 --- /dev/null +++ b/packages/backend/src/toeic/dto/req-toeic-query.dto.ts @@ -0,0 +1,24 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsOptional, Max, Min } from 'class-validator'; +import { ToeicWhereInputProps } from '../toeic.interface'; +import { Type } from 'class-transformer'; + +export class ToeicQueryParam { + @ApiProperty({ + description: '문제 공개 여부 (0: 비공개, 1: 공개)', + required: false, + default: 1, + }) + @Min(0) + @Max(1) + @IsOptional() + @Type(() => Number) + isPublic: number; + + getQueryProps(): ToeicWhereInputProps { + return { + is_public: + typeof this.isPublic === 'undefined' ? true : Boolean(this.isPublic), + }; + } +} diff --git a/packages/backend/src/toeic/toeic.controller.ts b/packages/backend/src/toeic/toeic.controller.ts index 4103d85..8ca4411 100644 --- a/packages/backend/src/toeic/toeic.controller.ts +++ b/packages/backend/src/toeic/toeic.controller.ts @@ -7,6 +7,7 @@ import { ParseIntPipe, ParseUUIDPipe, Patch, + Query, } from '@nestjs/common'; import { ToeicService } from './toeic.service'; import { ApiSwagger } from '../common/swagger/api.decorator'; @@ -15,6 +16,7 @@ import { PatchToeicWithQuestion } from './dto/patch-quesion.dto'; import { ApiTags } from '@nestjs/swagger'; import { Role } from '../auth/constant/roles.enum'; import { Roles } from '../auth/decorator/roles.decorator'; +import { ToeicQueryParam } from './dto/req-toeic-query.dto'; @ApiTags('토익 문제') @Controller('toeic') @@ -23,8 +25,8 @@ export class ToeicController { @Get() @ApiSwagger({ name: '토익 문제 조회' }) - async findAll() { - const result = await this.toeicService.findAllToeic(); + async findAll(@Query() query: ToeicQueryParam) { + const result = await this.toeicService.findAllToeic(query.getQueryProps()); return ResponseEntity.OK_WITH( `Successfully find ${result.length} questsions`, result, diff --git a/packages/backend/src/toeic/toeic.interface.ts b/packages/backend/src/toeic/toeic.interface.ts index bd98316..dc52287 100644 --- a/packages/backend/src/toeic/toeic.interface.ts +++ b/packages/backend/src/toeic/toeic.interface.ts @@ -27,3 +27,7 @@ export interface PatchQuestionToEntity }[]; }; } + +export interface ToeicWhereInputProps { + is_public: boolean; +} diff --git a/packages/backend/src/toeic/toeic.service.ts b/packages/backend/src/toeic/toeic.service.ts index b2d454a..f38c60d 100644 --- a/packages/backend/src/toeic/toeic.service.ts +++ b/packages/backend/src/toeic/toeic.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { UploadedQuestionInSheet } from '../upload/dto/upload.dto'; import { PrismaService } from '../prisma/prisma.service'; -import { PatchQuestionToEntity } from './toeic.interface'; +import { PatchQuestionToEntity, ToeicWhereInputProps } from './toeic.interface'; @Injectable() export class ToeicService { @@ -22,8 +22,8 @@ export class ToeicService { }); } - async findAllToeic() { - return this.prisma.toeic.findMany(); + async findAllToeic(where: ToeicWhereInputProps) { + return this.prisma.toeic.findMany({ where }); } async findToeicUnique(id: number) {