應用 APP 功能實作

This commit is contained in:
2025-09-09 18:18:02 +08:00
parent 22bbe64349
commit 900e33aefa
22 changed files with 2745 additions and 242 deletions

View File

@@ -0,0 +1,77 @@
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: { id: string } }) {
try {
const { id: appId } = await params
const { searchParams } = new URL(request.url)
const page = parseInt(searchParams.get('page') || '1')
const limit = parseInt(searchParams.get('limit') || '10')
const offset = (page - 1) * limit
// 獲取應用評價列表
const reviews = await appService.getAppReviews(appId, limit, offset)
// 獲取評價總數
const totalReviews = await appService.getAppReviewCount(appId)
return NextResponse.json({
success: true,
data: {
reviews,
pagination: {
page,
limit,
total: totalReviews,
totalPages: Math.ceil(totalReviews / limit)
}
}
})
} 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 body = await request.json()
const { reviewId } = body
if (!reviewId) {
return NextResponse.json(
{ success: false, error: '缺少評價ID' },
{ status: 400 }
)
}
// 刪除評價
const result = await appService.deleteReview(reviewId, 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 }
)
}
}

View File

@@ -0,0 +1,119 @@
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: { id: string } }) {
try {
const { id: appId } = await params
const app = await appService.getAppById(appId)
if (!app) {
return NextResponse.json(
{ success: false, error: '應用不存在' },
{ status: 404 }
)
}
return NextResponse.json({
success: true,
data: {
app: {
...app,
status: app.is_active ? 'published' : 'draft',
views: app.views_count || 0,
likes: app.likes_count || 0,
rating: app.rating || 0,
creator: app.creator_name || '未知',
department: app.creator_department || '未知',
createdAt: app.created_at ? new Date(app.created_at).toLocaleDateString('zh-TW') : '-'
}
}
})
} catch (error) {
console.error('獲取應用詳情錯誤:', error)
return NextResponse.json(
{ success: false, error: '獲取應用詳情時發生錯誤' },
{ status: 500 }
)
}
}
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
try {
const { id: appId } = await params
const body = await request.json()
const { name, description, category, type, app_url, icon, icon_color } = body
// 更新應用
const result = await appService.updateApp(appId, {
name,
description,
category,
type,
app_url,
icon,
icon_color
})
if (result.success) {
return NextResponse.json({
success: true,
message: '應用更新成功',
data: {
app: {
...result.app,
status: result.app?.is_active ? 'published' : 'draft',
views: result.app?.views_count || 0,
likes: result.app?.likes_count || 0,
rating: result.app?.rating || 0,
creator: result.app?.creator_name || '未知',
department: result.app?.creator_department || '未知',
createdAt: result.app?.created_at ? new Date(result.app.created_at).toLocaleDateString('zh-TW') : '-'
}
}
})
} 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 DELETE(request: NextRequest, { params }: { params: { id: string } }) {
try {
const { id: appId } = await params
const result = await appService.deleteApp(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 }
)
}
}

View File

@@ -0,0 +1,46 @@
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: { id: string } }) {
try {
const { id: appId } = await params
// 獲取應用基本統計
const app = await appService.getAppById(appId)
if (!app) {
return NextResponse.json(
{ success: false, error: '應用不存在' },
{ status: 404 }
)
}
// 獲取評分統計
const ratingStats = await appService.getAppRatingStats(appId)
// 獲取使用統計
const usageStats = await appService.getAppUsageStats(appId)
return NextResponse.json({
success: true,
data: {
basic: {
views: app.views_count || 0,
likes: app.likes_count || 0,
rating: ratingStats.averageRating || 0,
reviewCount: ratingStats.totalRatings || 0
},
usage: usageStats,
rating: ratingStats
}
})
} catch (error) {
console.error('獲取應用統計數據錯誤:', error)
return NextResponse.json(
{ success: false, error: '獲取統計數據時發生錯誤' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,43 @@
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 result = await appService.toggleAppStatus(appId)
if (result.success) {
return NextResponse.json({
success: true,
message: '應用狀態更新成功',
data: {
app: {
...result.app,
status: result.app?.is_active ? 'published' : 'draft',
views: result.app?.views_count || 0,
likes: result.app?.likes_count || 0,
rating: result.app?.rating || 0,
creator: result.app?.creator_name || '未知',
department: result.app?.creator_department || '未知',
createdAt: result.app?.created_at ? new Date(result.app.created_at).toLocaleDateString('zh-TW') : '-'
}
}
})
} else {
return NextResponse.json(
{ success: false, error: result.error },
{ status: 400 }
)
}
} catch (error) {
console.error('切換應用狀態錯誤:', error)
return NextResponse.json(
{ success: false, error: '切換應用狀態時發生錯誤' },
{ status: 500 }
)
}
}