-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/#26 문제 검증 핸들러 패턴 구현 #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
203be82
add: 파일 구조 추가
vvzvvv e22402f
feat: 검증 핸들러 인터페이스 추가
vvzvvv f7b9012
feat: 문제 검증 로직 구현 (타입별 검증)
vvzvvv daf9c84
refactor: ProblemsService가 ValidationService를 사용하도록 변경
vvzvvv 4b82796
feat: 문제 모듈에 검증 서비스 및 핸들러 추가
vvzvvv d82c74a
refactor: 문제 유형인 ProblemType 을 string 에서 enum 으로 변경
vvzvvv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { ProblemsController } from './problems.controller'; | ||
| import { ProblemsService } from './problems.service'; | ||
| import { ValidationService } from './validation/validation.service'; | ||
| import { HandlerResolver } from './validation/handler-resolver'; | ||
| import { UnitValidationHandler } from './validation/handlers/unit-validation.handler'; | ||
|
|
||
| @Module({ | ||
| controllers: [ProblemsController], | ||
| providers: [ProblemsService], | ||
| providers: [ | ||
| ProblemsService, | ||
| ValidationService, | ||
| HandlerResolver, | ||
| UnitValidationHandler, | ||
| ], | ||
| }) | ||
| export class ProblemsModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,26 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { SubmitRequestDto } from './dto/submit-request.dto'; | ||
| import { SubmitResponseDto } from './dto/submit-response.dto'; | ||
| import { ValidationService } from './validation/validation.service'; | ||
| import { ProblemType } from './types/problem-type.enum'; | ||
|
|
||
| @Injectable() | ||
| export class ProblemsService { | ||
| constructor(private readonly validationService: ValidationService) {} | ||
|
|
||
| submit(problemId: number, body: SubmitRequestDto): SubmitResponseDto { | ||
| const problem_type = body.submitConfig?.[0]; | ||
| const answer = (problem_type?.configInfo?.answer ?? '') as string; | ||
| // MOCK 문제 데이터 | ||
| const problemData = { | ||
| problemType: ProblemType.UNIT, | ||
| answer: '1234', | ||
| }; | ||
|
|
||
| // test: 정답이 1234이면 PASS, 아니면 FAIL | ||
| const isCorrect = answer === '1234'; | ||
| const result = this.validationService.validate( | ||
| problemData.problemType, | ||
| body.submitConfig[0], | ||
| problemData, | ||
| ); | ||
|
|
||
| return isCorrect | ||
| ? { result: 'PASS', feedback: [] } | ||
| : { | ||
| result: 'FAIL', | ||
| feedback: [ | ||
| { | ||
| field: 'answer', | ||
| code: 'WRONG_ANSWER', | ||
| message: '틀렸습니다.', | ||
| }, | ||
| ], | ||
| }; | ||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export enum ProblemType { | ||
| UNIT = 'unit', | ||
| SCENARIO = 'scenario', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ValidationHandler } from './handlers/validation.handler'; | ||
| import { UnitValidationHandler } from './handlers/unit-validation.handler'; | ||
| import { ProblemType } from '../types/problem-type.enum'; | ||
|
|
||
| @Injectable() | ||
| export class HandlerResolver { | ||
| private handlers: Map<ProblemType, ValidationHandler>; | ||
|
|
||
| constructor(private readonly unitHandler: UnitValidationHandler) { | ||
| this.handlers = new Map([[ProblemType.UNIT, this.unitHandler]]); | ||
| } | ||
|
|
||
| resolve(problemType: ProblemType): ValidationHandler { | ||
| const handler = this.handlers.get(problemType); | ||
| if (!handler) { | ||
| throw new Error(`${problemType} 에 대한 ValidationHandler가 없습니다.`); | ||
| } | ||
| return handler; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
apps/server/src/problems/validation/handlers/unit-validation.handler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { ValidationHandler } from './validation.handler'; | ||
| import { SubmitResponseDto } from 'src/problems/dto/submit-response.dto'; | ||
| import { ConfigDto } from 'src/problems/dto/submit-request.dto'; | ||
| import { ProblemType } from 'src/problems/types/problem-type.enum'; | ||
|
|
||
| export class UnitValidationHandler implements ValidationHandler { | ||
| support(problemType: ProblemType): boolean { | ||
| return problemType === ProblemType.UNIT; | ||
| } | ||
|
|
||
| validate(submitConfig: ConfigDto, problemData: any): SubmitResponseDto { | ||
| // test 용 검증 로직: 제출한 풀이가 problemData.answer와 일치하는지 확인 | ||
| const answer = (submitConfig.configInfo?.answer ?? '') as string; | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
| const expectedAnswer = problemData.answer as string; | ||
|
|
||
| if (answer === expectedAnswer) { | ||
| return { result: 'PASS', feedback: [] }; | ||
| } | ||
|
|
||
| return { | ||
| result: 'FAIL', | ||
| feedback: [ | ||
| { | ||
| field: 'answer', | ||
| code: 'WRONG_ANSWER', | ||
| message: '틀렸습니다.', | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
apps/server/src/problems/validation/handlers/validation.handler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { ConfigDto } from 'src/problems/dto/submit-request.dto'; | ||
| import { SubmitResponseDto } from 'src/problems/dto/submit-response.dto'; | ||
| import { ProblemType } from 'src/problems/types/problem-type.enum'; | ||
|
|
||
| export interface ValidationHandler { | ||
| /** | ||
| * 문제 타입(ex. unit)에 대해 핸들러가 지원되는지 여부를 반환하는 메서드 | ||
| * @param problemType 문제 타입 | ||
| * @returns 지원 여부 (true/false) | ||
| */ | ||
| support(problemType: ProblemType): boolean; | ||
|
|
||
| /** | ||
| * 제출된 풀이를 검증하고 결과를 반환하는 메서드 | ||
| * @param submitConfig 제출된 풀이 객체 | ||
| * @param problemData 문제의 메타데이터 (우선은 이렇게 둠) | ||
| * @returns 검증 결과 객체 | ||
| */ | ||
| validate(submitConfig: ConfigDto, problemData: any): SubmitResponseDto; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { ConfigDto } from '../dto/submit-request.dto'; | ||
| import { HandlerResolver } from './handler-resolver'; | ||
| import { ProblemType } from '../types/problem-type.enum'; | ||
|
|
||
| @Injectable() | ||
| export class ValidationService { | ||
| constructor(private readonly handlerResolver: HandlerResolver) {} | ||
|
|
||
| validate( | ||
| problemType: ProblemType, | ||
| submitConfig: ConfigDto, | ||
| problemData: any, | ||
| ) { | ||
| const handler = this.handlerResolver.resolve(problemType); | ||
| return handler.validate(submitConfig, problemData); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.