Files
BYW-VoiceHub/server/api/admin/changelog/[id].put.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

70 lines
2.1 KiB
TypeScript

import { eq } from 'drizzle-orm'
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 id = Number(getRouterParam(event, 'id'))
if (!Number.isFinite(id) || id <= 0) {
throw createError({ statusCode: 400, message: '无效的 id' })
}
const body = await readBody(event)
const updateData: any = { updatedAt: new Date() }
if (body?.version !== undefined) {
if (body.version !== null && typeof body.version !== 'string') {
throw createError({ statusCode: 400, message: 'version 必须是字符串' })
}
updateData.version = (body.version || '').trim()
}
if (body?.title !== undefined) {
if (body.title !== null && typeof body.title !== 'string') {
throw createError({ statusCode: 400, message: 'title 必须是字符串' })
}
updateData.title = (body.title || '').trim()
}
if (body?.content !== undefined) {
if (body.content !== null && typeof body.content !== 'string') {
throw createError({ statusCode: 400, message: 'content 必须是字符串' })
}
updateData.content = (body.content || '').trim()
}
if (body?.published !== undefined) {
if (typeof body.published !== 'boolean') {
throw createError({ statusCode: 400, message: 'published 必须是布尔值' })
}
updateData.published = body.published
updateData.publishedAt = body.published ? new Date() : null
}
if (Object.keys(updateData).length <= 1) {
throw createError({ statusCode: 400, message: '没有可更新的字段' })
}
const rows = await db
.update(changelogEntries)
.set(updateData)
.where(eq(changelogEntries.id, id))
.returning()
if (!rows[0]) {
throw createError({ statusCode: 404, message: '更新日志不存在' })
}
return rows[0]
})