197 lines
4.3 KiB
TypeScript
197 lines
4.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// 获取所有场景类别
|
|
export async function GET() {
|
|
try {
|
|
const scenes = await prisma.scene.findMany({
|
|
orderBy: {
|
|
name: 'asc',
|
|
},
|
|
})
|
|
|
|
// 获取每个场景类别下的素材数量
|
|
const scenesWithCount = await Promise.all(
|
|
scenes.map(async (scene) => {
|
|
const count = await prisma.material.count({
|
|
where: {
|
|
sceneName: scene.name,
|
|
},
|
|
})
|
|
return {
|
|
...scene,
|
|
count,
|
|
}
|
|
})
|
|
)
|
|
|
|
return NextResponse.json(scenesWithCount)
|
|
} catch (error) {
|
|
console.error('Error fetching scenes:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取场景类别列表失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 添加新场景类别
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session) {
|
|
return NextResponse.json(
|
|
{ error: '请先登录' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
const { name } = await req.json()
|
|
|
|
if (!name || !name.trim()) {
|
|
return NextResponse.json(
|
|
{ error: '场景类别名称不能为空' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 检查场景类别是否已存在
|
|
const existing = await prisma.scene.findUnique({
|
|
where: { name: name.trim() },
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json(
|
|
{ error: '该场景类别已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 创建新场景类别
|
|
const scene = await prisma.scene.create({
|
|
data: {
|
|
name: name.trim(),
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(scene, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Error creating scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '创建场景类别失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 更新场景类别名称
|
|
export async function PATCH(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '权限不足' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { id, name } = await req.json()
|
|
|
|
if (!id || !name || !name.trim()) {
|
|
return NextResponse.json(
|
|
{ error: '参数错误' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 检查新名称是否已存在(排除当前场景)
|
|
const existing = await prisma.scene.findFirst({
|
|
where: {
|
|
name: name.trim(),
|
|
NOT: { id },
|
|
},
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json(
|
|
{ error: '该场景类别已存在' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 获取旧名称
|
|
const oldScene = await prisma.scene.findUnique({
|
|
where: { id },
|
|
})
|
|
|
|
if (!oldScene) {
|
|
return NextResponse.json(
|
|
{ error: '分类不存在' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// 更新场景类别名称
|
|
const scene = await prisma.scene.update({
|
|
where: { id },
|
|
data: { name: name.trim() },
|
|
})
|
|
|
|
// 同步更新所有使用该场景的素材
|
|
await prisma.material.updateMany({
|
|
where: { sceneName: oldScene.name },
|
|
data: { sceneName: name.trim() },
|
|
})
|
|
|
|
return NextResponse.json(scene)
|
|
} catch (error) {
|
|
console.error('Error updating scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '更新失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 删除场景类别
|
|
export async function DELETE(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any)?.role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '权限不足' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
const id = searchParams.get('id')
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: '缺少ID参数' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 删除场景类别
|
|
await prisma.scene.delete({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error deleting scene:', error)
|
|
return NextResponse.json(
|
|
{ error: '删除失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|