Added Delete Function

This commit is contained in:
rudi committed 2026-07-31 10:17:45 +02:00
1 parent df169d2943
commit 3c20c0a70b
1 file changed
+33 -46
+33 -46
View File
@@ -53,7 +53,7 @@ export async function getSongBookById(id: number) {
},
},
orderBy: {
songNumber: "asc", // Songs direkt nach Liednummer sortiert zurückgeben
songNumber: "asc",
},
},
},
@@ -154,59 +154,46 @@ export async function createSongBook(data: { title: string; language: string })
}
/**
* AddSongToBook
* Connects an existing song to a songbook with a specific song number
* DeleteSongBook
* Deletes a songbook by its ID and returns it
*
* @param songId - ID of the song
* @param bookId - ID of the songbook
* @param songNumber - Track/Song number in that specific book
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function addSongToBook(songId: number, bookId: number, songNumber: number) {
export async function deleteSongBook(id: number) {
try {
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: {
songId_bookId: { // Composite Primary/Unique Key in Prisma (Prisma-Standard für Pivot-Tabellen)
songId,
bookId,
const songBook = await prisma.songBookModel.delete({
where: { id },
select: {
id: true,
title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc",
},
},
},
});
return { success: true };
} catch (error) {
if (error.code === "P2025") {
return { success: false, error: "Song assignment not found" };
if (!songBook) {
return { success: false, error: "SongBook not found" };
}
console.error("Error in removeSongFromBook:", error);
return { success: false, error: "Error while removing song from book" };
return { success: true, songBook };
} catch (error) {
console.error("Error in deleteSongBook:", error);
return { success: false, error: "Error while deleting SongBook" };
}
}