Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apps/server/src/problems/validation/handler-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { ValidationHandler } from './handlers/validation.handler';
import { UnitValidationHandler } from './handlers/unit-validation.handler';

@Injectable()
export class HandlerResolver {
private handlers: Map<string, ValidationHandler>;

constructor(private readonly unitHandler: UnitValidationHandler) {
this.handlers = new Map([['unit', this.unitHandler]]);
}

resolve(problemType: string): ValidationHandler {
const handler = this.handlers.get(problemType);
if (!handler) {
throw new Error(`${problemType} 에 대한 ValidationHandler가 없습니다.`);
}
return handler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ValidationHandler } from './validation.handler';
import { SubmitResponseDto } from 'src/problems/dto/submit-response.dto';
import { ConfigDto } from 'src/problems/dto/submit-request.dto';

export class UnitValidationHandler implements ValidationHandler {
support(problemType: string): boolean {
return 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: '틀렸습니다.',
},
],
};
}
}
13 changes: 13 additions & 0 deletions apps/server/src/problems/validation/validation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { ConfigDto } from '../dto/submit-request.dto';
import { HandlerResolver } from './handler-resolver';

@Injectable()
export class ValidationService {
constructor(private readonly handlerResolver: HandlerResolver) {}

validate(problemType: string, submitConfig: ConfigDto, problemData: any) {
const handler = this.handlerResolver.resolve(problemType);
return handler.validate(submitConfig, problemData);
}
}