Files
BYW-VoiceHub/server/api/songs/[id]/comments.post.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

37 lines
937 B
TypeScript

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'))
if (!Number.isFinite(songId) || songId <= 0) {
throw createError({ statusCode: 400, message: '无效的 songId' })
}
const body = await readBody(event)
const content = typeof body?.content === 'string' ? body.content.trim() : ''
if (!content) {
throw createError({ statusCode: 400, message: '评论内容不能为空' })
}
if (content.length > 300) {
throw createError({ statusCode: 400, message: '评论内容最多 300 字' })
}
const rows = await db
.insert(songComments)
.values({
songId,
userId: user.id,
content
})
.returning()
return rows[0]
})