實現得獎資訊與資料庫整合

This commit is contained in:
2025-09-26 01:42:52 +08:00
parent 9bed168238
commit 0675fe63b0
8 changed files with 1531 additions and 409 deletions

View File

@@ -98,6 +98,7 @@ export async function POST(request: NextRequest) {
// 獲取獎項列表
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');
@@ -105,14 +106,20 @@ export async function GET(request: NextRequest) {
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);
@@ -127,10 +134,27 @@ export async function GET(request: NextRequest) {
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
});
return {
...award,
application_links: award.application_links ? JSON.parse(award.application_links) : null,
documents: award.documents ? JSON.parse(award.documents) : [],
photos: award.photos ? JSON.parse(award.photos) : [],
};
});
return NextResponse.json({
success: true,
message: '獎項列表獲取成功',
data: awards
data: processedAwards
});
} catch (error) {
@@ -141,4 +165,4 @@ export async function GET(request: NextRequest) {
error: error instanceof Error ? error.message : '未知錯誤'
}, { status: 500 });
}
}
}

View File

@@ -1,95 +1,43 @@
// =====================================================
// 競賽評審關聯管理 API
// 競賽評審 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { CompetitionService } from '@/lib/services/database-service';
import { AwardService } from '@/lib/services/database-service';
// 獲取競賽評審列表
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
// 獲取競賽評審列表
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const { id: competitionId } = 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) {
if (!competitionId) {
return NextResponse.json({
success: false,
message: '缺少評審ID列表',
error: 'judgeIds 必須是非空陣列'
message: '缺少競賽ID參數'
}, { status: 400 });
}
const result = await CompetitionService.addCompetitionJudges(id, judgeIds);
console.log('🔍 獲取競賽評審:', competitionId);
const judges = await AwardService.getCompetitionJudges(competitionId);
console.log('✅ 獲取到評審數量:', judges?.length || 0);
return NextResponse.json({
success: true,
message: '評審添加成功',
data: result
message: '評審列表獲取成功',
data: judges || []
});
} catch (error) {
console.error('添加競賽評審失敗:', error);
console.error('❌ 獲取評審失敗:', error);
return NextResponse.json({
success: false,
message: '添加競賽評審失敗',
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 });
}
}
}

View File

@@ -1,95 +1,37 @@
// =====================================================
// 競賽團隊關聯管理 API
// 競賽參賽團隊 API
// =====================================================
import { NextRequest, NextResponse } from 'next/server';
import { CompetitionService } from '@/lib/services/database-service';
// 獲取競賽的團隊列表
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
// 獲取競賽的參賽團隊
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const teams = await CompetitionService.getCompetitionTeams(id);
const { id: competitionId } = await params;
if (!competitionId) {
return NextResponse.json({
success: false,
message: '缺少競賽ID參數'
}, { status: 400 });
}
// 獲取競賽的參賽團隊
const teams = await CompetitionService.getCompetitionTeams(competitionId);
return NextResponse.json({
success: true,
message: '賽團隊列表獲取成功',
message: '賽團隊獲取成功',
data: teams
});
} catch (error) {
console.error('獲取賽團隊失敗:', error);
console.error('獲取賽團隊失敗:', error);
return NextResponse.json({
success: false,
message: '獲取賽團隊失敗',
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 });
}
}
}

View File

@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from 'next/server'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function POST(request: NextRequest) {
try {
const data = await request.formData()
const file: File | null = data.get('file') as unknown as File
const awardId = data.get('awardId') as string
if (!file) {
return NextResponse.json({ error: '沒有選擇文件' }, { status: 400 })
}
// 驗證文件類型
const allowedTypes = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
]
if (!allowedTypes.includes(file.type)) {
return NextResponse.json({
error: '不支持的文件格式,請上傳 PDF、DOC、DOCX、PPT、PPTX 格式的文件'
}, { status: 400 })
}
// 驗證文件大小 (10MB)
const maxSize = 10 * 1024 * 1024
if (file.size > maxSize) {
return NextResponse.json({
error: '文件大小超過 10MB 限制'
}, { status: 400 })
}
// 創建上傳目錄
const uploadDir = join(process.cwd(), 'public', 'uploads', 'documents')
if (!existsSync(uploadDir)) {
await mkdir(uploadDir, { recursive: true })
}
// 生成唯一文件名
const timestamp = Date.now()
const fileExtension = file.name.split('.').pop()
const fileName = `${awardId}_${timestamp}.${fileExtension}`
const filePath = join(uploadDir, fileName)
// 保存文件
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
await writeFile(filePath, buffer)
// 返回文件信息
const fileUrl = `/uploads/documents/${fileName}`
return NextResponse.json({
success: true,
url: fileUrl,
fileName: file.name,
size: file.size,
type: file.type
})
} catch (error) {
console.error('文件上傳錯誤:', error)
return NextResponse.json(
{ error: '文件上傳失敗' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from 'next/server'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function POST(request: NextRequest) {
try {
const data = await request.formData()
const file: File | null = data.get('file') as unknown as File
const awardId = data.get('awardId') as string
if (!file) {
return NextResponse.json({ error: '沒有選擇照片' }, { status: 400 })
}
// 驗證文件類型
const allowedTypes = [
'image/jpeg',
'image/jpg',
'image/png',
'image/gif',
'image/webp'
]
if (!allowedTypes.includes(file.type)) {
return NextResponse.json({
error: '不支持的照片格式,請上傳 JPG、PNG、GIF、WEBP 格式的照片'
}, { status: 400 })
}
// 驗證文件大小 (5MB)
const maxSize = 5 * 1024 * 1024
if (file.size > maxSize) {
return NextResponse.json({
error: '照片大小超過 5MB 限制'
}, { status: 400 })
}
// 創建上傳目錄
const uploadDir = join(process.cwd(), 'public', 'uploads', 'photos')
if (!existsSync(uploadDir)) {
await mkdir(uploadDir, { recursive: true })
}
// 生成唯一文件名
const timestamp = Date.now()
const fileExtension = file.name.split('.').pop()
const fileName = `${awardId}_${timestamp}.${fileExtension}`
const filePath = join(uploadDir, fileName)
// 保存文件
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
await writeFile(filePath, buffer)
// 返回文件信息
const fileUrl = `/uploads/photos/${fileName}`
return NextResponse.json({
success: true,
url: fileUrl,
fileName: file.name,
size: file.size,
type: file.type
})
} catch (error) {
console.error('照片上傳錯誤:', error)
return NextResponse.json(
{ error: '照片上傳失敗' },
{ status: 500 }
)
}
}