219 lines
7.5 KiB
TypeScript
219 lines
7.5 KiB
TypeScript
// =====================================================
|
|
// 獎項管理 API
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { AwardService } from '@/lib/services/database-service';
|
|
|
|
// 創建獎項
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
|
|
// 驗證必填欄位
|
|
if (!body.competition_id || !body.award_name || !body.creator) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '缺少必填欄位',
|
|
error: 'competition_id, award_name, creator 為必填欄位'
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 驗證獎項類型
|
|
const validAwardTypes = ['gold', 'silver', 'bronze', 'popular', 'innovation', 'technical', 'custom'];
|
|
if (!validAwardTypes.includes(body.award_type)) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的獎項類型',
|
|
error: `award_type 必須是以下之一: ${validAwardTypes.join(', ')}`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 驗證競賽類型
|
|
const validCompetitionTypes = ['individual', 'team', 'proposal'];
|
|
if (!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 (!validCategories.includes(body.category)) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '無效的獎項類別',
|
|
error: `category 必須是以下之一: ${validCategories.join(', ')}`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// 準備獎項資料
|
|
const awardData = {
|
|
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 award = await AwardService.createAward(awardData);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '獎項創建成功',
|
|
data: award
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('創建獎項失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '創建獎項失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// 獲取獎項列表
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
console.log('🔍 開始獲取獎項列表...');
|
|
const { searchParams } = new URL(request.url);
|
|
const competitionId = searchParams.get('competitionId');
|
|
const awardType = searchParams.get('awardType');
|
|
const category = searchParams.get('category');
|
|
const year = searchParams.get('year');
|
|
const month = searchParams.get('month');
|
|
|
|
console.log('📋 查詢參數:', { competitionId, awardType, category, year, month });
|
|
|
|
let awards;
|
|
|
|
if (competitionId) {
|
|
console.log('🎯 根據競賽ID獲取獎項:', competitionId);
|
|
awards = await AwardService.getAwardsByCompetition(competitionId);
|
|
} else {
|
|
console.log('📊 獲取所有獎項');
|
|
awards = await AwardService.getAllAwards();
|
|
}
|
|
|
|
console.log('✅ 獲取到獎項數量:', awards?.length || 0);
|
|
|
|
// 應用篩選條件
|
|
if (awardType) {
|
|
awards = awards.filter(award => award.award_type === awardType);
|
|
}
|
|
if (category) {
|
|
awards = awards.filter(award => award.category === category);
|
|
}
|
|
if (year) {
|
|
awards = awards.filter(award => award.year === parseInt(year));
|
|
}
|
|
if (month) {
|
|
awards = awards.filter(award => award.month === parseInt(month));
|
|
}
|
|
|
|
// 解析 JSON 欄位並統一欄位命名
|
|
const processedAwards = awards.map(award => {
|
|
console.log('🔍 處理獎項:', {
|
|
id: award.id,
|
|
competition_name: (award as any).competition_name,
|
|
competition_type: (award as any).competition_type,
|
|
competition_id: award.competition_id,
|
|
team_name_from_teams: (award as any).team_name_from_teams,
|
|
team_name: (award as any).team_name,
|
|
app_name: (award as any).app_name,
|
|
team_id: (award as any).team_id
|
|
});
|
|
|
|
return {
|
|
...award,
|
|
// 統一欄位命名:將下劃線命名轉換為駝峰命名
|
|
competitionId: award.competition_id,
|
|
competitionName: (award as any).competition_name,
|
|
competitionType: (award as any).competition_type,
|
|
competitionDescription: (award as any).competition_description,
|
|
competitionStartDate: (award as any).competition_start_date,
|
|
competitionEndDate: (award as any).competition_end_date,
|
|
awardName: award.award_name,
|
|
awardType: award.award_type,
|
|
teamName: (award as any).team_name_from_teams || award.team_name,
|
|
appName: award.app_name,
|
|
applicationLinks: (() => {
|
|
const links = (award as any).application_links;
|
|
if (!links) return null;
|
|
if (typeof links === 'string') {
|
|
try {
|
|
return JSON.parse(links);
|
|
} catch (e) {
|
|
console.warn('解析 application_links JSON 失敗:', e);
|
|
return null;
|
|
}
|
|
}
|
|
return links; // 已經是對象
|
|
})(),
|
|
documents: (() => {
|
|
const docs = (award as any).documents;
|
|
if (!docs) return [];
|
|
if (typeof docs === 'string') {
|
|
try {
|
|
return JSON.parse(docs);
|
|
} catch (e) {
|
|
console.warn('解析 documents JSON 失敗:', e);
|
|
return [];
|
|
}
|
|
}
|
|
return docs; // 已經是數組
|
|
})(),
|
|
photos: (() => {
|
|
const pics = (award as any).photos;
|
|
if (!pics) return [];
|
|
if (typeof pics === 'string') {
|
|
try {
|
|
return JSON.parse(pics);
|
|
} catch (e) {
|
|
console.warn('解析 photos JSON 失敗:', e);
|
|
return [];
|
|
}
|
|
}
|
|
return pics; // 已經是數組
|
|
})(),
|
|
};
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '獎項列表獲取成功',
|
|
data: processedAwards
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('獲取獎項列表失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獲取獎項列表失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
} |