judge4c/src/app/actions/getProblem.ts
fly6516 cf09a15893 feat(creater): realise problem-editor interactive with database logic
- 在 edit-code-panel、edit-description-panel、edit-detail-panel、edit-solution-panel 和 edit-testcase-panel 组件中添加保存逻辑
- 实现与后端 API 的交互,包括保存代码模板、题目描述、详情、解析和测试用例
-优化错误处理和用户提示,使用 toast 组件显示操作结果
- 调整界面布局和交互细节,提升用户体验
2025-06-21 12:43:16 +08:00

64 lines
1.8 KiB
TypeScript

// app/actions/get-problem-data.ts
'use server';
import prisma from '@/lib/prisma';
import { Locale } from '@/generated/client';
import { serialize } from 'next-mdx-remote/serialize';
export async function getProblemData(problemId: string, locale?: string) {
const selectedLocale = locale as Locale;
const problem = await prisma.problem.findUnique({
where: { id: problemId },
include: {
templates: true,
testcases: {
include: { inputs: true }
},
localizations: {
where: {
locale: selectedLocale,
},
},
},
});
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,
mdxDescription,
solution: getContent('SOLUTION'),
templates: problem.templates.map(t => ({
language: t.language,
content: t.content,
})),
testcases: problem.testcases.map(tc => ({
id: tc.id,
expectedOutput: tc.expectedOutput,
inputs: tc.inputs.map(input => ({
name: input.name,
value: input.value,
})),
})),
};
}