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
+32 -45
+32 -45
View File
@@ -53,7 +53,7 @@ export async function getSongBookById(id: number) {
}, },
}, },
orderBy: { 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 * DeleteSongBook
* Connects an existing song to a songbook with a specific song number * Deletes a songbook by its ID and returns it
* *
* @param songId - ID of the song * @param id - ID indicating the songbook
* @param bookId - ID of the songbook * @returns {"success": boolean, "songBook": object}
* @param songNumber - Track/Song number in that specific book
*/ */
export async function addSongToBook(songId: number, bookId: number, songNumber: number) { export async function deleteSongBook(id: number) {
try { try {
const entry = await prisma.songBook.create({ // Verknüpfungs-Tabelle const songBook = await prisma.songBookModel.delete({
data: { where: { id },
songId, select: {
bookId, id: true,
songNumber, title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc",
}, },
});
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,
}, },
}, },
}); });
return { success: true }; if (!songBook) {
} catch (error) { return { success: false, error: "SongBook not found" };
if (error.code === "P2025") {
return { success: false, error: "Song assignment not found" };
} }
console.error("Error in removeSongFromBook:", error); return { success: true, songBook };
return { success: false, error: "Error while removing song from book" }; } catch (error) {
console.error("Error in deleteSongBook:", error);
return { success: false, error: "Error while deleting SongBook" };
} }
} }