Files
BYW-VoiceHub/server/api/songs/[id]/comments/[commentId].delete.ts
T
Henry_Du 4ce4649aaf
Build VoiceHub FPK / build-fpk (push) Failing after 56m5s
Build and Push Docker Image / build-and-push (push) Canceled after 1h9m35s
初次提交
2026-09-06 08:19:02 +08:00

43 lines
1.2 KiB
TypeScript

import { and, eq } from 'drizzle-orm'
import { db } from '~/drizzle/db'
import { songComments } from '~/drizzle/schema'
export default defineEventHandler(async (event) => {
const user = event.context.user
if (!user) {
throw createError({ statusCode: 401, message: '未授权访问' })
}
const songId = Number(getRouterParam(event, 'id'))
const commentId = Number(getRouterParam(event, 'commentId'))
if (!Number.isFinite(songId) || songId <= 0) {
throw createError({ statusCode: 400, message: '无效的 songId' })
}
if (!Number.isFinite(commentId) || commentId <= 0) {
throw createError({ statusCode: 400, message: '无效的 commentId' })
}
const rows = await db
.select()
.from(songComments)
.where(and(eq(songComments.id, commentId), eq(songComments.songId, songId)))
.limit(1)
const comment = rows[0]
if (!comment) {
throw createError({ statusCode: 404, message: '评论不存在' })
}
const canDelete =
comment.userId === user.id || ['ADMIN', 'SUPER_ADMIN', 'SONG_ADMIN'].includes(user.role)
if (!canDelete) {
throw createError({ statusCode: 403, message: '无权删除该评论' })
}
await db.delete(songComments).where(eq(songComments.id, commentId))
return { success: true }
})