Files
ExecuBoard/app/api/kpi/route.ts
2025-08-01 00:55:05 +08:00

32 lines
1006 B
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { kpiService } from '@/lib/database'
// GET - 獲取 KPI 列表
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
if (!userId) {
return NextResponse.json({ error: '缺少用戶 ID' }, { status: 400 })
}
const kpis = await kpiService.getKPIsByUser(userId)
return NextResponse.json(kpis)
} catch (error) {
console.error('獲取 KPI 失敗:', error)
return NextResponse.json({ error: '獲取 KPI 失敗' }, { status: 500 })
}
}
// POST - 創建新 KPI
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const kpi = await kpiService.createKPI(body)
return NextResponse.json(kpi, { status: 201 })
} catch (error) {
console.error('創建 KPI 失敗:', error)
return NextResponse.json({ error: '創建 KPI 失敗' }, { status: 500 })
}
}