107 lines
3.0 KiB
TypeScript
107 lines
3.0 KiB
TypeScript
import Database from 'better-sqlite3'
|
|
import { join } from 'path'
|
|
|
|
let db: Database.Database | null = null
|
|
|
|
export interface Sermon {
|
|
id?: number
|
|
slug: string
|
|
title: string
|
|
date: string
|
|
bible_references: string
|
|
personal_appliance: string
|
|
pastors_challenge: string
|
|
created_at?: string
|
|
}
|
|
|
|
export interface User {
|
|
id?: number
|
|
username: string
|
|
password: string
|
|
}
|
|
|
|
export function getDatabase() {
|
|
if (!db) {
|
|
const dbPath = join(process.cwd(), 'data', 'sermons.db')
|
|
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,
|
|
archived INTEGER DEFAULT 0,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`)
|
|
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE NOT NULL,
|
|
password TEXT NOT NULL
|
|
)
|
|
`)
|
|
|
|
// Insert default admin user (password: admin123)
|
|
// In production, this should be hashed properly
|
|
const userExists = db.prepare('SELECT COUNT(*) as count FROM users WHERE username = ?').get('admin') as { count: number }
|
|
if (userExists.count === 0) {
|
|
db.prepare('INSERT INTO users (username, password) VALUES (?, ?)').run('admin', 'admin123')
|
|
}
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
export function getAllSermons(limit?: number, includeArchived: boolean = false) {
|
|
const db = getDatabase()
|
|
const whereClause = includeArchived ? '' : 'WHERE archived = 0'
|
|
|
|
if (limit) {
|
|
return db.prepare(`SELECT * FROM sermons ${whereClause} ORDER BY date DESC LIMIT ?`).all(limit) 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) {
|
|
const db = getDatabase()
|
|
return db.prepare('SELECT * FROM sermons WHERE slug = ?').get(slug) as Sermon | undefined
|
|
}
|
|
|
|
export function createSermon(sermon: Sermon) {
|
|
const db = getDatabase()
|
|
const stmt = db.prepare(`
|
|
INSERT INTO sermons (slug, title, date, bible_references, personal_appliance, pastors_challenge)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
`)
|
|
return stmt.run(
|
|
sermon.slug,
|
|
sermon.title,
|
|
sermon.date,
|
|
sermon.bible_references,
|
|
sermon.personal_appliance,
|
|
sermon.pastors_challenge
|
|
)
|
|
}
|
|
|
|
export function getUserByUsername(username: string) {
|
|
const db = getDatabase()
|
|
return db.prepare('SELECT * FROM users WHERE username = ?').get(username) as User | undefined
|
|
}
|