Notes!
This commit is contained in:
37
server/api/notes/[sermonId].get.ts
Normal file
37
server/api/notes/[sermonId].get.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getSermonNote, getUserByUsername } from '~/server/utils/database'
|
||||
import { getAuthCookie } from '~/server/utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getAuthCookie(event)
|
||||
|
||||
if (!username) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Unauthorized'
|
||||
})
|
||||
}
|
||||
|
||||
const user = getUserByUsername(username)
|
||||
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'User not found'
|
||||
})
|
||||
}
|
||||
|
||||
const sermonId = parseInt(event.context.params?.sermonId || '')
|
||||
|
||||
if (isNaN(sermonId)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid sermon ID'
|
||||
})
|
||||
}
|
||||
|
||||
const note = getSermonNote(user.id!, sermonId)
|
||||
|
||||
return {
|
||||
notes: note?.notes || ''
|
||||
}
|
||||
})
|
||||
50
server/api/notes/[sermonId].post.ts
Normal file
50
server/api/notes/[sermonId].post.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { saveSermonNote, getUserByUsername } from '~/server/utils/database'
|
||||
import { getAuthCookie } from '~/server/utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getAuthCookie(event)
|
||||
|
||||
if (!username) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Unauthorized'
|
||||
})
|
||||
}
|
||||
|
||||
const user = getUserByUsername(username)
|
||||
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'User not found'
|
||||
})
|
||||
}
|
||||
|
||||
const sermonId = parseInt(event.context.params?.sermonId || '')
|
||||
const body = await readBody(event)
|
||||
const { notes } = body
|
||||
|
||||
if (isNaN(sermonId)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid sermon ID'
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof notes !== 'string') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Notes must be a string'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
saveSermonNote(user.id!, sermonId, notes)
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: 'Failed to save notes'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -24,6 +24,15 @@ export interface User {
|
||||
is_admin: number
|
||||
}
|
||||
|
||||
export interface SermonNote {
|
||||
id?: number
|
||||
user_id: number
|
||||
sermon_id: number
|
||||
notes: string
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
export function getDatabase() {
|
||||
if (!db) {
|
||||
const dbPath = join(process.cwd(), 'data', 'sermons.db')
|
||||
@@ -55,6 +64,20 @@ export function getDatabase() {
|
||||
)
|
||||
`)
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS sermon_notes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
sermon_id INTEGER NOT NULL,
|
||||
notes TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (sermon_id) REFERENCES sermons(id) ON DELETE CASCADE,
|
||||
UNIQUE(user_id, sermon_id)
|
||||
)
|
||||
`)
|
||||
|
||||
// Insert default admin user from environment variables with hashed password
|
||||
const config = useRuntimeConfig()
|
||||
const adminUsername = config.adminUsername
|
||||
@@ -148,3 +171,24 @@ export function resetUserPassword(id: number, newPassword: string) {
|
||||
const hashedPassword = bcrypt.hashSync(newPassword, saltRounds)
|
||||
return db.prepare('UPDATE users SET password = ? WHERE id = ?').run(hashedPassword, id)
|
||||
}
|
||||
|
||||
export function getSermonNote(userId: number, sermonId: number) {
|
||||
const db = getDatabase()
|
||||
return db.prepare('SELECT * FROM sermon_notes WHERE user_id = ? AND sermon_id = ?').get(userId, sermonId) as SermonNote | undefined
|
||||
}
|
||||
|
||||
export function saveSermonNote(userId: number, sermonId: number, notes: string) {
|
||||
const db = getDatabase()
|
||||
const existing = getSermonNote(userId, sermonId)
|
||||
|
||||
if (existing) {
|
||||
return db.prepare('UPDATE sermon_notes SET notes = ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ? AND sermon_id = ?').run(notes, userId, sermonId)
|
||||
} else {
|
||||
return db.prepare('INSERT INTO sermon_notes (user_id, sermon_id, notes) VALUES (?, ?, ?)').run(userId, sermonId, notes)
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteSermonNote(userId: number, sermonId: number) {
|
||||
const db = getDatabase()
|
||||
return db.prepare('DELETE FROM sermon_notes WHERE user_id = ? AND sermon_id = ?').run(userId, sermonId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user