49 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
// 批量删除素材(软删除或永久删除)
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || (session.user as any)?.role !== 'ADMIN') {
return NextResponse.json({ error: '无权限' }, { status: 403 })
}
const { ids, permanent } = await request.json()
if (!Array.isArray(ids) || ids.length === 0) {
return NextResponse.json({ error: '请提供要删除的素材ID列表' }, { status: 400 })
}
if (permanent) {
// 永久删除:从数据库中彻底删除
await prisma.material.deleteMany({
where: {
id: { in: ids }
}
})
} else {
// 软删除:设置deletedAt字段
await prisma.material.updateMany({
where: {
id: { in: ids }
},
data: {
deletedAt: new Date()
}
})
}
return NextResponse.json({
success: true,
message: permanent ? '批量永久删除成功' : '批量删除成功'
})
} catch (error) {
console.error('Batch delete materials error:', error)
return NextResponse.json({ error: '批量删除失败' }, { status: 500 })
}
}