SongBook Getter & Setter

SongBook Erstellt
This commit is contained in:
rudi committed 2026-07-31 09:58:47 +02:00
1 parent b5b5f593f5
commit 87a894db1e
1 file changed
+159 -180
+159 -180
View File
@@ -1,233 +1,212 @@
import { prisma } from "@/lib/db/prisma"; import { prisma } from "@/lib/db/prisma";
/** /**
* getSongBooks * GetSongBooks
* Gibt alle Songbücher aus, mit allen Liedern * Returns all available songbooks
* [{"id":number, "title": string, "songs":[{"id":number, "title": string},...]},...] *
* @returns {"success": boolean, "songBooks": [{"id": number, "title": string, "language": string},...]}
*/ */
export async function getSongBooks() { export async function getSongBooks() {
try{ try {
const songbooks = await prisma.songBook.findMany({ const songBooks = await prisma.songBookModel.findMany({ // Name deines Prisma-Modells für Bücher
include: {
songs: {
select: { select: {
song_number: true, id: true,
song: true, title: true,
language: true,
}, },
orderBy: { orderBy: {
song_number: "asc", title: "asc",
},
},
}, },
}); });
return { success: true, songBooks: songbooks };
} catch(error){ return { success: true, songBooks };
return { success: false, error: error } } catch (error) {
console.error("Error in getSongBooks:", error);
return { success: false, error: "Error while loading SongBooks" };
} }
} }
/** /**
* getSongBooks * GetSongBookById
* Gibt ein Songbuch aus, mit allen Liedern * Returns a songbook by its ID along with all included songs sorted by song number
* {"id":number, "title": string, "songs":[{"id":number, "title": string},...]} *
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/ */
export async function getSongBook(songbookid: number) { export async function getSongBookById(id: number) {
try{ try {
const songbook = await prisma.songBook.findMany({ const songBook = await prisma.songBookModel.findUnique({
where: { where: { id },
id: songbookid, select: {
}, id: true,
include: { title: true,
language: true,
songs: { songs: {
select: { select: {
songNumber: true, songNumber: true,
song: true, song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc", // Songs direkt nach Liednummer sortiert zurückgeben
},
},
},
});
if (!songBook) {
return { success: false, error: "SongBook not found" };
}
return { success: true, songBook };
} catch (error) {
console.error("Error in getSongBookById:", error);
return { success: false, error: "Error while loading SongBook" };
}
}
/**
* SearchSongBooksByTitle
* Searches for songbooks matching the given title query, including their songs.
*
* @param query - The search term for the songbook title.
* @param limit - Max number of songbooks returned for performance (default: 20).
* @returns Object with success state and an array of matching songbooks.
*/
export async function searchSongBooksByTitle(query: string, limit: number = 20) {
try {
const cleanQuery = query.trim();
if (!cleanQuery) {
return { success: true, songBooks: [] };
}
const songBooks = await prisma.songBookModel.findMany({
where: {
title: {
contains: cleanQuery,
mode: "insensitive", // Textsearch
},
},
select: {
id: true,
title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
}, },
orderBy: { orderBy: {
songNumber: "asc", songNumber: "asc",
}, },
}, },
}, },
take: limit,
}); });
return { success: true, songBook: songbook };
} catch(error){ return { success: true, songBooks };
return { success: false, error: error } } catch (error) {
console.error("Error in searchSongBooksByTitle:", error);
return { success: false, error: "Error while searching SongBooks" };
} }
} }
/** /**
* GetSongs * CreateSongBook
* Gibt alle Songs zurück. * Creates a new songbook
* [{"id": int, "title": string}] *
* @param data - object with title and language
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
*/ */
export async function getSongs() { export async function createSongBook(data: { title: string; language: string }) {
try { try {
const songs = prisma.song.findMany({ const songBook = await prisma.songBookModel.create({
data: {
title: data.title,
language: data.language,
},
select: { select: {
id: true, id: true,
title: true, title: true,
language: true,
}, },
orderBy: [
{ id: "asc" },
],
}); });
return songs;
return { success: true, songBook };
} catch (error) { } catch (error) {
return { success: false, error: error }; if (error.code === "P2002") {
return { success: false, error: "SongBook title already exists" };
}
console.error("Error in createSongBook:", error);
return { success: false, error: "Error while creating SongBook" };
} }
} }
export async function getSong(songBookId: number, songId: string) { /**
* AddSongToBook
* Connects an existing song to a songbook with a specific song number
*
* @param songId - ID of the song
* @param bookId - ID of the songbook
* @param songNumber - Track/Song number in that specific book
*/
export async function addSongToBook(songId: number, bookId: number, songNumber: number) {
try { try {
const song = await prisma.songBookSongs.findFirst({ const entry = await prisma.songBook.create({ // Verknüpfungs-Tabelle
data: {
songId,
bookId,
songNumber,
},
});
return { success: true, entry };
} catch (error) {
if (error.code === "P2002") {
return { success: false, error: "Song is already in this book or song number is already taken" };
}
console.error("Error in addSongToBook:", error);
return { success: false, error: "Error while adding song to book" };
}
}
/**
* RemoveSongFromBook
* Removes a song from a specific songbook
*
* @param songId - ID of the song
* @param bookId - ID of the songbook
*/
export async function removeSongFromBook(songId: number, bookId: number) {
try {
await prisma.songBook.delete({
where: { where: {
songBookId: songBookId, songId_bookId: { // Composite Primary/Unique Key in Prisma (Prisma-Standard für Pivot-Tabellen)
songId: songId, songId,
}, bookId,
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 }; return { success: true };
} catch (error) { } catch (error) {
console.error("Error in attachSongToSongbook:", error); if (error.code === "P2025") {
return { success: false, error: error }; return { success: false, error: "Song assignment not found" };
} }
}
export async function removeSongfromSongbook(songId: number, songbookId: number) { console.error("Error in removeSongFromBook:", error);
try { return { success: false, error: "Error while removing song from book" };
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 };
} }
} }