44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { getDatabase } from '~/server/utils/database'
|
|
import { verifyJWT, generateSlug } from '~/server/utils/auth'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const db = await getDatabase()
|
|
|
|
const body = await readBody(event)
|
|
const { title, date, bibleReferences, personalApplication, pastorChallenge } = body
|
|
|
|
if (!title || !date) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Title and date are required'
|
|
})
|
|
}
|
|
|
|
const slug = generateSlug(title, date)
|
|
|
|
try {
|
|
const result = db.prepare(`
|
|
INSERT INTO sermons (title, date, slug, bible_references, personal_application, pastor_challenge)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
`).run(title, date, slug, JSON.stringify(bibleReferences || []), personalApplication || '', pastorChallenge || '')
|
|
|
|
return {
|
|
id: result.lastInsertRowid,
|
|
title,
|
|
date,
|
|
slug,
|
|
bibleReferences: bibleReferences || [],
|
|
personalApplication: personalApplication || '',
|
|
pastorChallenge: pastorChallenge || ''
|
|
}
|
|
} catch (error: any) {
|
|
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
|
|
throw createError({
|
|
statusCode: 409,
|
|
statusMessage: 'A sermon with this date already exists'
|
|
})
|
|
}
|
|
throw error
|
|
}
|
|
})
|