45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { db } from '~/drizzle/db'
|
|
import { changelogEntries } from '~/drizzle/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = event.context.user
|
|
|
|
if (!user) {
|
|
throw createError({ statusCode: 401, message: '未授权访问' })
|
|
}
|
|
|
|
if (!['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
|
throw createError({ statusCode: 403, message: '只有管理员才能创建更新日志' })
|
|
}
|
|
|
|
const body = await readBody(event)
|
|
const version = typeof body?.version === 'string' ? body.version.trim() : ''
|
|
const title = typeof body?.title === 'string' ? body.title.trim() : ''
|
|
const content = typeof body?.content === 'string' ? body.content.trim() : ''
|
|
const published = body?.published === true
|
|
|
|
if (!version) {
|
|
throw createError({ statusCode: 400, message: 'version 不能为空' })
|
|
}
|
|
if (!title) {
|
|
throw createError({ statusCode: 400, message: 'title 不能为空' })
|
|
}
|
|
if (!content) {
|
|
throw createError({ statusCode: 400, message: 'content 不能为空' })
|
|
}
|
|
|
|
const rows = await db
|
|
.insert(changelogEntries)
|
|
.values({
|
|
version,
|
|
title,
|
|
content,
|
|
published,
|
|
publishedAt: published ? new Date() : null,
|
|
createdBy: user.id
|
|
})
|
|
.returning()
|
|
|
|
return rows[0]
|
|
})
|