實作個人收藏、個人活動紀錄

This commit is contained in:
2025-09-11 17:40:07 +08:00
parent bc2104d374
commit 9c5dceb001
29 changed files with 3781 additions and 601 deletions

View File

@@ -0,0 +1,79 @@
import { NextRequest, NextResponse } from 'next/server'
import { AppService } from '@/lib/services/database-service'
const appService = new AppService()
// 獲取用戶活動記錄
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
if (!userId) {
return NextResponse.json(
{ success: false, error: '用戶ID不能為空' },
{ status: 400 }
)
}
// 獲取用戶最近使用的應用
const recentApps = await appService.getUserRecentApps(userId, 10)
// 獲取用戶統計數據
const userStats = await appService.getUserActivityStats(userId)
// 獲取類別使用統計
const categoryStats = await appService.getUserCategoryStats(userId)
return NextResponse.json({
success: true,
data: {
recentApps,
userStats,
categoryStats
}
})
} catch (error) {
console.error('獲取用戶活動記錄錯誤:', error)
return NextResponse.json(
{ success: false, error: '獲取活動記錄時發生錯誤' },
{ status: 500 }
)
}
}
// 記錄用戶活動
export async function POST(request: NextRequest) {
try {
const { userId, action, resourceType, resourceId, details } = await request.json()
if (!userId || !action || !resourceType) {
return NextResponse.json(
{ success: false, error: '缺少必要參數' },
{ status: 400 }
)
}
// 記錄活動到資料庫
const activityId = await appService.logUserActivity({
userId,
action,
resourceType,
resourceId,
details
})
return NextResponse.json({
success: true,
data: { activityId }
})
} catch (error) {
console.error('記錄用戶活動錯誤:', error)
return NextResponse.json(
{ success: false, error: '記錄活動時發生錯誤' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,44 @@
import { NextRequest, NextResponse } from 'next/server'
import { AppService } from '@/lib/services/database-service'
const appService = new AppService()
// 獲取用戶收藏列表
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
const page = parseInt(searchParams.get('page') || '1')
const limit = parseInt(searchParams.get('limit') || '20')
if (!userId) {
return NextResponse.json(
{ success: false, error: '用戶ID不能為空' },
{ status: 400 }
)
}
const offset = (page - 1) * limit
const result = await appService.getUserFavorites(userId, limit, offset)
return NextResponse.json({
success: true,
data: {
apps: result.apps,
pagination: {
page,
limit,
total: result.total,
totalPages: Math.ceil(result.total / limit)
}
}
})
} catch (error) {
console.error('獲取用戶收藏列表錯誤:', error)
return NextResponse.json(
{ success: false, error: '獲取收藏列表時發生錯誤' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,41 @@
import { NextRequest, NextResponse } from 'next/server'
import { AppService } from '@/lib/services/database-service'
const appService = new AppService()
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
if (!userId) {
return NextResponse.json(
{ success: false, error: '缺少用戶ID' },
{ status: 400 }
)
}
// 直接查詢收藏的應用ID不依賴 apps 表的 is_active 狀態)
const favoriteAppIds = await appService.getUserFavoriteAppIds(userId)
console.log('用戶收藏的應用ID列表:', favoriteAppIds)
// 獲取用戶的按讚列表
const likedAppIds = await appService.getUserLikedApps(userId)
console.log('用戶按讚的應用ID列表:', likedAppIds)
return NextResponse.json({
success: true,
data: {
favorites: favoriteAppIds,
likes: likedAppIds
}
})
} catch (error) {
console.error('獲取用戶互動狀態錯誤:', error)
return NextResponse.json(
{ success: false, error: '獲取用戶互動狀態時發生錯誤' },
{ status: 500 }
)
}
}