190 lines
5.7 KiB
TypeScript
190 lines
5.7 KiB
TypeScript
// =====================================================
|
|
// 競賽管理 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', 'ongoing', '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 });
|
|
}
|
|
}
|