134 lines
3.7 KiB
TypeScript
134 lines
3.7 KiB
TypeScript
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 page = parseInt(searchParams.get('page') || '1')
|
|
const limit = parseInt(searchParams.get('limit') || '10')
|
|
const search = searchParams.get('search') || ''
|
|
const category = searchParams.get('category') || 'all'
|
|
const type = searchParams.get('type') || 'all'
|
|
const status = searchParams.get('status') || 'all'
|
|
|
|
// 獲取應用列表
|
|
const { apps, total } = await appService.getAllApps({
|
|
search,
|
|
category,
|
|
type,
|
|
status,
|
|
page,
|
|
limit
|
|
})
|
|
|
|
// 獲取應用統計
|
|
const stats = await appService.getDashboardStats()
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
apps: apps.map(app => ({
|
|
...app,
|
|
status: app.is_active ? 'published' : 'draft',
|
|
views: app.views_count || 0,
|
|
likes: app.likes_count || 0,
|
|
rating: app.rating || 0,
|
|
reviewCount: app.reviewCount || 0,
|
|
creator: app.creator_name || '未知',
|
|
department: app.creator_department || '未知',
|
|
createdAt: app.created_at ? new Date(app.created_at).toLocaleDateString('zh-TW') : '-',
|
|
icon: app.icon || 'Bot',
|
|
iconColor: app.icon_color || 'from-blue-500 to-purple-500',
|
|
appUrl: app.app_url
|
|
})),
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
totalPages: Math.ceil(total / limit)
|
|
},
|
|
stats
|
|
}
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('獲取應用列表錯誤:', error)
|
|
return NextResponse.json(
|
|
{ success: false, error: '獲取應用列表時發生錯誤' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { name, description, creator_id, category, type, app_url, icon, icon_color } = body
|
|
|
|
// 驗證必填欄位
|
|
if (!name || !description || !creator_id || !category || !type) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '請填寫所有必填欄位' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 檢查應用名稱是否已存在
|
|
const existingApp = await appService.getAppByName(name)
|
|
if (existingApp) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '應用名稱已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 創建應用
|
|
const result = await appService.createApp({
|
|
name,
|
|
description,
|
|
creator_id,
|
|
category,
|
|
type,
|
|
app_url,
|
|
icon,
|
|
icon_color
|
|
})
|
|
|
|
if (result.success) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '應用創建成功',
|
|
data: {
|
|
app: {
|
|
...result.app,
|
|
status: 'published',
|
|
views: 0,
|
|
likes: 0,
|
|
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') : '-',
|
|
icon: result.app?.icon || 'Bot',
|
|
iconColor: result.app?.icon_color || 'from-blue-500 to-purple-500',
|
|
appUrl: result.app?.app_url
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
return NextResponse.json(
|
|
{ success: false, error: result.error },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('創建應用錯誤:', error)
|
|
return NextResponse.json(
|
|
{ success: false, error: '創建應用時發生錯誤' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|