Skip to content
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

feat(backend)!: 問題の更新処理を修正 #182

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:

- A unique constraint covering the columns `[languageName,languageVersion,problemId]` on the table `Language` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[input,output,problemId]` on the table `TestCase` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "Language_languageName_languageVersion_problemId_key" ON "Language"("languageName", "languageVersion", "problemId");

-- CreateIndex
CREATE UNIQUE INDEX "TestCase_input_output_problemId_key" ON "TestCase"("input", "output", "problemId");
4 changes: 4 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ model TestCase {
results TestResult[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([input, output, problemId])
}

model TestResult {
Expand Down Expand Up @@ -89,6 +91,8 @@ model Language {
problemId Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([languageName, languageVersion, problemId])
}

model Submission {
Expand Down
45 changes: 41 additions & 4 deletions backend/src/api/components/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import { z } from "@hono/zod-openapi"

export const SupportedLanguage = z
.object({
name: z.string(),
version: z.string(),
})
.openapi("SupportedLanguage")

export const Language = z
.object({
id: z.number().int().nonnegative(),
name: z.string(),
version: z.string(),
})
.openapi("Language")

export const LanguageCreate = Language.omit({ id: true }).openapi(
"LanguageCreate",
)

export const LanguageUpdate = Language.openapi("LanguageUpdate")

export const TestCase = z
.object({
id: z.number().int().nonnegative(),
input: z.string(),
output: z.string(),
})
.openapi("TestCase")

export const TestCaseCreate = TestCase.omit({ id: true }).openapi(
"TestCaseCreate",
)

export const TestCaseUpdate = TestCase.partial({
input: true,
output: true,
}).openapi("TestCaseUpdate")

export const Problem = z
.object({
body: z.string(),
Expand All @@ -24,10 +48,23 @@ export const Problem = z
})
.openapi("Problem")

export const ProblemCreate = Problem.omit({ id: true }).openapi("ProblemCreate")
export const ProblemCreate = Problem.omit({ id: true })
.merge(
z.object({
supported_languages: z.array(LanguageCreate),
test_cases: z.array(TestCaseCreate),
} satisfies Partial<Record<keyof z.infer<typeof Problem>, unknown>>),
)
.openapi("ProblemCreate")

export const ProblemUpdate = Problem.partial()
.omit({ id: true })
export const ProblemUpdate = Problem.omit({ id: true })
.merge(
z.object({
supported_languages: z.array(LanguageUpdate),
test_cases: z.array(TestCaseUpdate),
} satisfies Partial<Record<keyof z.infer<typeof Problem>, unknown>>),
)
.partial()
.openapi("ProblemUpdate")

export const SubmissionStatus = z
Expand Down Expand Up @@ -55,7 +92,7 @@ export const Submission = z
.object({
code: z.string(),
id: z.number().int().nonnegative(),
language: Language,
language: SupportedLanguage,
problem_id: z.number().int().nonnegative(),
result: SubmissionResult,
student_id: z.number().int().nonnegative(),
Expand Down
135 changes: 83 additions & 52 deletions backend/src/api/paths/problems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,26 @@ app.openapi(getProblemsRoute, async (c) => {
},
})

const formattedProblems = problems.map((problem) => ({
body: problem.body,
id: problem.id,
supported_languages: problem.supportedLanguages.map((supportedLang) => ({
name: supportedLang.language.name,
version: supportedLang.language.version,
})),
test_cases: problem.testCases.map((testCase) => ({
input: testCase.input,
output: testCase.output,
})),
title: problem.title,
}))
const formattedProblems = problems.map(
(problem) =>
({
body: problem.body,
id: problem.id,
supported_languages: problem.supportedLanguages.map(
({ id, language }) => ({
id: id,
name: language.name,
version: language.version,
}),
),
test_cases: problem.testCases.map(({ id, input, output }) => ({
id,
input,
output,
})),
title: problem.title,
}) satisfies z.infer<typeof schemas.Problem>,
)

return c.json(formattedProblems, 200)
})
Expand All @@ -265,11 +272,8 @@ app.openapi(createProblemRoute, async (c) => {
create: data.supported_languages.map(
(lang: { name: string; version: string }) => ({
language: {
connectOrCreate: {
create: { name: lang.name, version: lang.version },
where: {
name_version: { name: lang.name, version: lang.version },
},
connect: {
name_version: { name: lang.name, version: lang.version },
},
},
}),
Expand Down Expand Up @@ -299,15 +303,17 @@ app.openapi(createProblemRoute, async (c) => {
body: createdProblem.body,
id: createdProblem.id,
supported_languages: createdProblem.supportedLanguages.map((lang) => ({
id: lang.id,
name: lang.language.name,
version: lang.language.version,
})),
test_cases: createdProblem.testCases.map((testCase) => ({
id: testCase.id,
input: testCase.input,
output: testCase.output,
})),
title: createdProblem.title,
}
} satisfies z.infer<typeof schemas.Problem>
return c.json(formattedProblem, 201)
})

Expand All @@ -330,17 +336,19 @@ app.openapi(getProblemRoute, async (c) => {
body: problem.body,
id: problem.id,
supported_languages: problem.supportedLanguages.map(
({ languageName, languageVersion }) => ({
name: languageName,
version: languageVersion,
({ id, language }) => ({
id,
name: language.name,
version: language.version,
}),
),
test_cases: problem.testCases.map(({ input, output }) => ({
test_cases: problem.testCases.map(({ id, input, output }) => ({
id,
input,
output,
})),
title: problem.title,
},
} satisfies z.infer<typeof schemas.Problem>,
200,
)
})
Expand All @@ -352,26 +360,44 @@ app.openapi(updateProblemRoute, async (c) => {
data: {
body: data.body,
supportedLanguages: {
create: data.supported_languages?.map(
(lang: { name: string; version: string }) => ({
upsert: data.supported_languages?.map((lang) => ({
create: {
language: {
connectOrCreate: {
create: { name: lang.name, version: lang.version },
where: {
name_version: { name: lang.name, version: lang.version },
connect: {
name_version: {
name: lang.name,
version: lang.version,
},
},
},
}),
),
},
update: {
language: {
connect: {
name_version: {
name: lang.name,
version: lang.version,
},
},
},
},
where: {
id: lang.id,
problemId,
},
})),
},
testCases: {
create: data.test_cases?.map(
(testCase: { input: string; output: string }) => ({
update: data.test_cases?.map((testCase) => ({
data: {
input: testCase.input,
output: testCase.output,
}),
),
},
where: {
id: testCase.id,
problemId,
},
})),
},
title: data.title,
},
Expand All @@ -383,23 +409,28 @@ app.openapi(updateProblemRoute, async (c) => {
},
testCases: true,
},
where: {
id: problemId,
},
where: { id: problemId },
})
return c.json({
body: updatedProblem.body,
id: updatedProblem.id,
supported_languages: updatedProblem.supportedLanguages.map((lang) => ({
name: lang.language.name,
version: lang.language.version,
})),
test_cases: updatedProblem.testCases.map((testCase) => ({
input: testCase.input,
output: testCase.output,
})),
title: updatedProblem.title,
} satisfies z.infer<typeof schemas.Problem>)
return c.json(
{
body: updatedProblem.body,
id: updatedProblem.id,
supported_languages: updatedProblem.supportedLanguages.map(
({ id, language }) => ({
id,
name: language.name,
version: language.version,
}),
),
test_cases: updatedProblem.testCases.map(({ id, input, output }) => ({
id,
input,
output,
})),
title: updatedProblem.title,
} satisfies z.infer<typeof schemas.Problem>,
200,
)
})

app.openapi(deleteProblemRoute, async (c) => {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/program/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "@hono/zod-openapi"
import * as schemas from "../../api/components/schemas"
import { runPythonCode } from "./languages"

type Language = z.infer<typeof schemas.Language>
type SupportedLanguage = z.infer<typeof schemas.SupportedLanguage>

export const run = ({
code,
Expand All @@ -12,7 +12,7 @@ export const run = ({
}: {
code: string
input: string
language: Language
language: SupportedLanguage
}) => {
switch (language.name) {
case "Python": {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/program/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "@hono/zod-openapi"
import * as schemas from "../../api/components/schemas"
import { run } from "./run"

type Language = z.infer<typeof schemas.Language>
type SupportedLanguage = z.infer<typeof schemas.SupportedLanguage>

type TestResult = Omit<z.infer<typeof schemas.TestResult>, "test_case_id">

Expand All @@ -15,7 +15,7 @@ export const test = async ({
}: {
code: string
input: string
language: Language
language: SupportedLanguage
output: string
}): Promise<TestResult> => {
const result = await run({ code, input, language })
Expand Down
Loading