import { NextRequest, NextResponse } from 'next/server' import { AppService } from '@/lib/services/database-service' const appService = new AppService() // 獲取應用的統計數據 export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id: appId } = await params const { searchParams } = new URL(request.url) const userId = searchParams.get('userId') // 獲取應用的統計數據 const stats = await appService.getAppStats(appId, userId || undefined) return NextResponse.json({ success: true, data: { likesCount: stats.likes_count || 0, viewsCount: stats.views_count || 0, rating: stats.average_rating || 0, reviewsCount: stats.reviews_count || 0, userLiked: stats.userLiked || false } }) } catch (error) { console.error('獲取應用統計數據錯誤:', error) return NextResponse.json( { success: false, error: '獲取應用統計數據時發生錯誤' }, { status: 500 } ) } } // 更新應用統計數據(按讚、觀看等) export async function POST( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id: appId } = await params const { action, userId } = await request.json() if (!userId) { return NextResponse.json( { success: false, error: '需要用戶身份驗證' }, { status: 401 } ) } let result = false switch (action) { case 'like': result = await appService.toggleAppLike(appId, userId) break case 'view': result = await appService.incrementAppViews(appId, userId) break case 'favorite': result = await appService.toggleAppFavorite(appId, userId) break default: return NextResponse.json( { success: false, error: '無效的操作類型' }, { status: 400 } ) } if (result) { // 重新獲取更新後的統計數據 const stats = await appService.getAppStats(appId) return NextResponse.json({ success: true, data: { likesCount: stats.likes_count || 0, viewsCount: stats.views_count || 0, rating: stats.average_rating || 0, reviewsCount: stats.reviews_count || 0 } }) } else { return NextResponse.json( { success: false, error: '操作失敗' }, { status: 500 } ) } } catch (error) { console.error('更新應用統計數據錯誤:', error) return NextResponse.json( { success: false, error: '更新應用統計數據時發生錯誤' }, { status: 500 } ) } }