146 lines
4.7 KiB
TypeScript
146 lines
4.7 KiB
TypeScript
// =====================================================
|
|
// 獎項編輯和刪除 API
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { AwardService } from '@/lib/services/database-service';
|
|
|
|
// 編輯獎項
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
|
|
// 驗證必填欄位
|
|
if (!body.award_name || !body.creator) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '缺少必填欄位',
|
|
error: 'award_name, creator 為必填欄位'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 驗證獎項類型
|
|
const validAwardTypes = ['gold', 'silver', 'bronze', 'popular', 'innovation', 'technical', 'custom'];
|
|
if (body.award_type && !validAwardTypes.includes(body.award_type)) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的獎項類型',
|
|
error: `award_type 必須是以下之一: ${validAwardTypes.join(', ')}`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 驗證競賽類型
|
|
const validCompetitionTypes = ['individual', 'team', 'proposal'];
|
|
if (body.competition_type && !validCompetitionTypes.includes(body.competition_type)) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的競賽類型',
|
|
error: `competition_type 必須是以下之一: ${validCompetitionTypes.join(', ')}`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 驗證獎項類別
|
|
const validCategories = ['innovation', 'technical', 'practical', 'popular', 'teamwork', 'solution', 'creativity'];
|
|
if (body.category && !validCategories.includes(body.category)) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的獎項類別',
|
|
error: `category 必須是以下之一: ${validCategories.join(', ')}`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 準備更新資料
|
|
const updateData = {
|
|
competition_id: body.competition_id,
|
|
app_id: body.app_id || null,
|
|
team_id: body.team_id || null,
|
|
proposal_id: body.proposal_id || null,
|
|
app_name: body.app_name || null,
|
|
team_name: body.team_name || null,
|
|
proposal_title: body.proposal_title || null,
|
|
creator: body.creator,
|
|
award_type: body.award_type,
|
|
award_name: body.award_name,
|
|
score: parseFloat(body.score) || 0,
|
|
year: parseInt(body.year) || new Date().getFullYear(),
|
|
month: parseInt(body.month) || new Date().getMonth() + 1,
|
|
icon: body.icon || '🏆',
|
|
custom_award_type_id: body.custom_award_type_id || null,
|
|
competition_type: body.competition_type,
|
|
rank: parseInt(body.rank) || 0,
|
|
category: body.category,
|
|
description: body.description || null,
|
|
judge_comments: body.judge_comments || null,
|
|
application_links: body.application_links ? JSON.stringify(body.application_links) : null,
|
|
documents: body.documents ? JSON.stringify(body.documents) : null,
|
|
photos: body.photos ? JSON.stringify(body.photos) : null,
|
|
};
|
|
|
|
// 更新獎項
|
|
const success = await AwardService.updateAward(id, updateData);
|
|
|
|
if (success) {
|
|
// 獲取更新後的獎項資料
|
|
const updatedAward = await AwardService.getAwardById(id);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '獎項更新成功',
|
|
data: updatedAward
|
|
});
|
|
} else {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獎項更新失敗',
|
|
error: '找不到指定的獎項或更新失敗'
|
|
}, { status: 404 });
|
|
}
|
|
|
|
} 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 success = await AwardService.deleteAward(id);
|
|
|
|
if (success) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '獎項刪除成功'
|
|
});
|
|
} else {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獎項刪除失敗',
|
|
error: '找不到指定的獎項或刪除失敗'
|
|
}, { status: 404 });
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('刪除獎項失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '刪除獎項失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|