forked from BRETT/BRETT
Initial Commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { unlink } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { eq, sql } from 'drizzle-orm'
|
||||
import { commentAttachments, comments, uploads } from '../../db/schema'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requirePermission(event, 'comment.delete')
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Ungültige ID' })
|
||||
}
|
||||
const db = useDb()
|
||||
const comment = await db
|
||||
.select({ uploadId: comments.uploadId })
|
||||
.from(comments)
|
||||
.where(eq(comments.id, id))
|
||||
.limit(1)
|
||||
.then(r => r[0])
|
||||
const files = await db
|
||||
.select({ fileName: commentAttachments.fileName, cover: commentAttachments.cover })
|
||||
.from(commentAttachments)
|
||||
.where(eq(commentAttachments.commentId, id))
|
||||
await db.delete(comments).where(eq(comments.id, id))
|
||||
if (comment) {
|
||||
await db.update(uploads).set({ commentCount: sql`GREATEST(comment_count - 1, 0)` }).where(eq(uploads.id, comment.uploadId))
|
||||
}
|
||||
for (const f of files) {
|
||||
await unlink(join(uploadDir(), 'attachments', f.fileName)).catch(() => {})
|
||||
if (f.cover) {
|
||||
await unlink(join(process.cwd(), 'public', f.cover)).catch(() => {})
|
||||
}
|
||||
}
|
||||
return { ok: true }
|
||||
})
|
||||
Reference in New Issue
Block a user