Files

139 lines
3.8 KiB
TypeScript

// =====================================================
// 評審管理 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { JudgeService } from '@/lib/services/database-service';
// 獲取所有評審
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const search = searchParams.get('search') || '';
const department = searchParams.get('department') || '';
const expertise = searchParams.get('expertise') || '';
let judges = await JudgeService.getAllJudges();
// 應用篩選
if (search) {
judges = judges.filter(judge =>
judge.name.toLowerCase().includes(search.toLowerCase()) ||
judge.title.toLowerCase().includes(search.toLowerCase())
);
}
if (department && department !== 'all') {
judges = judges.filter(judge => judge.department === department);
}
if (expertise && expertise !== 'all') {
judges = judges.filter(judge =>
judge.expertise.some(exp => exp.toLowerCase().includes(expertise.toLowerCase()))
);
}
return NextResponse.json({
success: true,
message: '評審列表獲取成功',
data: judges
});
} catch (error) {
console.error('獲取評審列表失敗:', error);
return NextResponse.json({
success: false,
message: '獲取評審列表失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
// 創建新評審
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const {
name,
title,
department,
expertise = [],
avatar,
isActive = true
} = body;
// 驗證必填欄位
if (!name || !title || !department) {
return NextResponse.json({
success: false,
message: '缺少必填欄位',
error: 'name, title, department 為必填欄位'
}, { status: 400 });
}
// 驗證姓名長度
if (name.length < 2 || name.length > 50) {
return NextResponse.json({
success: false,
message: '姓名長度無效',
error: '姓名長度必須在 2-50 個字符之間'
}, { status: 400 });
}
// 驗證職稱長度
if (title.length < 2 || title.length > 100) {
return NextResponse.json({
success: false,
message: '職稱長度無效',
error: '職稱長度必須在 2-100 個字符之間'
}, { status: 400 });
}
// 驗證專業領域
if (!Array.isArray(expertise)) {
return NextResponse.json({
success: false,
message: '專業領域格式無效',
error: 'expertise 必須是陣列格式'
}, { status: 400 });
}
// 檢查是否已存在相同姓名的評審
const existingJudge = await JudgeService.getJudgeByName(name);
if (existingJudge) {
return NextResponse.json({
success: false,
message: '評審已存在',
error: '該姓名的評審已存在'
}, { status: 409 });
}
// 創建評審
const judgeData = {
name: name.trim(),
title: title.trim(),
department: department.trim(),
expertise: expertise.map((exp: string) => exp.trim()).filter(Boolean),
avatar: avatar || null,
is_active: isActive
};
const judgeService = new JudgeService();
const newJudge = await judgeService.createJudge(judgeData);
return NextResponse.json({
success: true,
message: '評審創建成功',
data: newJudge
});
} catch (error) {
console.error('創建評審失敗:', error);
return NextResponse.json({
success: false,
message: '創建評審失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}