New SiteContent.

This commit is contained in:
Jennie Robinson Faber 2026-04-16 21:11:14 +01:00
parent 02222a5c16
commit 7e7672d52b
5 changed files with 285 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import SiteContent from '../../../models/siteContent.js'
import { requireAdmin } from '../../../utils/auth.js'
import { validateBody } from '../../../utils/validateBody.js'
import { SITE_CONTENT_KEYS, siteContentUpsertSchema } from '../../../utils/schemas.js'
export default defineEventHandler(async (event) => {
await requireAdmin(event)
const key = getRouterParam(event, 'key')
if (!SITE_CONTENT_KEYS.includes(key)) {
throw createError({ statusCode: 404, statusMessage: 'Unknown content key' })
}
const body = await validateBody(event, siteContentUpsertSchema)
const doc = await SiteContent.findOneAndUpdate(
{ key },
{ $set: { title: body.title || '', body: body.body || '' } },
{ upsert: true, new: true, runValidators: true }
).lean()
return {
key: doc.key,
title: doc.title || '',
body: doc.body || '',
updatedAt: doc.updatedAt || null
}
})

View file

@ -0,0 +1,18 @@
import SiteContent from '../../models/siteContent.js'
import { SITE_CONTENT_KEYS } from '../../utils/schemas.js'
export default defineEventHandler(async (event) => {
const key = getRouterParam(event, 'key')
if (!SITE_CONTENT_KEYS.includes(key)) {
throw createError({ statusCode: 404, statusMessage: 'Unknown content key' })
}
const doc = await SiteContent.findOne({ key }).lean()
return {
key,
title: doc?.title || '',
body: doc?.body || '',
updatedAt: doc?.updatedAt || null
}
})

View file

@ -0,0 +1,5 @@
import { SITE_CONTENT_KEYS } from '../../utils/schemas.js'
export default defineEventHandler(() => {
return { keys: [...SITE_CONTENT_KEYS] }
})

View file

@ -0,0 +1,9 @@
import mongoose from 'mongoose'
const siteContentSchema = new mongoose.Schema({
key: { type: String, required: true, unique: true, index: true },
title: { type: String, default: '' },
body: { type: String, default: '' },
}, { timestamps: true })
export default mongoose.models.SiteContent || mongoose.model('SiteContent', siteContentSchema)