應用 APP 功能實作
This commit is contained in:
119
app/api/admin/apps/[id]/route.ts
Normal file
119
app/api/admin/apps/[id]/route.ts
Normal 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 }
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user