實作個人收藏、個人活動紀錄
This commit is contained in:
69
app/api/reviews/[id]/votes/route.ts
Normal file
69
app/api/reviews/[id]/votes/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
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: reviewId } = await params
|
||||
const { searchParams } = new URL(request.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
const votes = await appService.getReviewVotes(reviewId, userId || undefined)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: votes
|
||||
})
|
||||
} 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: reviewId } = await params
|
||||
const { userId, isHelpful } = await request.json()
|
||||
|
||||
if (!userId || typeof isHelpful !== 'boolean') {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '缺少必要參數' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const result = await appService.toggleReviewVote(reviewId, userId, isHelpful)
|
||||
|
||||
if (result.success) {
|
||||
// 獲取更新後的投票統計
|
||||
const votes = await appService.getReviewVotes(reviewId, userId)
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: votes
|
||||
})
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: result.error || '投票失敗' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('投票評論錯誤:', error)
|
||||
return NextResponse.json(
|
||||
{ success: false, error: '投票評論時發生錯誤' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user