96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
// =====================================================
|
|
// 競賽團隊關聯管理 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 });
|
|
}
|
|
}
|