新增 競賽建立、評審建立、團隊建立
This commit is contained in:
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 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user