feat: add word import and class type display controls
Build Docker Package / docker-package (push) Successful in 6m34s
Build Docker Package / docker-package (push) Successful in 6m34s
This commit is contained in:
@@ -15,6 +15,7 @@ export async function PUT(request: NextRequest, context: RouteContext) {
|
||||
const classType = updateClassType(Number(id), {
|
||||
name: String(body.name ?? ""),
|
||||
enabled: Boolean(body.enabled),
|
||||
showCurrentWord: body.showCurrentWord !== false,
|
||||
});
|
||||
|
||||
return NextResponse.json({ classType });
|
||||
@@ -35,7 +36,7 @@ export async function DELETE(_request: NextRequest, context: RouteContext) {
|
||||
|
||||
function getErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "操作失败";
|
||||
if (message.includes("UNIQUE")) return "班级类型已存在";
|
||||
if (message.includes("UNIQUE")) return "该年级下班级类型已存在";
|
||||
if (message.includes("FOREIGN KEY")) return "该班级类型下仍有题库,不能删除";
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createClassType, listClassTypes } from "@/lib/db";
|
||||
import { createClassType, listClassTypes, type Grade } from "@/lib/db";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const includeDisabled = request.nextUrl.searchParams.get("includeDisabled") !== "false";
|
||||
return NextResponse.json({ classTypes: listClassTypes(includeDisabled) });
|
||||
const grade = request.nextUrl.searchParams.get("grade") ?? undefined;
|
||||
return NextResponse.json({
|
||||
classTypes: listClassTypes({
|
||||
grade: parseGrade(grade),
|
||||
includeDisabled,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const classType = createClassType(String(body.name ?? ""));
|
||||
const classType = createClassType(body.grade as Grade, String(body.name ?? ""), {
|
||||
showCurrentWord: body.showCurrentWord !== false,
|
||||
});
|
||||
return NextResponse.json({ classType }, { status: 201 });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ message: getErrorMessage(error) }, { status: 400 });
|
||||
@@ -21,6 +29,10 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
function getErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "操作失败";
|
||||
if (message.includes("UNIQUE")) return "班级类型已存在";
|
||||
if (message.includes("UNIQUE")) return "该年级下班级类型已存在";
|
||||
return message;
|
||||
}
|
||||
|
||||
function parseGrade(value: string | undefined) {
|
||||
return value === "小班" || value === "中班" || value === "大班" ? value : undefined;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ export async function POST(_request: NextRequest, context: RouteContext) {
|
||||
const safeEnglish = word.english.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");
|
||||
const fileName = `${word.id}-${safeEnglish || "word"}.mp3`;
|
||||
const filePath = getAudioFilePath(fileName);
|
||||
const publicPath = getAudioPublicPath(fileName);
|
||||
const publicPath = `${getAudioPublicPath(fileName)}?v=${Date.now()}`;
|
||||
|
||||
const settings = getSettings();
|
||||
const tts = new EdgeTTS(word.english, settings.ttsVoice, {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { listWords } from "@/lib/db";
|
||||
import { buildWordExcel } from "@/lib/word-excel";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const grade = searchParams.get("grade") ?? undefined;
|
||||
const classTypeId = Number(searchParams.get("classTypeId"));
|
||||
const words = listWords({
|
||||
grade,
|
||||
classTypeId: Number.isFinite(classTypeId) && classTypeId > 0 ? classTypeId : undefined,
|
||||
});
|
||||
const workbook = buildWordExcel(words);
|
||||
|
||||
return new Response(workbook, {
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"Content-Disposition": `attachment; filename="${encodeURIComponent("词库导入表格")}.xlsx"`,
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { importWords, type Grade } from "@/lib/db";
|
||||
import { parseWordExcel } from "@/lib/word-excel";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const grade = String(formData.get("grade") ?? "") as Grade;
|
||||
const classTypeId = Number(formData.get("classTypeId"));
|
||||
const file = formData.get("file");
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
return NextResponse.json({ message: "请选择要导入的 Excel 表格" }, { status: 400 });
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
const rows = parseWordExcel(buffer);
|
||||
const result = importWords(grade, classTypeId, rows);
|
||||
|
||||
return NextResponse.json({ result });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ message: getErrorMessage(error) }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "导入失败";
|
||||
if (message.includes("FOREIGN KEY")) return "班级类型不存在";
|
||||
if (message.includes("Unsupported file")) return "无法读取该表格,请使用 .xlsx 文件";
|
||||
return message;
|
||||
}
|
||||
Reference in New Issue
Block a user