Files
ai-showcase-platform/app/api/admin/judges/[id]/route.ts
2025-09-19 18:36:35 +08:00

189 lines
5.6 KiB
TypeScript

// =====================================================
// 評審詳細操作 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { JudgeService } from '@/lib/services/database-service';
const judgeService = new JudgeService();
// 獲取單一評審
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
try {
const { id } = await params;
const judge = await JudgeService.getJudgeById(id);
if (!judge) {
return NextResponse.json({
success: false,
message: '評審不存在',
error: '找不到指定的評審'
}, { status: 404 });
}
return NextResponse.json({
success: true,
message: '評審獲取成功',
data: judge
});
} catch (error) {
console.error('獲取評審失敗:', error);
return NextResponse.json({
success: false,
message: '獲取評審失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
// 更新評審
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
try {
const { id } = await params;
const body = await request.json();
// 檢查評審是否存在
const existingJudge = await JudgeService.getJudgeById(id);
if (!existingJudge) {
return NextResponse.json({
success: false,
message: '評審不存在',
error: '找不到指定的評審'
}, { status: 404 });
}
// 驗證姓名長度(如果提供)
if (body.name && (body.name.length < 2 || body.name.length > 50)) {
return NextResponse.json({
success: false,
message: '姓名長度無效',
error: '姓名長度必須在 2-50 個字符之間'
}, { status: 400 });
}
// 驗證職稱長度(如果提供)
if (body.title && (body.title.length < 2 || body.title.length > 100)) {
return NextResponse.json({
success: false,
message: '職稱長度無效',
error: '職稱長度必須在 2-100 個字符之間'
}, { status: 400 });
}
// 驗證專業領域(如果提供)
if (body.expertise && !Array.isArray(body.expertise)) {
return NextResponse.json({
success: false,
message: '專業領域格式無效',
error: 'expertise 必須是陣列格式'
}, { status: 400 });
}
// 如果更新姓名,檢查是否與其他評審重複
if (body.name && body.name !== existingJudge.name) {
const duplicateJudge = await JudgeService.getJudgeByName(body.name);
if (duplicateJudge) {
return NextResponse.json({
success: false,
message: '評審姓名重複',
error: '該姓名的評審已存在'
}, { status: 409 });
}
}
// 準備更新資料
const updateData: any = {};
if (body.name !== undefined) updateData.name = body.name.trim();
if (body.title !== undefined) updateData.title = body.title.trim();
if (body.department !== undefined) updateData.department = body.department.trim();
if (body.expertise !== undefined) {
updateData.expertise = body.expertise.map((exp: string) => exp.trim()).filter(Boolean);
}
if (body.avatar !== undefined) updateData.avatar = body.avatar;
if (body.isActive !== undefined) updateData.is_active = body.isActive;
if (body.is_active !== undefined) updateData.is_active = body.is_active;
// 執行更新
const success = await JudgeService.updateJudge(id, updateData);
if (!success) {
return NextResponse.json({
success: false,
message: '更新評審失敗',
error: '無法更新評審資料'
}, { status: 500 });
}
// 獲取更新後的評審資料
const updatedJudge = await JudgeService.getJudgeById(id);
return NextResponse.json({
success: true,
message: '評審更新成功',
data: updatedJudge
});
} catch (error) {
console.error('更新評審失敗:', error);
return NextResponse.json({
success: false,
message: '更新評審失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
// 刪除評審
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
try {
const { id } = await params;
const url = new URL(request.url);
const hardDelete = url.searchParams.get('hard') === 'true';
// 檢查評審是否存在
const existingJudge = await JudgeService.getJudgeById(id);
if (!existingJudge) {
return NextResponse.json({
success: false,
message: '評審不存在',
error: '找不到指定的評審'
}, { status: 404 });
}
let success: boolean;
if (hardDelete) {
// 硬刪除:從資料庫中完全移除
success = await judgeservice.deleteJudge(id);
} else {
// 軟刪除:將 is_active 設為 false
success = await judgeservice.updateJudge(id, { is_active: false });
}
if (!success) {
return NextResponse.json({
success: false,
message: '刪除評審失敗',
error: '無法刪除評審'
}, { status: 500 });
}
return NextResponse.json({
success: true,
message: hardDelete ? '評審已永久刪除' : '評審已停用',
data: { hardDelete }
});
} catch (error) {
console.error('刪除評審失敗:', error);
return NextResponse.json({
success: false,
message: '刪除評審失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}