整合資料庫、完成登入註冊忘記密碼功能

This commit is contained in:
2025-09-09 12:00:22 +08:00
parent af88c0f037
commit 32b19e9a0f
85 changed files with 11672 additions and 2350 deletions

View File

@@ -0,0 +1,103 @@
import { NextRequest, NextResponse } from 'next/server'
import { UserService } from '@/lib/services/database-service'
const userService = new UserService()
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await userService.findById(params.id)
if (!user) {
return NextResponse.json(
{ error: '用戶不存在' },
{ status: 404 }
)
}
// 獲取用戶統計
const stats = await userService.getUserStatistics(params.id)
return NextResponse.json({
success: true,
data: {
user,
stats
}
})
} catch (error) {
console.error('獲取用戶詳情錯誤:', error)
return NextResponse.json(
{ error: '獲取用戶詳情時發生錯誤' },
{ status: 500 }
)
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const updates = await request.json()
// 移除不允許更新的欄位
delete updates.id
delete updates.created_at
delete updates.password_hash
const updatedUser = await userService.update(params.id, updates)
if (!updatedUser) {
return NextResponse.json(
{ error: '用戶不存在或更新失敗' },
{ status: 404 }
)
}
return NextResponse.json({
success: true,
message: '用戶資料已更新',
data: updatedUser
})
} catch (error) {
console.error('更新用戶錯誤:', error)
return NextResponse.json(
{ error: '更新用戶時發生錯誤' },
{ status: 500 }
)
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
// 軟刪除:將 is_active 設為 false
const result = await userService.update(params.id, { is_active: false })
if (!result) {
return NextResponse.json(
{ error: '用戶不存在或刪除失敗' },
{ status: 404 }
)
}
return NextResponse.json({
success: true,
message: '用戶已刪除'
})
} catch (error) {
console.error('刪除用戶錯誤:', error)
return NextResponse.json(
{ error: '刪除用戶時發生錯誤' },
{ status: 500 }
)
}
}