New SiteContent.
This commit is contained in:
parent
02222a5c16
commit
7e7672d52b
5 changed files with 285 additions and 0 deletions
28
server/api/admin/site-content/[key].put.js
Normal file
28
server/api/admin/site-content/[key].put.js
Normal 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
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue