初次提交
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
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]
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user