first commit
This commit is contained in:
commit
7bf0cb4f05
27 files changed
+5490
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { Noto_Sans_Tamil_Supplement } from "next/font/google";
|
||||
import path from "path";
|
||||
|
||||
const dbPath = path.join(process.cwd(), "songs.db");
|
||||
|
||||
// Verhindert, dass Next.js bei jedem Reload die DB neu öffnet
|
||||
const globalForDb = globalThis as unknown as { db: Database.Database | undefined };
|
||||
|
||||
export const db = globalForDb.db ?? new Database(dbPath);
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForDb.db = db;
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS songs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS songbooks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
language TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS songbook_songs (
|
||||
songbook_id INTEGER,
|
||||
song_id INTEGER,
|
||||
song_number TEXT,
|
||||
PRIMARY KEY (songbook_id, song_id),
|
||||
FOREIGN KEY (songbook_id) REFERENCES songbooks(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (song_id) REFERENCES songs(id) ON DELETE CASCADE
|
||||
);
|
||||
`)
|
||||
|
||||
export async function getSongs() {
|
||||
try {
|
||||
const songs = db.prepare(`
|
||||
SELECT
|
||||
ss.songbook_id,
|
||||
ss.song_id,
|
||||
ss.song_number,
|
||||
s.title
|
||||
FROM songbook_songs ss
|
||||
JOIN songs s ON ss.song_id = s.id
|
||||
ORDER BY ss.songbook_id, ss.song_number ASC
|
||||
`).all();
|
||||
return songs;
|
||||
} catch (error) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSong(songbookId: number, songId: string) {
|
||||
try {
|
||||
const song = db.prepare(`
|
||||
SELECT
|
||||
sb.name AS songbook_name,
|
||||
ss.song_number AS number,
|
||||
s.title AS song_title
|
||||
FROM songbook_songs ss
|
||||
JOIN songbooks sb ON ss.songbook_id = sb.id
|
||||
JOIN songs s ON ss.song_id = s.id
|
||||
WHERE ss.songbook_id = ? AND ss.song_id = ?
|
||||
`).get(songbookId, songId) as { songbook_name: string; number: string; song_title: string } | undefined;
|
||||
|
||||
return song || null;
|
||||
} catch (error) {
|
||||
console.error("Fehler in getSong:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function addSongToSongbook(title:string, songbookId:number, number:string) {
|
||||
try {
|
||||
const runTransaction = db.transaction(() => {
|
||||
let song = db.prepare("SELECT id FROM songs WHERE title = ?").get(title) as { id: number } | undefined
|
||||
let songId: number | bigint;
|
||||
|
||||
if (!song) {
|
||||
const insertSong = db.prepare("INSERT INTO songs (title) VALUES (?)").run(title);
|
||||
songId = insertSong.lastInsertRowid;
|
||||
} else {
|
||||
songId = song.id;
|
||||
}
|
||||
|
||||
db.prepare(`
|
||||
INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number)
|
||||
VALUES (?, ?, ?)
|
||||
`).run(songbookId, songId, number);
|
||||
})
|
||||
runTransaction();
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
console.error("Error in addSongToSongbook:", error);
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSongbooks() {
|
||||
try {
|
||||
const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all();
|
||||
return songbooks;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function addSongbook(name:String, language:String) {
|
||||
try {
|
||||
const info = db.prepare(`
|
||||
INSERT INTO songbooks (name, language)
|
||||
VALUES (?, ?)
|
||||
`).run(name, language || null);
|
||||
|
||||
const newBook = {
|
||||
id: info.lastInsertRowid,
|
||||
name,
|
||||
language
|
||||
}
|
||||
return newBook;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user