新增 競賽建立、評審建立、團隊建立
This commit is contained in:
129
app/api/admin/apps/available/route.ts
Normal file
129
app/api/admin/apps/available/route.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
// =====================================================
|
||||
// 獲取可用應用列表 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
console.log('🚀 ========== 應用 API 開始執行 ==========');
|
||||
const { searchParams } = new URL(request.url);
|
||||
const teamId = searchParams.get('teamId');
|
||||
|
||||
console.log('🔍 獲取可用應用列表, teamId:', teamId);
|
||||
console.log('🔍 請求 URL:', request.url);
|
||||
|
||||
// 先檢查所有應用
|
||||
console.log('📊 開始檢查數據庫...');
|
||||
const allAppsSql = `SELECT COUNT(*) as count FROM apps`;
|
||||
const allAppsResult = await db.query(allAppsSql);
|
||||
console.log('📊 數據庫中應用總數:', allAppsResult[0].count);
|
||||
|
||||
// 檢查活躍應用
|
||||
const activeAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = TRUE`;
|
||||
const activeAppsResult = await db.query(activeAppsSql);
|
||||
console.log('✅ 活躍應用數量 (is_active = TRUE):', activeAppsResult[0].count);
|
||||
|
||||
// 檢查所有應用的 is_active 值
|
||||
const allAppsWithStatusSql = `SELECT id, name, is_active, team_id FROM apps LIMIT 5`;
|
||||
const allAppsWithStatusResult = await db.query(allAppsWithStatusSql);
|
||||
console.log('📋 前5個應用的狀態:', allAppsWithStatusResult);
|
||||
|
||||
// 檢查是否有 is_active = 1 的應用
|
||||
const activeAppsWith1Sql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1`;
|
||||
const activeAppsWith1Result = await db.query(activeAppsWith1Sql);
|
||||
console.log('✅ is_active = 1 的應用數量:', activeAppsWith1Result[0].count);
|
||||
|
||||
// 檢查是否有 is_active = '1' 的應用(字符串)
|
||||
const activeAppsWithStringSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = '1'`;
|
||||
const activeAppsWithStringResult = await db.query(activeAppsWithStringSql);
|
||||
console.log('✅ is_active = "1" 的應用數量:', activeAppsWithStringResult[0].count);
|
||||
|
||||
// 檢查沒有團隊的應用
|
||||
const noTeamAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1 AND team_id IS NULL`;
|
||||
const noTeamAppsResult = await db.query(noTeamAppsSql);
|
||||
console.log('🔓 沒有團隊的應用數量:', noTeamAppsResult[0].count);
|
||||
|
||||
// 檢查屬於其他團隊的應用
|
||||
const otherTeamAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1 AND team_id IS NOT NULL AND team_id != ?`;
|
||||
const otherTeamAppsResult = await db.query(otherTeamAppsSql, [teamId || '']);
|
||||
console.log('🔓 屬於其他團隊的應用數量:', otherTeamAppsResult[0].count);
|
||||
|
||||
// 獲取所有活躍的應用,編輯團隊時顯示所有應用(包括已綁定的)
|
||||
// 使用 is_active = 1 因為數據庫中存儲的是數字 1
|
||||
let sql = `
|
||||
SELECT id, name, description, category, type, icon, icon_color, app_url, creator_id, team_id
|
||||
FROM apps
|
||||
WHERE is_active = 1
|
||||
ORDER BY created_at DESC
|
||||
`;
|
||||
|
||||
const params: any[] = [];
|
||||
|
||||
console.log('📝 執行的 SQL:', sql);
|
||||
console.log('📝 參數:', params);
|
||||
|
||||
const apps = await db.query(sql, params);
|
||||
console.log('📊 查詢結果:', apps.length, '個應用');
|
||||
|
||||
// 如果沒有結果,嘗試不同的查詢條件
|
||||
if (apps.length === 0) {
|
||||
console.log('⚠️ 沒有找到 is_active = 1 的應用,嘗試其他查詢條件...');
|
||||
|
||||
// 嘗試 is_active = TRUE
|
||||
const sqlTrue = sql.replace('WHERE is_active = 1', 'WHERE is_active = TRUE');
|
||||
const appsTrue = await db.query(sqlTrue, params);
|
||||
console.log('📊 is_active = TRUE 查詢結果:', appsTrue.length, '個應用');
|
||||
|
||||
// 嘗試 is_active = '1'
|
||||
const sqlString = sql.replace('WHERE is_active = 1', 'WHERE is_active = "1"');
|
||||
const appsString = await db.query(sqlString, params);
|
||||
console.log('📊 is_active = "1" 查詢結果:', appsString.length, '個應用');
|
||||
|
||||
// 嘗試沒有 is_active 條件
|
||||
const sqlNoFilter = sql.replace('WHERE is_active = 1', 'WHERE 1=1');
|
||||
const appsNoFilter = await db.query(sqlNoFilter, params);
|
||||
console.log('📊 無 is_active 過濾查詢結果:', appsNoFilter.length, '個應用');
|
||||
|
||||
// 使用有結果的查詢
|
||||
if (appsTrue.length > 0) {
|
||||
console.log('✅ 使用 is_active = TRUE 的結果');
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '可用應用列表獲取成功',
|
||||
data: appsTrue
|
||||
});
|
||||
} else if (appsString.length > 0) {
|
||||
console.log('✅ 使用 is_active = "1" 的結果');
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '可用應用列表獲取成功',
|
||||
data: appsString
|
||||
});
|
||||
} else if (appsNoFilter.length > 0) {
|
||||
console.log('✅ 使用無過濾條件的結果');
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '可用應用列表獲取成功',
|
||||
data: appsNoFilter
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log('🚀 ========== 應用 API 執行完成 ==========');
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '可用應用列表獲取成功',
|
||||
data: apps
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 獲取可用應用列表失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '獲取可用應用列表失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
95
app/api/admin/competitions/[id]/awards/route.ts
Normal file
95
app/api/admin/competitions/[id]/awards/route.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// =====================================================
|
||||
// 競賽獎項類型管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取競賽的獎項類型列表
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const awards = await CompetitionService.getCompetitionAwardTypes(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽獎項類型列表獲取成功',
|
||||
data: awards
|
||||
});
|
||||
|
||||
} 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, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { awardTypes } = body;
|
||||
|
||||
if (!awardTypes || !Array.isArray(awardTypes) || awardTypes.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少獎項類型列表',
|
||||
error: 'awardTypes 必須是非空陣列'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.addCompetitionAwardTypes(id, awardTypes);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '獎項類型添加成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} 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 { searchParams } = new URL(request.url);
|
||||
const awardTypeId = searchParams.get('awardTypeId');
|
||||
|
||||
if (!awardTypeId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少獎項類型ID',
|
||||
error: 'awardTypeId 參數是必需的'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.removeCompetitionAwardType(id, awardTypeId);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '獎項類型移除成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除競賽獎項類型失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除競賽獎項類型失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
95
app/api/admin/competitions/[id]/judges/route.ts
Normal file
95
app/api/admin/competitions/[id]/judges/route.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// =====================================================
|
||||
// 競賽評審關聯管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取競賽的評審列表
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const judges = await CompetitionService.getCompetitionJudges(id);
|
||||
|
||||
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, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { judgeIds } = body;
|
||||
|
||||
if (!judgeIds || !Array.isArray(judgeIds) || judgeIds.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少評審ID列表',
|
||||
error: 'judgeIds 必須是非空陣列'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.addCompetitionJudges(id, judgeIds);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '評審添加成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} 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 { searchParams } = new URL(request.url);
|
||||
const judgeId = searchParams.get('judgeId');
|
||||
|
||||
if (!judgeId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少評審ID',
|
||||
error: 'judgeId 參數是必需的'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.removeCompetitionJudge(id, judgeId);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '評審移除成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除競賽評審失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除競賽評審失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
199
app/api/admin/competitions/[id]/route.ts
Normal file
199
app/api/admin/competitions/[id]/route.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
// =====================================================
|
||||
// 競賽詳細操作 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取單一競賽
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const competition = await CompetitionService.getCompetitionWithDetails(id);
|
||||
|
||||
if (!competition) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '競賽不存在',
|
||||
error: '找不到指定的競賽'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽獲取成功',
|
||||
data: competition
|
||||
});
|
||||
|
||||
} 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 existingCompetition = await CompetitionService.getCompetitionById(id);
|
||||
if (!existingCompetition) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '競賽不存在',
|
||||
error: '找不到指定的競賽'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 驗證日期(如果提供)
|
||||
if (body.startDate && body.endDate) {
|
||||
const startDateObj = new Date(body.startDate);
|
||||
const endDateObj = new Date(body.endDate);
|
||||
|
||||
if (isNaN(startDateObj.getTime()) || isNaN(endDateObj.getTime())) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '日期格式無效',
|
||||
error: 'startDate 和 endDate 必須是有效的日期格式'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
if (endDateObj <= startDateObj) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '結束日期不能早於或等於開始日期',
|
||||
error: 'endDate 必須晚於 startDate'
|
||||
}, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
// 驗證競賽類型(如果提供)
|
||||
if (body.type) {
|
||||
const validTypes = ['individual', 'team', 'mixed', 'proposal'];
|
||||
if (!validTypes.includes(body.type)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '無效的競賽類型',
|
||||
error: `type 必須是以下之一: ${validTypes.join(', ')}`
|
||||
}, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
// 驗證狀態(如果提供)
|
||||
if (body.status) {
|
||||
const validStatuses = ['upcoming', 'active', 'judging', 'completed'];
|
||||
if (!validStatuses.includes(body.status)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '無效的競賽狀態',
|
||||
error: `status 必須是以下之一: ${validStatuses.join(', ')}`
|
||||
}, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
// 準備更新資料
|
||||
const updateData: any = {};
|
||||
|
||||
if (body.name !== undefined) updateData.name = body.name;
|
||||
if (body.year !== undefined) updateData.year = parseInt(body.year);
|
||||
if (body.month !== undefined) updateData.month = parseInt(body.month);
|
||||
if (body.startDate !== undefined) updateData.start_date = body.startDate;
|
||||
if (body.endDate !== undefined) updateData.end_date = body.endDate;
|
||||
if (body.status !== undefined) updateData.status = body.status;
|
||||
if (body.description !== undefined) updateData.description = body.description;
|
||||
if (body.type !== undefined) updateData.type = body.type;
|
||||
if (body.evaluationFocus !== undefined) updateData.evaluation_focus = body.evaluationFocus;
|
||||
if (body.maxTeamSize !== undefined) updateData.max_team_size = body.maxTeamSize ? parseInt(body.maxTeamSize) : null;
|
||||
if (body.isActive !== undefined) updateData.is_active = body.isActive;
|
||||
|
||||
// 執行更新
|
||||
const success = await CompetitionService.updateCompetition(id, updateData);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '更新競賽失敗',
|
||||
error: '無法更新競賽資料'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
// 更新關聯數據
|
||||
if (body.judges !== undefined) {
|
||||
await CompetitionService.addCompetitionJudges(id, body.judges || []);
|
||||
}
|
||||
if (body.teams !== undefined) {
|
||||
await CompetitionService.addCompetitionTeams(id, body.teams || []);
|
||||
}
|
||||
if (body.awardTypes !== undefined) {
|
||||
await CompetitionService.addCompetitionAwardTypes(id, body.awardTypes || []);
|
||||
}
|
||||
if (body.rules !== undefined) {
|
||||
await CompetitionService.addCompetitionRules(id, body.rules || []);
|
||||
}
|
||||
|
||||
// 獲取更新後的完整競賽資料(包含關聯數據)
|
||||
const updatedCompetition = await CompetitionService.getCompetitionWithDetails(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽更新成功',
|
||||
data: updatedCompetition
|
||||
});
|
||||
|
||||
} 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 existingCompetition = await CompetitionService.getCompetitionById(id);
|
||||
if (!existingCompetition) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '競賽不存在',
|
||||
error: '找不到指定的競賽'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 軟刪除:將 is_active 設為 false
|
||||
const success = await CompetitionService.updateCompetition(id, { is_active: false });
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '刪除競賽失敗',
|
||||
error: '無法刪除競賽'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽刪除成功'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('刪除競賽失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '刪除競賽失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
95
app/api/admin/competitions/[id]/rules/route.ts
Normal file
95
app/api/admin/competitions/[id]/rules/route.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// =====================================================
|
||||
// 競賽評分規則管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取競賽的評分規則列表
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const rules = await CompetitionService.getCompetitionRules(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽評分規則列表獲取成功',
|
||||
data: rules
|
||||
});
|
||||
|
||||
} 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, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { rules } = body;
|
||||
|
||||
if (!rules || !Array.isArray(rules) || rules.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少評分規則列表',
|
||||
error: 'rules 必須是非空陣列'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.addCompetitionRules(id, rules);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '評分規則添加成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} 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 { searchParams } = new URL(request.url);
|
||||
const ruleId = searchParams.get('ruleId');
|
||||
|
||||
if (!ruleId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少評分規則ID',
|
||||
error: 'ruleId 參數是必需的'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.removeCompetitionRule(id, ruleId);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '評分規則移除成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除競賽評分規則失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除競賽評分規則失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
95
app/api/admin/competitions/[id]/teams/route.ts
Normal file
95
app/api/admin/competitions/[id]/teams/route.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// =====================================================
|
||||
// 競賽團隊關聯管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取競賽的團隊列表
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const teams = await CompetitionService.getCompetitionTeams(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽團隊列表獲取成功',
|
||||
data: teams
|
||||
});
|
||||
|
||||
} 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, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { teamIds } = body;
|
||||
|
||||
if (!teamIds || !Array.isArray(teamIds) || teamIds.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少團隊ID列表',
|
||||
error: 'teamIds 必須是非空陣列'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.addCompetitionTeams(id, teamIds);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊添加成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} 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 { searchParams } = new URL(request.url);
|
||||
const teamId = searchParams.get('teamId');
|
||||
|
||||
if (!teamId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少團隊ID',
|
||||
error: 'teamId 參數是必需的'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const result = await CompetitionService.removeCompetitionTeam(id, teamId);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊移除成功',
|
||||
data: result
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除競賽團隊失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除競賽團隊失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
189
app/api/admin/competitions/route.ts
Normal file
189
app/api/admin/competitions/route.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
// =====================================================
|
||||
// 競賽管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取所有競賽
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const competitions = await CompetitionService.getAllCompetitions();
|
||||
|
||||
// 為每個競賽獲取關聯數據
|
||||
const competitionsWithDetails = await Promise.all(
|
||||
competitions.map(async (competition) => {
|
||||
try {
|
||||
const fullCompetition = await CompetitionService.getCompetitionWithDetails(competition.id);
|
||||
return fullCompetition;
|
||||
} catch (error) {
|
||||
console.error(`獲取競賽 ${competition.id} 詳細信息失敗:`, error);
|
||||
return competition; // 如果獲取詳細信息失敗,返回基本競賽信息
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽列表獲取成功',
|
||||
data: competitionsWithDetails
|
||||
});
|
||||
|
||||
} 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,
|
||||
year,
|
||||
month,
|
||||
startDate,
|
||||
endDate,
|
||||
status = 'upcoming',
|
||||
description,
|
||||
type = 'individual',
|
||||
evaluationFocus,
|
||||
maxTeamSize,
|
||||
isActive = true,
|
||||
// 關聯數據
|
||||
judges = [],
|
||||
teams = [],
|
||||
awardTypes = [],
|
||||
rules = []
|
||||
} = body;
|
||||
|
||||
// 驗證必填欄位
|
||||
if (!name || !year || !month || !startDate || !endDate || !type) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少必填欄位',
|
||||
error: 'name, year, month, startDate, endDate, type 為必填欄位'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 驗證日期格式
|
||||
const startDateObj = new Date(startDate);
|
||||
const endDateObj = new Date(endDate);
|
||||
|
||||
if (isNaN(startDateObj.getTime()) || isNaN(endDateObj.getTime())) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '日期格式無效',
|
||||
error: 'startDate 和 endDate 必須是有效的日期格式'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 驗證結束日期不能早於開始日期
|
||||
if (endDateObj <= startDateObj) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '結束日期不能早於或等於開始日期',
|
||||
error: 'endDate 必須晚於 startDate'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 驗證競賽類型
|
||||
const validTypes = ['individual', 'team', 'mixed', 'proposal'];
|
||||
if (!validTypes.includes(type)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '無效的競賽類型',
|
||||
error: `type 必須是以下之一: ${validTypes.join(', ')}`
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 驗證狀態
|
||||
const validStatuses = ['upcoming', 'active', 'judging', 'completed'];
|
||||
if (!validStatuses.includes(status)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '無效的競賽狀態',
|
||||
error: `status 必須是以下之一: ${validStatuses.join(', ')}`
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 創建競賽
|
||||
const competitionData = {
|
||||
name,
|
||||
year: parseInt(year),
|
||||
month: parseInt(month),
|
||||
start_date: startDate,
|
||||
end_date: endDate,
|
||||
status,
|
||||
description: description || null,
|
||||
type,
|
||||
evaluation_focus: evaluationFocus || null,
|
||||
max_team_size: maxTeamSize ? parseInt(maxTeamSize) : null,
|
||||
is_active: isActive
|
||||
};
|
||||
|
||||
const newCompetition = await CompetitionService.createCompetition(competitionData);
|
||||
|
||||
// 保存關聯數據
|
||||
if (newCompetition) {
|
||||
try {
|
||||
// 保存評審關聯
|
||||
if (judges && judges.length > 0) {
|
||||
await CompetitionService.addCompetitionJudges(newCompetition.id, judges);
|
||||
}
|
||||
|
||||
// 保存團隊關聯
|
||||
if (teams && teams.length > 0) {
|
||||
await CompetitionService.addCompetitionTeams(newCompetition.id, teams);
|
||||
}
|
||||
|
||||
// 保存獎項類型
|
||||
if (awardTypes && awardTypes.length > 0) {
|
||||
await CompetitionService.addCompetitionAwardTypes(newCompetition.id, awardTypes);
|
||||
}
|
||||
|
||||
// 保存評分規則
|
||||
if (rules && rules.length > 0) {
|
||||
await CompetitionService.addCompetitionRules(newCompetition.id, rules);
|
||||
}
|
||||
|
||||
// 獲取完整的競賽信息(包含關聯數據)
|
||||
const fullCompetition = await CompetitionService.getCompetitionWithDetails(newCompetition.id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽創建成功',
|
||||
data: fullCompetition
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('保存競賽關聯數據失敗:', error);
|
||||
// 即使關聯數據保存失敗,競賽本身已經創建成功
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '競賽創建成功,但部分關聯數據保存失敗',
|
||||
data: newCompetition,
|
||||
warning: '部分關聯數據可能未正確保存,請檢查資料庫狀態'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '競賽創建失敗',
|
||||
error: '無法創建競賽'
|
||||
}, { status: 500 });
|
||||
|
||||
} catch (error) {
|
||||
console.error('創建競賽失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '創建競賽失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
45
app/api/admin/competitions/stats/route.ts
Normal file
45
app/api/admin/competitions/stats/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// =====================================================
|
||||
// 競賽統計 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取競賽統計數據
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const competitions = await CompetitionService.getAllCompetitions();
|
||||
|
||||
// 計算統計數據
|
||||
const stats = {
|
||||
total: competitions.length,
|
||||
upcoming: competitions.filter(c => c.status === 'upcoming').length,
|
||||
active: competitions.filter(c => c.status === 'active').length,
|
||||
judging: competitions.filter(c => c.status === 'judging').length,
|
||||
completed: competitions.filter(c => c.status === 'completed').length,
|
||||
individual: competitions.filter(c => c.type === 'individual').length,
|
||||
team: competitions.filter(c => c.type === 'team').length,
|
||||
mixed: competitions.filter(c => c.type === 'mixed').length,
|
||||
proposal: competitions.filter(c => c.type === 'proposal').length,
|
||||
currentYear: competitions.filter(c => c.year === new Date().getFullYear()).length,
|
||||
thisMonth: competitions.filter(c =>
|
||||
c.year === new Date().getFullYear() &&
|
||||
c.month === new Date().getMonth() + 1
|
||||
).length
|
||||
};
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
186
app/api/admin/judges/[id]/route.ts
Normal file
186
app/api/admin/judges/[id]/route.ts
Normal 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 });
|
||||
}
|
||||
}
|
137
app/api/admin/judges/route.ts
Normal file
137
app/api/admin/judges/route.ts
Normal 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 });
|
||||
}
|
||||
}
|
72
app/api/admin/judges/stats/route.ts
Normal file
72
app/api/admin/judges/stats/route.ts
Normal 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 });
|
||||
}
|
||||
}
|
195
app/api/admin/teams/[id]/members/route.ts
Normal file
195
app/api/admin/teams/[id]/members/route.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
// =====================================================
|
||||
// 團隊成員管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { TeamService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取團隊成員
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
// 檢查團隊是否存在
|
||||
const team = await TeamService.getTeamById(id);
|
||||
if (!team) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
const members = await TeamService.getTeamMembers(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊成員獲取成功',
|
||||
data: members
|
||||
});
|
||||
|
||||
} 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, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
|
||||
// 驗證必填字段
|
||||
if (!body.user_id || !body.role) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '請提供用戶ID和角色',
|
||||
error: '缺少必填字段'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 檢查團隊是否存在
|
||||
const team = await TeamService.getTeamById(id);
|
||||
if (!team) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 添加團隊成員
|
||||
const success = await TeamService.addTeamMember(id, body.user_id, body.role);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '添加團隊成員失敗',
|
||||
error: '無法添加團隊成員'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊成員添加成功',
|
||||
data: { team_id: id, user_id: body.user_id, role: body.role }
|
||||
});
|
||||
|
||||
} 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();
|
||||
|
||||
// 驗證必填字段
|
||||
if (!body.user_id || !body.role) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '請提供用戶ID和角色',
|
||||
error: '缺少必填字段'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 檢查團隊是否存在
|
||||
const team = await TeamService.getTeamById(id);
|
||||
if (!team) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 更新團隊成員角色
|
||||
const success = await TeamService.updateTeamMemberRole(id, body.user_id, body.role);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '更新團隊成員角色失敗',
|
||||
error: '無法更新團隊成員角色'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊成員角色更新成功',
|
||||
data: { team_id: id, user_id: body.user_id, role: body.role }
|
||||
});
|
||||
|
||||
} 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 userId = url.searchParams.get('user_id');
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '請提供用戶ID',
|
||||
error: '缺少用戶ID參數'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 檢查團隊是否存在
|
||||
const team = await TeamService.getTeamById(id);
|
||||
if (!team) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 移除團隊成員
|
||||
const success = await TeamService.removeTeamMember(id, userId);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除團隊成員失敗',
|
||||
error: '無法移除團隊成員'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊成員移除成功',
|
||||
data: { team_id: id, user_id: userId }
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除團隊成員失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '移除團隊成員失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
154
app/api/admin/teams/[id]/route.ts
Normal file
154
app/api/admin/teams/[id]/route.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
// =====================================================
|
||||
// 團隊詳細操作 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { TeamService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取單一團隊
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
const team = await TeamService.getTeamById(id);
|
||||
|
||||
if (!team) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 獲取團隊成員
|
||||
const members = await TeamService.getTeamMembers(id);
|
||||
team.members = members;
|
||||
|
||||
// 獲取團隊應用
|
||||
const apps = await TeamService.getTeamApps(id);
|
||||
team.apps = apps;
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊獲取成功',
|
||||
data: team
|
||||
});
|
||||
|
||||
} 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 existingTeam = await TeamService.getTeamById(id);
|
||||
if (!existingTeam) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 如果更新名稱,檢查是否重複
|
||||
if (body.name && body.name !== existingTeam.name) {
|
||||
const nameExists = await TeamService.getTeamByName(body.name);
|
||||
if (nameExists) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊名稱已存在',
|
||||
error: '團隊名稱重複'
|
||||
}, { status: 409 });
|
||||
}
|
||||
}
|
||||
|
||||
// 更新團隊
|
||||
const success = await TeamService.updateTeam(id, body);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '更新團隊失敗',
|
||||
error: '無法更新團隊'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
// 獲取更新後的團隊信息
|
||||
const updatedTeam = await TeamService.getTeamById(id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊更新成功',
|
||||
data: updatedTeam
|
||||
});
|
||||
|
||||
} 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 existingTeam = await TeamService.getTeamById(id);
|
||||
if (!existingTeam) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊不存在',
|
||||
error: '找不到指定的團隊'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
let success: boolean;
|
||||
|
||||
if (hardDelete) {
|
||||
// 硬刪除:從資料庫中完全移除
|
||||
success = await TeamService.hardDeleteTeam(id);
|
||||
} else {
|
||||
// 軟刪除:將 is_active 設為 false
|
||||
success = await TeamService.deleteTeam(id);
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
111
app/api/admin/teams/route.ts
Normal file
111
app/api/admin/teams/route.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
// =====================================================
|
||||
// 團隊管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { TeamService } 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') || '';
|
||||
|
||||
let teams = await TeamService.getAllTeams();
|
||||
|
||||
// 應用篩選
|
||||
if (search) {
|
||||
teams = teams.filter(team =>
|
||||
team.name.toLowerCase().includes(search.toLowerCase()) ||
|
||||
team.leader_name?.toLowerCase().includes(search.toLowerCase())
|
||||
);
|
||||
}
|
||||
|
||||
if (department && department !== 'all') {
|
||||
teams = teams.filter(team => team.department === department);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊列表獲取成功',
|
||||
data: teams
|
||||
});
|
||||
|
||||
} 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();
|
||||
|
||||
// 驗證必填字段
|
||||
if (!body.name || !body.leader_id || !body.department || !body.contact_email) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '請填寫所有必填字段',
|
||||
error: '缺少必填字段'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 檢查團隊名稱是否已存在
|
||||
const existingTeam = await TeamService.getTeamByName(body.name);
|
||||
if (existingTeam) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '團隊名稱已存在',
|
||||
error: '團隊名稱重複'
|
||||
}, { status: 409 });
|
||||
}
|
||||
|
||||
// 創建團隊
|
||||
const teamId = await TeamService.createTeam({
|
||||
name: body.name,
|
||||
leader_id: body.leader_id,
|
||||
department: body.department,
|
||||
contact_email: body.contact_email,
|
||||
description: body.description
|
||||
});
|
||||
|
||||
// 如果提供了成員列表,添加成員
|
||||
if (body.members && Array.isArray(body.members)) {
|
||||
for (const member of body.members) {
|
||||
if (member.user_id && member.role) {
|
||||
await TeamService.addTeamMember(teamId, member.user_id, member.role);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果提供了應用列表,綁定應用
|
||||
if (body.apps && Array.isArray(body.apps)) {
|
||||
for (const appId of body.apps) {
|
||||
await TeamService.bindAppToTeam(teamId, appId);
|
||||
}
|
||||
}
|
||||
|
||||
// 獲取創建後的團隊信息
|
||||
const createdTeam = await TeamService.getTeamById(teamId);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '團隊創建成功',
|
||||
data: createdTeam
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('創建團隊失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '創建團隊失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
26
app/api/admin/teams/stats/route.ts
Normal file
26
app/api/admin/teams/stats/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// =====================================================
|
||||
// 團隊統計 API
|
||||
// =====================================================
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { TeamService } from '@/lib/services/database-service';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const stats = await TeamService.getTeamStats();
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
34
app/api/admin/users/available/route.ts
Normal file
34
app/api/admin/users/available/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// =====================================================
|
||||
// 獲取可用用戶列表 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// 獲取所有活躍的用戶
|
||||
const sql = `
|
||||
SELECT id, name, email, department, phone
|
||||
FROM users
|
||||
WHERE status = 'active'
|
||||
ORDER BY name ASC
|
||||
`;
|
||||
|
||||
const users = await db.query(sql);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '可用用戶列表獲取成功',
|
||||
data: users
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('獲取可用用戶列表失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '獲取可用用戶列表失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user