hulai-website/server/api/admin/stories.post.js

36 行
1.2 KiB
JavaScript

import { useDB, schema } from '../../utils/db.js'
import { requireAdmin } from '../../utils/auth.js'
export default defineEventHandler(async (event) => {
requireAdmin(event)
const body = await readBody(event)
if (!body?.title || !body?.slug) {
throw createError({ statusCode: 422, message: '标题和 slug 不能为空' })
}
const db = useDB()
const result = db.insert(schema.stories).values({
slug: body.slug,
title: body.title,
subtitle: body.subtitle || '',
familyType: body.familyType || '',
childrenAge: body.childrenAge || '',
fromCity: body.fromCity || '',
tripProduct: body.tripProduct || '',
travelDate: body.travelDate || '',
coverImage: body.coverImage || '',
summary: body.summary || '',
author: body.author || '',
sourceUrl: body.sourceUrl || '',
tags: JSON.stringify(Array.isArray(body.tags) ? body.tags : []),
sections: body.sections ? JSON.stringify(body.sections) : '[]',
keyMoments: body.keyMoments ? JSON.stringify(body.keyMoments) : '[]',
seoTitle: body.seoTitle || '',
seoDesc: body.seoDesc || '',
published: body.published !== false ? 1 : 0,
}).run()
return { success: true, id: result.lastInsertRowid }
})