96 lines
2.3 KiB
TypeScript
96 lines
2.3 KiB
TypeScript
import { createError, defineEventHandler, readBody } from 'h3'
|
|
import { db } from '~/drizzle/db'
|
|
import { songBlacklists } from '~/drizzle/schema'
|
|
import { and, eq } from 'drizzle-orm'
|
|
import {
|
|
BLACKLIST_TYPES,
|
|
BLACKLIST_LANGUAGE_VALUES,
|
|
BLACKLIST_GENRE_VALUES
|
|
} from '~~/server/config/constants'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// 检查认证和权限
|
|
const user = event.context.user
|
|
if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: '需要管理员权限'
|
|
})
|
|
}
|
|
|
|
const body = await readBody(event)
|
|
const { type, value, reason } = body
|
|
|
|
// 验证输入
|
|
if (!type || !value) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '类型和值不能为空'
|
|
})
|
|
}
|
|
|
|
if (!Object.values(BLACKLIST_TYPES).includes(type)) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '无效的黑名单类型'
|
|
})
|
|
}
|
|
|
|
// 语种/曲风为固定候选值,防止录入无法匹配的自由文本
|
|
if (type === BLACKLIST_TYPES.LANGUAGE && !BLACKLIST_LANGUAGE_VALUES.includes(value.trim())) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '无效的语种候选值'
|
|
})
|
|
}
|
|
if (type === BLACKLIST_TYPES.GENRE && !BLACKLIST_GENRE_VALUES.includes(value.trim())) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '无效的曲风候选值'
|
|
})
|
|
}
|
|
|
|
try {
|
|
// 检查是否已存在
|
|
const existing = await db
|
|
.select()
|
|
.from(songBlacklists)
|
|
.where(and(eq(songBlacklists.type, type), eq(songBlacklists.value, value.trim())))
|
|
.limit(1)
|
|
|
|
if (existing.length > 0) {
|
|
throw createError({
|
|
statusCode: 409,
|
|
message: '该项目已在黑名单中'
|
|
})
|
|
}
|
|
|
|
// 创建黑名单项
|
|
const blacklistItem = await db
|
|
.insert(songBlacklists)
|
|
.values({
|
|
type,
|
|
value: value.trim(),
|
|
reason: reason?.trim() || null,
|
|
createdBy: user.id,
|
|
isActive: true
|
|
})
|
|
.returning()
|
|
|
|
return {
|
|
success: true,
|
|
message: '黑名单项添加成功',
|
|
item: blacklistItem[0]
|
|
}
|
|
} catch (error) {
|
|
if (error.statusCode) {
|
|
throw error
|
|
}
|
|
console.error('添加黑名单项失败:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: '添加黑名单项失败'
|
|
})
|
|
}
|
|
})
|