Skip to content

Commit

Permalink
Refact: 토익 문제 조회 API 수정
Browse files Browse the repository at this point in the history
- 토익 문제 조회 API에서 Query 파라미터 옵션 추가
- 기본으로 공개된 문제만 조회하도록 변경

Related to #35
  • Loading branch information
Zamoca42 committed Feb 25, 2024
1 parent a933321 commit 86fae1e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
24 changes: 24 additions & 0 deletions packages/backend/src/toeic/dto/req-toeic-query.dto.ts
Original file line number Diff line number Diff line change
@@ -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),
};
}
}
6 changes: 4 additions & 2 deletions packages/backend/src/toeic/toeic.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ParseIntPipe,
ParseUUIDPipe,
Patch,
Query,
} from '@nestjs/common';
import { ToeicService } from './toeic.service';
import { ApiSwagger } from '../common/swagger/api.decorator';
Expand All @@ -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')
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/toeic/toeic.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export interface PatchQuestionToEntity
}[];
};
}

export interface ToeicWhereInputProps {
is_public: boolean;
}
6 changes: 3 additions & 3 deletions packages/backend/src/toeic/toeic.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 86fae1e

Please sign in to comment.