monaco-editor-lsp-next/src/app/actions/getProblem.ts

64 lines
1.9 KiB
TypeScript
Raw Normal View History

// app/actions/get-problem-data.ts
'use server';
import prisma from '@/lib/prisma';
import { Locale } from '@/generated/client'; // ✅ 导入 enum Locale
import { serialize } from 'next-mdx-remote/serialize';
export async function getProblemData(problemId: string, locale: string) {
const selectedLocale = locale as Locale; // ✅ 强制转换 string 为 Prisma enum
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,
})),
})),
};
}