Add archive functionality: database schema, API endpoint, and helper functions

This commit is contained in:
2025-10-01 23:49:44 -04:00
parent 160d898d94
commit 4b47f56b30
2 changed files with 70 additions and 15 deletions

View File

@@ -0,0 +1,42 @@
import { isAuthenticated } from '~/server/utils/auth'
import { archiveSermon } 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'
})
}
try {
const result = archiveSermon(parseInt(id))
if (result.changes === 0) {
throw createError({
statusCode: 404,
message: 'Sermon not found'
})
}
return {
success: true,
message: 'Sermon archived successfully'
}
} catch (error: any) {
throw createError({
statusCode: 500,
message: error.message || 'Failed to archive sermon'
})
}
})

View File

@@ -26,18 +26,19 @@ export function getDatabase() {
db = new Database(dbPath)
// Create tables if they don't exist
db.exec(`
CREATE TABLE IF NOT EXISTS sermons (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
date TEXT NOT NULL,
bible_references TEXT NOT NULL,
personal_appliance TEXT NOT NULL,
pastors_challenge TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
db.exec(`
CREATE TABLE IF NOT EXISTS sermons (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
date TEXT NOT NULL,
bible_references TEXT NOT NULL,
personal_appliance TEXT NOT NULL,
pastors_challenge TEXT NOT NULL,
archived INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
db.exec(`
CREATE TABLE IF NOT EXISTS users (
@@ -58,12 +59,24 @@ export function getDatabase() {
return db
}
export function getAllSermons(limit?: number) {
export function getAllSermons(limit?: number, includeArchived: boolean = true) {
const db = getDatabase()
const whereClause = includeArchived ? '' : 'WHERE archived = 0'
if (limit) {
return db.prepare('SELECT * FROM sermons ORDER BY date DESC LIMIT ?').all(limit) as Sermon[]
return db.prepare(`SELECT * FROM sermons ${whereClause} ORDER BY date DESC LIMIT ?`).all(limit) as Sermon[]
}
return db.prepare('SELECT * FROM sermons ORDER BY date DESC').all() as Sermon[]
return db.prepare(`SELECT * FROM sermons ${whereClause} ORDER BY date DESC`).all() as Sermon[]
}
export function getArchivedSermons() {
const db = getDatabase()
return db.prepare('SELECT * FROM sermons WHERE archived = 1 ORDER BY date DESC').all() as Sermon[]
}
export function archiveSermon(id: number) {
const db = getDatabase()
return db.prepare('UPDATE sermons SET archived = 1 WHERE id = ?').run(id)
}
export function getSermonBySlug(slug: string) {