25 lines
800 B
TypeScript
25 lines
800 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { userService } from '@/lib/database'
|
|
|
|
// GET - 獲取所有用戶
|
|
export async function GET() {
|
|
try {
|
|
const users = await userService.getAllUsers()
|
|
return NextResponse.json(users)
|
|
} catch (error) {
|
|
console.error('獲取用戶列表失敗:', error)
|
|
return NextResponse.json({ error: '獲取用戶列表失敗' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// POST - 創建新用戶
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const user = await userService.createUser(body)
|
|
return NextResponse.json(user, { status: 201 })
|
|
} catch (error) {
|
|
console.error('創建用戶失敗:', error)
|
|
return NextResponse.json({ error: '創建用戶失敗' }, { status: 500 })
|
|
}
|
|
}
|