Files
yfan 3d175d75af
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
Sync to CNB / sync (push) Has been cancelled
Delete old workflow runs / del_runs (push) Has been cancelled
Upstream Sync / Sync latest commits from upstream repo (push) Has been cancelled
first commit
2026-01-30 16:57:44 +08:00

76 lines
2.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 }
);
}
}