160 lines
4.9 KiB
TypeScript
160 lines
4.9 KiB
TypeScript
import { prisma } from "@/lib/db/prisma";
|
|
|
|
|
|
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 getSongInfos(songId: string) {
|
|
try {
|
|
const songRecords = db.prepare(`
|
|
SELECT
|
|
ss.songbook_id,
|
|
sb.name AS songbook_name,
|
|
ss.song_number,
|
|
s.title
|
|
FROM songbook_songs ss
|
|
JOIN songbooks sb ON ss.songbook_id = sb.id
|
|
JOIN songs s ON ss.song_id = s.id
|
|
WHERE s.id = ?
|
|
`).all(songId) as {
|
|
songbook_id: number;
|
|
songbook_name: string;
|
|
song_number: string;
|
|
title: string;
|
|
}[] | undefined
|
|
|
|
return songRecords || 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 attachSongToSongbook(songId: number, songbookId: number, number: string) {
|
|
try {
|
|
const songExists = db.prepare("SELECT id FROM songs WHERE id = ?").get(songId);
|
|
if (!songExists) {
|
|
console.error(`Song mit ID ${songId} existiert nicht in DB.`);
|
|
return { success: false, error: "Song nicht gefunden." };
|
|
}
|
|
|
|
const bookExists = db.prepare("SELECT id FROM songbooks WHERE id = ?").get(songbookId);
|
|
if (!bookExists) {
|
|
console.error(`Songbook mit ID ${songbookId} existiert nicht in DB.`);
|
|
return { success: false, error: "Liederbuch nicht gefunden." };
|
|
}
|
|
|
|
db.prepare(`
|
|
INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number)
|
|
VALUES (?, ?, ?)
|
|
`).run(songbookId, songId, number);
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Error in attachSongToSongbook:", error);
|
|
return { success: false };
|
|
}
|
|
}
|
|
|
|
export async function removeSongfromSongbook(songId: number, songbookId: number) {
|
|
try {
|
|
const info = db.prepare(`
|
|
DELETE FROM songbook_songs
|
|
WHERE songbook_id = ? AND song_id = ?
|
|
`).run(songbookId, songId);
|
|
|
|
return { success: info.changes > 0 };
|
|
} catch (error) {
|
|
console.error("Error in removeSongfromSongbook:", 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;
|
|
}
|
|
}
|