43 lines
1.2 KiB
TypeScript
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 }
|
|
})
|
|
|