Skip to content

Commit

Permalink
feat: 대회 문제 상세 조회 API에 테스트케이스 정보 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yechan2468 committed Dec 5, 2023
1 parent df834ba commit 342b573
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';

export type TestcaseParameterMetadata = { name: string; type: string };
export type TestcaseData = { input: any[]; output: any };
export interface ITestcases {
input: TestcaseParameterMetadata[];
output: TestcaseParameterMetadata;
data: TestcaseData[];
}

export class CompetitionProblemResponseDto {
constructor(
id: number,
Expand All @@ -8,7 +16,7 @@ export class CompetitionProblemResponseDto {
memoryLimit: number,
content: string,
solutionCode: string,
testcases: object[],
testcases: ITestcases,
createdAt: Date,
) {
this.id = id;
Expand Down Expand Up @@ -40,7 +48,7 @@ export class CompetitionProblemResponseDto {
solutionCode: string;

@ApiProperty({ description: '공개 테스트 케이스' })
testcases: object[];
testcases: ITestcases;

@ApiProperty()
createdAt: Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ import { Server } from 'socket.io';
import { DataSource, Repository } from 'typeorm';

import { existsSync, readFileSync } from 'fs';
import * as fs from 'fs';
import * as path from 'path';

import { ProblemService } from './problem.service';
import { RESULT } from '../competition.enums';
import { CompetitionProblemResponseDto } from '../dto/competition.problem.response.dto';
import {
CompetitionProblemResponseDto,
ITestcases,
TestcaseData,
} from '../dto/competition.problem.response.dto';
import { CreateSubmissionDto } from '../dto/create-submission.dto';
import { ProblemSimpleResponseDto } from '../dto/problem.simple.response.dto';
import { ScoreResultDto } from '../dto/score-result.dto';
Expand Down Expand Up @@ -167,18 +172,51 @@ export class CompetitionService {

async findOneProblem(id: number) {
const problem = await this.problemRepository.findOneBy({ id });

const fileName = id.toString() + '.md';
const paths = path.join(process.env.PROBLEM_PATH, fileName);
if (!existsSync(paths)) throw new NotFoundException('문제 파일을 찾을 수 없습니다.');
const content = readFileSync(paths).toString();

const metadataPath = path.join(process.env.TESTCASE_PATH, problem.id.toString(), 'metadata');
let metadata;
if (!existsSync(metadataPath)) {
console.warn('문제에 대한 테스트케이스 메타데이터 파일을 찾을 수 없습니다.');
metadata = {
input: [],
output: null,
sampleTestcaseNum: 0,
};
} else {
metadata = JSON.parse(fs.readFileSync(metadataPath).toString());
}
const data: TestcaseData[] = [];
for (let i = 1; i <= metadata.sampleTestcaseNum; i++) {
const filename = path.join(
process.env.TESTCASE_PATH,
problem.id.toString(),
'samples',
i.toString(),
);
data.push({
input: JSON.parse(fs.readFileSync(`${filename}.in`).toString()),
output: JSON.parse(fs.readFileSync(`${filename}.ans`).toString()),
});
}
const testcases: ITestcases = {
input: metadata.input,
output: metadata.output,
data,
};

return new CompetitionProblemResponseDto(
problem.id,
problem.title,
problem.timeLimit,
problem.memoryLimit,
content,
problem.solutionCode,
[{ temp: '임시' }],
testcases,
problem.createdAt,
);
}
Expand Down

0 comments on commit 342b573

Please sign in to comment.