Add complete edit functionality for sermons with update API endpoint and enhanced Bible reference management
This commit is contained in:
58
server/api/sermons/update/[id].put.ts
Normal file
58
server/api/sermons/update/[id].put.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { isAuthenticated } from '~/server/utils/auth'
|
||||
import { getDatabase } from '~/server/utils/database'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
// Check authentication
|
||||
if (!isAuthenticated(event)) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Unauthorized'
|
||||
})
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
if (!id) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Sermon ID is required'
|
||||
})
|
||||
}
|
||||
|
||||
const body = await readBody(event)
|
||||
const { slug, title, date, bible_references, personal_appliance, pastors_challenge } = body
|
||||
|
||||
if (!slug || !title || !date || !bible_references || !personal_appliance || !pastors_challenge) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'All fields are required'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const db = getDatabase()
|
||||
const result = db.prepare(`
|
||||
UPDATE sermons
|
||||
SET slug = ?, title = ?, date = ?, bible_references = ?,
|
||||
personal_appliance = ?, pastors_challenge = ?
|
||||
WHERE id = ?
|
||||
`).run(slug, title, date, bible_references, personal_appliance, pastors_challenge, parseInt(id))
|
||||
|
||||
if (result.changes === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: 'Sermon not found'
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Sermon updated successfully'
|
||||
}
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: error.message || 'Failed to update sermon'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user