Files
psalmtafel-webservice/lib/db/songbook.ts
T
rudi d03ff9d26d Some changes in manage.ts --> 3 user.ts, song.ts, songbook.ts
Aufspaltung auf 3 Dateien, aber man muss noch weiter arbeiten

(Task: Von better-sqlite3 --> ORM)
2026-07-30 14:13:02 +02:00

234 lines
6.6 KiB
TypeScript

import { prisma } from "@/lib/db/prisma";
/**
* getSongBooks
* Gibt alle Songbücher aus, mit allen Liedern
* [{"id":number, "title": string, "songs":[{"id":number, "title": string},...]},...]
*/
export async function getSongBooks() {
try{
const songbooks = await prisma.songBook.findMany({
include: {
songs: {
select: {
song_number: true,
song: true,
},
orderBy: {
song_number: "asc",
},
},
},
});
return { success: true, songBooks: songbooks };
} catch(error){
return { success: false, error: error }
}
}
/**
* getSongBooks
* Gibt ein Songbuch aus, mit allen Liedern
* {"id":number, "title": string, "songs":[{"id":number, "title": string},...]}
*/
export async function getSongBook(songbookid: number) {
try{
const songbook = await prisma.songBook.findMany({
where: {
id: songbookid,
},
include: {
songs: {
select: {
songNumber: true,
song: true,
},
orderBy: {
songNumber: "asc",
},
},
},
});
return { success: true, songBook: songbook };
} catch(error){
return { success: false, error: error }
}
}
/**
* GetSongs
* Gibt alle Songs zurück.
* [{"id": int, "title": string}]
*/
export async function getSongs() {
try {
const songs = prisma.song.findMany({
select: {
id: true,
title: true,
},
orderBy: [
{ id: "asc" },
],
});
return songs;
} catch (error) {
return { success: false, error: error };
}
}
export async function getSong(songBookId: number, songId: string) {
try {
const song = await prisma.songBookSongs.findFirst({
where: {
songBookId: songBookId,
songId: songId,
},
select: {
songNumber: true,
book: {
select: {
title: true,
},
},
song: {
select: {
title: true,
},
},
},
});
return song ? {songbook_name: song.book.name, number: song.songNumber, song_title: song.song.title,}:null;
} catch (error) {
console.error("Fehler in getSong:", error);
return { success: false, error: error };
}
}
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 { success: false, error: error };
}
}
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, error: error };
}
}
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, error: error };
}
}
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, error: error };
}
}
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 { success: false, error: error };
}
}
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 { success: false, error: error };
}
}