Initial English PK app
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { existsSync, mkdirSync } from "node:fs";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { EdgeTTS } from "edge-tts-universal";
|
||||
import { findWord, getSettings, updateWordAudio } from "@/lib/db";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function POST(_request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const wordId = Number(id);
|
||||
const word = findWord(wordId);
|
||||
|
||||
if (!word) {
|
||||
return NextResponse.json({ message: "词库不存在" }, { status: 404 });
|
||||
}
|
||||
|
||||
const audioDirectory = path.join(process.cwd(), "public", "audio");
|
||||
if (!existsSync(audioDirectory)) {
|
||||
mkdirSync(audioDirectory, { recursive: true });
|
||||
}
|
||||
|
||||
const safeEnglish = word.english.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "");
|
||||
const fileName = `${word.id}-${safeEnglish || "word"}.mp3`;
|
||||
const filePath = path.join(audioDirectory, fileName);
|
||||
const publicPath = `/audio/${fileName}`;
|
||||
|
||||
const settings = getSettings();
|
||||
const tts = new EdgeTTS(word.english, settings.ttsVoice, {
|
||||
rate: settings.ttsRate,
|
||||
});
|
||||
const result = await tts.synthesize();
|
||||
const audioBuffer = Buffer.from(await result.audio.arrayBuffer());
|
||||
await writeFile(filePath, audioBuffer);
|
||||
|
||||
const updatedWord = updateWordAudio(word.id, publicPath);
|
||||
return NextResponse.json({ word: updatedWord });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "生成语音失败";
|
||||
return NextResponse.json({ message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { deleteWord, updateWord, type Grade } from "@/lib/db";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type RouteContext = {
|
||||
params: Promise<{ id: string }>;
|
||||
};
|
||||
|
||||
export async function PUT(request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const body = await request.json();
|
||||
const word = updateWord(Number(id), {
|
||||
grade: body.grade as Grade,
|
||||
classTypeId: Number(body.classTypeId),
|
||||
english: String(body.english ?? ""),
|
||||
chinese: String(body.chinese ?? ""),
|
||||
image: String(body.image ?? ""),
|
||||
audio: String(body.audio ?? ""),
|
||||
enabled: Boolean(body.enabled),
|
||||
});
|
||||
|
||||
return NextResponse.json({ word });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ message: getErrorMessage(error) }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(_request: NextRequest, context: RouteContext) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
deleteWord(Number(id));
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ message: getErrorMessage(error) }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "操作失败";
|
||||
if (message.includes("UNIQUE")) return "相同年级、班级类型和英语题库已存在";
|
||||
if (message.includes("FOREIGN KEY")) return "班级类型不存在";
|
||||
return message;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createWord, listWords, type Grade } from "@/lib/db";
|
||||
|
||||
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 enabledOnly = searchParams.get("enabledOnly") === "true";
|
||||
|
||||
const words = listWords({
|
||||
grade,
|
||||
classTypeId: Number.isFinite(classTypeId) && classTypeId > 0 ? classTypeId : undefined,
|
||||
enabledOnly,
|
||||
});
|
||||
|
||||
return NextResponse.json({ words });
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const word = createWord({
|
||||
grade: body.grade as Grade,
|
||||
classTypeId: Number(body.classTypeId),
|
||||
english: String(body.english ?? ""),
|
||||
chinese: String(body.chinese ?? ""),
|
||||
image: String(body.image ?? ""),
|
||||
audio: String(body.audio ?? ""),
|
||||
enabled: Boolean(body.enabled),
|
||||
});
|
||||
|
||||
return NextResponse.json({ word }, { status: 201 });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ message: getErrorMessage(error) }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : "操作失败";
|
||||
if (message.includes("UNIQUE")) return "相同年级、班级类型和英语题库已存在";
|
||||
if (message.includes("FOREIGN KEY")) return "班级类型不存在";
|
||||
return message;
|
||||
}
|
||||
Reference in New Issue
Block a user