實作個人收藏、個人活動紀錄
This commit is contained in:
124
app/api/apps/[id]/favorite/route.ts
Normal file
124
app/api/apps/[id]/favorite/route.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { AppService } from '@/lib/services/database-service'
|
||||
|
||||
const appService = new AppService()
|
||||
|
||||
// 添加收藏
|
||||
export async function POST(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id: appId } = await params
|
||||
const body = await request.json()
|
||||
const { userId } = body
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '用戶ID不能為空' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 檢查應用是否存在
|
||||
const app = await appService.getAppById(appId)
|
||||
if (!app) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '應用不存在' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
const result = await appService.addFavorite(userId, appId)
|
||||
|
||||
if (result.success) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '收藏成功'
|
||||
})
|
||||
} else {
|
||||
// 如果是重複收藏,返回 409 狀態碼
|
||||
const statusCode = result.error === '已經收藏過此應用' ? 409 : 400
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error },
|
||||
{ status: statusCode }
|
||||
)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('添加收藏錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '添加收藏時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 移除收藏
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id: appId } = await params
|
||||
const { searchParams } = new URL(request.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '用戶ID不能為空' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 移除收藏
|
||||
const result = await appService.removeFavorite(userId, appId)
|
||||
|
||||
if (result.success) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '取消收藏成功'
|
||||
})
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('移除收藏錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '移除收藏時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 檢查收藏狀態
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id: appId } = await params
|
||||
const { searchParams } = new URL(request.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '用戶ID不能為空' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 檢查收藏狀態
|
||||
const isFavorited = await appService.isFavorited(userId, appId)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
isFavorited
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('檢查收藏狀態錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '檢查收藏狀態時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
98
app/api/apps/[id]/interactions/route.ts
Normal file
98
app/api/apps/[id]/interactions/route.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
99
app/api/apps/[id]/reviews/route.ts
Normal file
99
app/api/apps/[id]/reviews/route.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
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 limit = parseInt(searchParams.get('limit') || '5') // 預設5筆
|
||||
const offset = parseInt(searchParams.get('offset') || '0')
|
||||
const page = parseInt(searchParams.get('page') || '1')
|
||||
|
||||
const reviews = await appService.getAppReviews(appId, limit, offset)
|
||||
const totalReviews = await appService.getAppReviewCount(appId)
|
||||
const totalPages = Math.ceil(totalReviews / limit)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
reviews,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total: totalReviews,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1
|
||||
}
|
||||
}
|
||||
})
|
||||
} 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 { userId, rating, comment, reviewId } = await request.json()
|
||||
|
||||
if (!userId || !rating || !comment) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '缺少必要參數' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// 檢查評分範圍
|
||||
if (rating < 1 || rating > 5) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '評分必須在 1-5 之間' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
let resultReviewId: string | null = null
|
||||
|
||||
if (reviewId) {
|
||||
// 更新現有評論
|
||||
resultReviewId = await appService.updateAppReview(reviewId, appId, userId, rating, comment)
|
||||
} else {
|
||||
// 創建新評論
|
||||
resultReviewId = await appService.createAppReview(appId, userId, rating, comment)
|
||||
}
|
||||
|
||||
if (resultReviewId) {
|
||||
// 獲取更新後的評論列表
|
||||
const reviews = await appService.getAppReviews(appId, 10, 0)
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: { reviewId: resultReviewId, reviews }
|
||||
})
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: reviewId ? '更新評論失敗' : '創建評論失敗' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('處理應用評論錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '處理應用評論時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
@@ -6,6 +6,11 @@ const appService = new AppService()
|
||||
export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const { id: appId } = await params
|
||||
const { searchParams } = new URL(request.url)
|
||||
|
||||
// 獲取日期範圍參數
|
||||
const startDate = searchParams.get('startDate')
|
||||
const endDate = searchParams.get('endDate')
|
||||
|
||||
// 獲取應用基本統計
|
||||
const app = await appService.getAppById(appId)
|
||||
@@ -19,8 +24,11 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
|
||||
// 獲取評分統計
|
||||
const ratingStats = await appService.getAppRatingStats(appId)
|
||||
|
||||
// 獲取使用趨勢數據
|
||||
const usageStats = await appService.getAppUsageStats(appId)
|
||||
// 獲取使用趨勢數據(支援日期範圍)
|
||||
const usageStats = await appService.getAppUsageStats(appId, startDate || undefined, endDate || undefined)
|
||||
|
||||
// 獲取收藏數量
|
||||
const favoritesCount = await appService.getAppFavoritesCount(appId)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -28,6 +36,7 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
|
||||
basic: {
|
||||
views: app.views_count || 0,
|
||||
likes: app.likes_count || 0,
|
||||
favorites: favoritesCount,
|
||||
rating: ratingStats.averageRating || 0,
|
||||
reviewCount: ratingStats.totalRatings || 0
|
||||
},
|
||||
|
Reference in New Issue
Block a user