新增 競賽建立、評審建立、團隊建立

This commit is contained in:
2025-09-15 13:32:30 +08:00
parent b85a9ce95e
commit 31ffaa1974
31 changed files with 5163 additions and 455 deletions

View File

@@ -0,0 +1,186 @@
// =====================================================
// 評審詳細操作 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { JudgeService } from '@/lib/services/database-service';
// 獲取單一評審
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 });
}
}

View File

@@ -0,0 +1,137 @@
// =====================================================
// 評審管理 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 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 });
}
}

View File

@@ -0,0 +1,72 @@
// =====================================================
// 評審統計 API
// =====================================================
import { NextResponse } from 'next/server';
import { JudgeService } from '@/lib/services/database-service';
// 獲取評審統計
export async function GET() {
try {
const judges = await JudgeService.getAllJudges();
// 計算統計數據
const totalJudges = judges.length;
const activeJudges = judges.filter(judge => judge.is_active).length;
const inactiveJudges = judges.filter(judge => !judge.is_active).length;
// 按部門統計
const departmentStats = judges.reduce((acc, judge) => {
const dept = judge.department || '未分類';
if (!acc[dept]) {
acc[dept] = { total: 0, active: 0, inactive: 0 };
}
acc[dept].total++;
if (judge.is_active) {
acc[dept].active++;
} else {
acc[dept].inactive++;
}
return acc;
}, {} as Record<string, { total: number; active: number; inactive: number }>);
// 按專業領域統計
const expertiseStats = judges.reduce((acc, judge) => {
judge.expertise.forEach(exp => {
if (!acc[exp]) {
acc[exp] = { total: 0, active: 0, inactive: 0 };
}
acc[exp].total++;
if (judge.is_active) {
acc[exp].active++;
} else {
acc[exp].inactive++;
}
});
return acc;
}, {} as Record<string, { total: number; active: number; inactive: number }>);
const stats = {
totalJudges,
activeJudges,
inactiveJudges,
departmentStats,
expertiseStats,
lastUpdated: new Date().toISOString()
};
return NextResponse.json({
success: true,
message: '評審統計獲取成功',
data: stats
});
} catch (error) {
console.error('獲取評審統計失敗:', error);
return NextResponse.json({
success: false,
message: '獲取評審統計失敗',
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}