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

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,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 });
}
}