131 lines
4.6 KiB
TypeScript
131 lines
4.6 KiB
TypeScript
// =====================================================
|
|
// 競賽參賽應用 API
|
|
// =====================================================
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { CompetitionService } from '@/lib/services/database-service';
|
|
import { AppService } from '@/lib/services/database-service';
|
|
|
|
// 獲取競賽的參賽應用
|
|
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
|
try {
|
|
const { id } = await params;
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
const search = searchParams.get('search') || '';
|
|
const category = searchParams.get('category') || 'all';
|
|
const type = searchParams.get('type') || 'all';
|
|
const department = searchParams.get('department') || 'all';
|
|
const competitionType = searchParams.get('competitionType') || 'all';
|
|
|
|
// 獲取競賽信息
|
|
const competition = await CompetitionService.getCompetitionWithDetails(id);
|
|
if (!competition) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '競賽不存在',
|
|
error: '找不到指定的競賽'
|
|
}, { status: 404 });
|
|
}
|
|
|
|
// 獲取競賽的參賽應用
|
|
const competitionApps = await CompetitionService.getCompetitionApps(id);
|
|
|
|
if (competitionApps.length === 0) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '競賽參賽應用獲取成功',
|
|
data: {
|
|
competition,
|
|
apps: [],
|
|
total: 0
|
|
}
|
|
});
|
|
}
|
|
|
|
// 獲取應用的點讚和瀏覽數據
|
|
const appService = new AppService();
|
|
const appsWithStats = await Promise.all(
|
|
competitionApps.map(async (app) => {
|
|
try {
|
|
const likes = await appService.getAppLikesCount(app.id);
|
|
const views = await appService.getAppViewsCount(app.id);
|
|
|
|
return {
|
|
...app,
|
|
likes,
|
|
views,
|
|
competitionType: competition.type // 添加競賽類型
|
|
};
|
|
} catch (error) {
|
|
console.error(`獲取應用 ${app.id} 統計數據失敗:`, error);
|
|
return {
|
|
...app,
|
|
likes: 0,
|
|
views: 0,
|
|
competitionType: competition.type
|
|
};
|
|
}
|
|
})
|
|
);
|
|
|
|
// 過濾掉無效的應用
|
|
const validApps = appsWithStats.filter(app => app !== null);
|
|
|
|
// 應用篩選
|
|
let filteredApps = validApps.filter(app => {
|
|
const matchesSearch = search === '' ||
|
|
app.name.toLowerCase().includes(search.toLowerCase()) ||
|
|
app.description.toLowerCase().includes(search.toLowerCase()) ||
|
|
app.creator_name?.toLowerCase().includes(search.toLowerCase());
|
|
|
|
const matchesCategory = category === 'all' || app.category === category;
|
|
const matchesType = type === 'all' || app.type === type;
|
|
const matchesDepartment = department === 'all' || app.creator_department === department;
|
|
const matchesCompetitionType = competitionType === 'all' ||
|
|
(competitionType === 'individual' && app.competition_type === 'individual') ||
|
|
(competitionType === 'team' && app.competition_type === 'team') ||
|
|
(competitionType === 'proposal' && app.competition_type === 'proposal');
|
|
|
|
return matchesSearch && matchesCategory && matchesType && matchesDepartment && matchesCompetitionType;
|
|
});
|
|
|
|
// 按人氣排序(按讚數)
|
|
filteredApps.sort((a, b) => (b.likes || 0) - (a.likes || 0));
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '競賽參賽應用獲取成功',
|
|
data: {
|
|
competition,
|
|
apps: filteredApps.map(app => ({
|
|
id: app.id,
|
|
name: app.name,
|
|
description: app.description,
|
|
category: app.category,
|
|
type: app.type,
|
|
views: app.views || 0,
|
|
likes: app.likes || 0,
|
|
rating: app.rating || 0,
|
|
creator: app.creator_name || '未知',
|
|
department: app.creator_department || '未知',
|
|
teamName: app.team_name || null,
|
|
createdAt: app.created_at ? new Date(app.created_at).toLocaleDateString('zh-TW') : '-',
|
|
icon: app.icon || 'Bot',
|
|
iconColor: app.icon_color || 'from-blue-500 to-purple-500',
|
|
competitionType: app.competitionType || 'individual'
|
|
})),
|
|
total: filteredApps.length
|
|
}
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('獲取競賽參賽應用失敗:', error);
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '獲取競賽參賽應用失敗',
|
|
error: error instanceof Error ? error.message : '未知錯誤'
|
|
}, { status: 500 });
|
|
}
|
|
}
|