2025-06-17 07:24:43 +00:00
|
|
|
// app/actions/get-problem-data.ts
|
|
|
|
'use server';
|
|
|
|
|
|
|
|
import prisma from '@/lib/prisma';
|
2025-06-17 08:18:59 +00:00
|
|
|
import { Locale } from '@/generated/client'; // ✅ 导入 enum Locale
|
2025-06-17 07:24:43 +00:00
|
|
|
import { serialize } from 'next-mdx-remote/serialize';
|
|
|
|
|
2025-06-17 08:18:59 +00:00
|
|
|
export async function getProblemData(problemId: string, locale: string) {
|
|
|
|
const selectedLocale = locale as Locale; // ✅ 强制转换 string 为 Prisma enum
|
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
const problem = await prisma.problem.findUnique({
|
|
|
|
where: { id: problemId },
|
|
|
|
include: {
|
|
|
|
templates: true,
|
|
|
|
testcases: {
|
|
|
|
include: { inputs: true }
|
2025-06-17 08:18:59 +00:00
|
|
|
},
|
|
|
|
localizations: {
|
|
|
|
where: {
|
|
|
|
locale: selectedLocale, // ✅ 这里使用枚举
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2025-06-17 07:24:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!problem) {
|
|
|
|
throw new Error('Problem not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const getContent = (type: string) =>
|
|
|
|
problem.localizations.find(loc => loc.type === type)?.content || '';
|
|
|
|
|
|
|
|
const rawDescription = getContent('DESCRIPTION');
|
|
|
|
|
|
|
|
const mdxDescription = await serialize(rawDescription, {
|
|
|
|
parseFrontmatter: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: problem.id,
|
|
|
|
displayId: problem.displayId,
|
|
|
|
difficulty: problem.difficulty,
|
|
|
|
isPublished: problem.isPublished,
|
|
|
|
timeLimit: problem.timeLimit,
|
|
|
|
memoryLimit: problem.memoryLimit,
|
|
|
|
title: getContent('TITLE'),
|
|
|
|
description: rawDescription,
|
2025-06-17 08:18:59 +00:00
|
|
|
mdxDescription,
|
2025-06-17 07:24:43 +00:00
|
|
|
solution: getContent('SOLUTION'),
|
|
|
|
templates: problem.templates.map(t => ({
|
|
|
|
language: t.language,
|
2025-06-17 08:18:59 +00:00
|
|
|
content: t.content,
|
2025-06-17 07:24:43 +00:00
|
|
|
})),
|
|
|
|
testcases: problem.testcases.map(tc => ({
|
|
|
|
id: tc.id,
|
|
|
|
expectedOutput: tc.expectedOutput,
|
|
|
|
inputs: tc.inputs.map(input => ({
|
|
|
|
name: input.name,
|
2025-06-17 08:18:59 +00:00
|
|
|
value: input.value,
|
|
|
|
})),
|
|
|
|
})),
|
2025-06-17 07:24:43 +00:00
|
|
|
};
|
|
|
|
}
|