diff --git a/src/app/api/keyword-cloud/quarters/route.ts b/src/app/api/keyword-cloud/quarters/route.ts new file mode 100644 index 0000000..938c767 --- /dev/null +++ b/src/app/api/keyword-cloud/quarters/route.ts @@ -0,0 +1,47 @@ +import { NextResponse } from 'next/server'; +import { getAllQuarters } from '@/hooks/useKeywordCloud'; + +export const dynamic = 'force-dynamic'; + +/** + * GET /api/keyword-cloud/quarters + * 获取季度列表 + */ +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const isActive = searchParams.get('isActive'); + + const quarters = await getAllQuarters( + isActive !== null ? { isActive: isActive === 'true' } : undefined + ); + + // 为每个季度添加关键词计数 + const { prisma } = await import('@/lib/prisma'); + const quartersWithCount = await Promise.all( + quarters.map(async (q) => { + const count = await prisma.keyword.count({ + where: { quarterId: q.id }, + }); + return { + ...q, + keywordCount: count, + }; + }) + ); + + return NextResponse.json({ + success: true, + quarters: quartersWithCount, + }); + } catch (error) { + console.error('Error fetching quarters:', error); + return NextResponse.json( + { + success: false, + error: 'Failed to fetch quarters', + }, + { status: 500 } + ); + } +}