SongBookSongs for connection between tables
Die Verbindung zwischen den Songs und den SongBooks wurde erstellt. TODO: - Weitere Functionen implementieren (bei bedarf) - Testing
This commit is contained in:
1 parent
3c20c0a70b
commit
df8a768e3e
1 file changed
+182
@@ -0,0 +1,182 @@
|
||||
import { prisma } from "@/lib/db/prisma";
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @returns {"success": boolean, "entry": {"songId": number, "bookId": number, "songNumber": number}}
|
||||
*/
|
||||
export async function addSongToBook(songId: number, bookId: number, songNumber: number) {
|
||||
try {
|
||||
const isTaken = await isSongNumberTaken(bookId, songNumber);
|
||||
if (isTaken) {
|
||||
return { success: false, error: "Songnumber already taken in this songbook" };
|
||||
}
|
||||
|
||||
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" };
|
||||
}
|
||||
|
||||
console.error("Error in addSongToBook:", error);
|
||||
return { success: false, error: "Error while adding song to book" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateSongInBook
|
||||
* Updates the song number for a song in a specific songbook.
|
||||
*
|
||||
* @param songId - ID of the song
|
||||
* @param bookId - ID of the songbook
|
||||
* @param data - Object of the new data
|
||||
* @returns {"success": true, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string}
|
||||
*/
|
||||
export async function updateSongInBook(songId: number, bookId: number, data: {songNumber: number}) {
|
||||
try {
|
||||
// 1. Prüfen, ob die neue Nummer bereits vergeben ist
|
||||
const isTaken = await isSongNumberTaken(bookId, data.songNumber);
|
||||
if (isTaken) {
|
||||
return { success: false, error: "New song number is already taken in this songbook" };
|
||||
}
|
||||
|
||||
// 2. Eintrag über den Composite Primary Key (songId_bookId) aktualisieren
|
||||
const entry = await prisma.songBook.update({
|
||||
where: {
|
||||
songId_bookId: {
|
||||
songId,
|
||||
bookId,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
songNumber: data.songNumber,
|
||||
},
|
||||
select: {
|
||||
songId: true,
|
||||
bookId: true,
|
||||
songNumber: true,
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true, entry };
|
||||
} catch (error: any) {
|
||||
// P2025 = Record to update not found (Verknüpfung existiert noch gar nicht)
|
||||
if (error?.code === "P2025") {
|
||||
return { success: false, error: "Song is not assigned to this songbook" };
|
||||
}
|
||||
|
||||
// P2002 = Unique Constraint Violation (z.B. neue Nummer doch schon besetzt)
|
||||
if (error?.code === "P2002") {
|
||||
return { success: false, error: "Song number already exists in this songbook" };
|
||||
}
|
||||
|
||||
console.error("Error in updateSongInBook:", error);
|
||||
return { success: false, error: "Error while updating song in 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: {
|
||||
songId,
|
||||
bookId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
if (error.code === "P2025") {
|
||||
return { success: false, error: "Song assignment not found" };
|
||||
}
|
||||
|
||||
console.error("Error in removeSongFromBook:", error);
|
||||
return { success: false, error: "Error while removing song from book" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the song assigned to a specific song number in a songbook.
|
||||
*
|
||||
* @param bookId - The ID of the songbook
|
||||
* @param songNumber - The song number in that book
|
||||
* @returns {"success": true, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
|
||||
*/
|
||||
export async function getSongByNumber(bookId: number, songNumber: number) {
|
||||
try {
|
||||
const entry = await prisma.songBookSongs.findFirst({
|
||||
where: {
|
||||
songBookId: bookId,
|
||||
songNumber: songNumber,
|
||||
},
|
||||
select: {
|
||||
songNumber: true,
|
||||
song: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!entry) {
|
||||
return { success: false, error: "No song found for this number" };
|
||||
}
|
||||
|
||||
return { success: true, entry };
|
||||
} catch (error) {
|
||||
console.error("Error in getSongByNumber:", error);
|
||||
return { success: false, error: "Error while fetching song by number" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a song number is already taken in a specific songbook.
|
||||
*
|
||||
* @param bookId - The ID of the songbook
|
||||
* @param songNumber - The song number to check
|
||||
* @returns true if taken, false if available
|
||||
*/
|
||||
export async function isSongNumberTaken(bookId: number, songNumber: number): Promise<boolean> {
|
||||
try {
|
||||
const existing = await prisma.songBook.findFirst({
|
||||
where: {
|
||||
bookId,
|
||||
songNumber,
|
||||
},
|
||||
select: {
|
||||
songId: true,
|
||||
},
|
||||
});
|
||||
|
||||
return existing !== null;
|
||||
} catch (error) {
|
||||
console.error("Error in isSongNumberTaken:", error);
|
||||
return true; // If Error->Number is taken
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user