feat: add recipe translation tool with AI translation and settings management
- Add Word document parsing and translation export - Add database configuration management - Add AI/LLM translation configuration - Add translation vocabulary management - Add export settings (colors, page orientation, headers) - Separate database and AI configurations - Add debounced search to prevent database overload
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import pool from "@/lib/database";
|
||||
|
||||
// 更新词组
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { word, english } = body;
|
||||
|
||||
if (!word || !english) {
|
||||
return NextResponse.json(
|
||||
{ error: "中文和英文翻译不能为空" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
await pool.execute("UPDATE words SET word = ?, english = ? WHERE id = ?", [
|
||||
word.trim(),
|
||||
english.trim(),
|
||||
parseInt(id),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: parseInt(id),
|
||||
word: word.trim(),
|
||||
english: english.trim(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "更新失败", details: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除词组
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
await pool.execute("DELETE FROM words WHERE id = ?", [parseInt(id)]);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "删除成功",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "删除失败", details: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import pool from "@/lib/database";
|
||||
|
||||
// 获取所有词组(分页)
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = Math.max(1, parseInt(searchParams.get("page") || "1") || 1);
|
||||
const pageSize = Math.min(100, Math.max(1, parseInt(searchParams.get("pageSize") || "20") || 20));
|
||||
const search = searchParams.get("search") || "";
|
||||
|
||||
try {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
// 构建查询条件
|
||||
let whereClause = "";
|
||||
let params: (string | number)[] = [];
|
||||
|
||||
if (search) {
|
||||
whereClause = "WHERE word LIKE ? OR english LIKE ?";
|
||||
params = [`%${search}%`, `%${search}%`];
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
const [countResult] = await pool.query(
|
||||
`SELECT COUNT(*) as total FROM words ${whereClause}`,
|
||||
params
|
||||
);
|
||||
const total = (countResult as any)[0]?.total || 0;
|
||||
|
||||
// 获取列表 - LIMIT 和 OFFSET 直接使用数字,不使用占位符
|
||||
const [rows] = await pool.query(
|
||||
`SELECT id, word, english, createTime, updateTime FROM words ${whereClause} ORDER BY id DESC LIMIT ${pageSize} OFFSET ${offset}`,
|
||||
params
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: rows,
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
total,
|
||||
totalPages: Math.ceil(total / pageSize) || 1,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "获取数据失败", details: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加词组
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { word, english } = body;
|
||||
|
||||
if (!word || !english) {
|
||||
return NextResponse.json(
|
||||
{ error: "中文和英文翻译不能为空" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const [result] = await pool.execute(
|
||||
"INSERT INTO words (word, english) VALUES (?, ?)",
|
||||
[word.trim(), english.trim()]
|
||||
);
|
||||
|
||||
const insertResult = result as any;
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: insertResult.insertId,
|
||||
word: word.trim(),
|
||||
english: english.trim(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "添加失败", details: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量添加词组
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { words } = body;
|
||||
|
||||
if (!Array.isArray(words) || words.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "请提供词组数组" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const values = words.map((w: { word: string; english: string }) => [w.word.trim(), w.english.trim()]);
|
||||
const placeholders = values.map(() => "(?, ?)").join(", ");
|
||||
const flatValues = values.flat();
|
||||
|
||||
await pool.execute(
|
||||
`INSERT INTO words (word, english) VALUES ${placeholders} ON DUPLICATE KEY UPDATE english = VALUES(english)`,
|
||||
flatValues
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `成功添加 ${words.length} 个词组`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Database error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "批量添加失败", details: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user