first commit
Some checks failed
Some checks failed
This commit is contained in:
75
app/api/testcase-generator/generate/route.ts
Normal file
75
app/api/testcase-generator/generate/route.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import {
|
||||
createTask,
|
||||
getEstimatedTime,
|
||||
findRunningTaskByEnglishName,
|
||||
processTask,
|
||||
} from "@/lib/testcase-backend";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const {
|
||||
title,
|
||||
englishName,
|
||||
description,
|
||||
standardCode,
|
||||
serviceLevel = "standard",
|
||||
testCaseCount: rawCount,
|
||||
} = body;
|
||||
|
||||
if (!title || !englishName || !description || !standardCode) {
|
||||
return Response.json(
|
||||
{ success: false, error: "请填写必填字段:题目标题、题目英文名、题目描述、C++标准程序" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!/^[a-z][a-z0-9_]*$/.test(String(englishName).trim())) {
|
||||
return Response.json(
|
||||
{ success: false, error: "题目英文名格式错误:只能包含小写字母、数字和下划线,必须以字母开头" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const testCaseCount = Math.min(100, Math.max(1, parseInt(String(rawCount), 10) || 10));
|
||||
|
||||
const existing = findRunningTaskByEnglishName(String(englishName));
|
||||
if (existing) {
|
||||
return Response.json(
|
||||
{ success: false, error: "相同英文名的任务正在处理中,请稍后再试或使用不同的英文名" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
const taskId = "task_" + Date.now() + "_" + Math.random().toString(36).slice(2, 11);
|
||||
createTask(taskId, {
|
||||
title: String(title).trim(),
|
||||
englishName: String(englishName).trim().toLowerCase(),
|
||||
description: String(description).trim(),
|
||||
standardCode: String(standardCode).trim(),
|
||||
testCaseCount,
|
||||
serviceLevel: String(serviceLevel),
|
||||
});
|
||||
|
||||
setImmediate(() => {
|
||||
processTask(taskId).catch((err) => {
|
||||
console.error(`[testcase] processTask error:`, err);
|
||||
});
|
||||
});
|
||||
|
||||
const estimated_time = getEstimatedTime(String(serviceLevel), testCaseCount);
|
||||
return Response.json({
|
||||
success: true,
|
||||
task_id: taskId,
|
||||
message: "任务已创建,正在处理中...",
|
||||
estimated_time,
|
||||
});
|
||||
} catch (e) {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
return Response.json(
|
||||
{ success: false, error: "创建任务时发生错误,请重试:" + message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user