新增競賽前台呈現、刪除競賽、修改競賽狀態
This commit is contained in:
@@ -88,7 +88,7 @@ export async function PUT(request: NextRequest, { params }: { params: { id: stri
|
||||
|
||||
// 驗證狀態(如果提供)
|
||||
if (body.status) {
|
||||
const validStatuses = ['upcoming', 'active', 'judging', 'completed'];
|
||||
const validStatuses = ['upcoming', 'ongoing', 'active', 'judging', 'completed'];
|
||||
if (!validStatuses.includes(body.status)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
@@ -160,7 +160,7 @@ export async function PUT(request: NextRequest, { params }: { params: { id: stri
|
||||
}
|
||||
}
|
||||
|
||||
// 刪除競賽(軟刪除)
|
||||
// 刪除競賽
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
@@ -175,8 +175,8 @@ export async function DELETE(request: NextRequest, { params }: { params: { id: s
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 軟刪除:將 is_active 設為 false
|
||||
const success = await CompetitionService.updateCompetition(id, { is_active: false });
|
||||
// 使用雙寫功能刪除競賽
|
||||
const success = await CompetitionService.deleteCompetition(id);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
|
109
app/api/admin/competitions/current/route.ts
Normal file
109
app/api/admin/competitions/current/route.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
// =====================================================
|
||||
// 當前競賽管理 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { CompetitionService } from '@/lib/services/database-service';
|
||||
|
||||
// 獲取當前競賽
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const currentCompetition = await CompetitionService.getCurrentCompetition();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '當前競賽獲取成功',
|
||||
data: currentCompetition
|
||||
});
|
||||
|
||||
} 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 { competitionId } = body;
|
||||
|
||||
if (!competitionId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '缺少競賽ID',
|
||||
error: 'competitionId 為必填欄位'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// 檢查競賽是否存在
|
||||
const competition = await CompetitionService.getCompetitionById(competitionId);
|
||||
if (!competition) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '競賽不存在',
|
||||
error: '找不到指定的競賽'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
// 設置為當前競賽
|
||||
const success = await CompetitionService.setCurrentCompetition(competitionId);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '設置當前競賽失敗',
|
||||
error: '無法設置當前競賽'
|
||||
}, { status: 500 });
|
||||
}
|
||||
|
||||
// 獲取更新後的當前競賽
|
||||
const updatedCurrentCompetition = await CompetitionService.getCurrentCompetition();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '當前競賽設置成功',
|
||||
data: updatedCurrentCompetition
|
||||
});
|
||||
|
||||
} 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) {
|
||||
try {
|
||||
const success = await CompetitionService.clearCurrentCompetition();
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
@@ -103,7 +103,7 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
// 驗證狀態
|
||||
const validStatuses = ['upcoming', 'active', 'judging', 'completed'];
|
||||
const validStatuses = ['upcoming', 'ongoing', 'active', 'judging', 'completed'];
|
||||
if (!validStatuses.includes(status)) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
|
@@ -14,7 +14,8 @@ export async function GET(request: NextRequest) {
|
||||
const stats = {
|
||||
total: competitions.length,
|
||||
upcoming: competitions.filter(c => c.status === 'upcoming').length,
|
||||
active: competitions.filter(c => c.status === 'active').length,
|
||||
active: competitions.filter(c => c.status === 'active' || c.status === 'ongoing').length,
|
||||
ongoing: competitions.filter(c => c.status === 'ongoing').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,
|
||||
|
Reference in New Issue
Block a user