API work on SongbookSongs

This commit is contained in:
Rudi committed 2026-08-27 14:59:19 +02:00
1 parent 4bdf03426c
commit b060ab29cf
10 files changed
+347 -119

No files matched your search

+56 -15
View File
@@ -1,10 +1,11 @@
import { prisma } from "@/lib/db/prisma";
import { parseInt } from "@/lib/helper/checkvar";
export type DbResponse =
| { success: true; status: number, entry: {
songId: number | undefined;
songBookId: number | undefined;
songNumber: number | undefined
songNumber: string | undefined
}, song: {id: number, title: string} | undefined }
| { success: false; status: number; error: string };
@@ -17,8 +18,20 @@ export type DbResponse =
* @param songNumber - Track/Song number in that specific book
* @returns {"success": boolean, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}}
*/
export async function addSongToBook(songId: number, songBookId: number, songNumber: number): Promise<DbResponse> {
export async function addSongToBook(rawSongId: number, rawSongBookId: number, rawSongNumber: string): Promise<DbResponse> {
try {
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "SongId has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "SongbookId has to be Integer" };
}
const songNumber = rawSongNumber?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const isTaken = await isSongNumberTaken(songBookId, songNumber);
if (isTaken) {
return { success: false, status: 409, error: "Songnumber already taken in this songbook" };
@@ -54,9 +67,21 @@ export async function addSongToBook(songId: number, songBookId: number, songNumb
* @param data - Object of the new data
* @returns {"success": true, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string}
*/
export async function updateSongInBook(songId: number, songBookId: number, data: {songNumber: number}) {
export async function updateSongInBook(rawSongId: number, rawSongBookId: number, data: {number: string}) {
try {
const isTaken = await isSongNumberTaken(songBookId, data.songNumber);
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songNumber = data.number?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const isTaken = await isSongNumberTaken(songBookId, songNumber);
if (isTaken) {
return { success: false, status: 409, error: "New song number is already taken in this songbook" };
}
@@ -69,7 +94,7 @@ export async function updateSongInBook(songId: number, songBookId: number, data:
},
},
data: {
songNumber: data.songNumber,
songNumber,
},
select: {
songId: true,
@@ -103,9 +128,17 @@ export async function updateSongInBook(songId: number, songBookId: number, data:
* @param bookId - ID of the songbook
* @returns {"success": boolean, "status": number}
*/
export async function removeSongFromBook(songId: number, songBookId: number) {
export async function removeSongFromBook(rawSongId: number, rawSongBookId: number) {
try {
await prisma.songBookSongs.delete({
const songId = parseInt(rawSongId);
if (!songId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const entry = await prisma.songBookSongs.delete({
where: {
songBookId_songId: {
songId,
@@ -114,7 +147,7 @@ export async function removeSongFromBook(songId: number, songBookId: number) {
},
});
return { success: true, status: 200 };
return { success: true, status: 200, entry };
} catch (error) {
if (error.code === "P2025") {
return { success: false, status: 404, error: "Song assignment not found" };
@@ -132,12 +165,20 @@ export async function removeSongFromBook(songId: number, songBookId: number) {
* @param songNumber - The song number in that book
* @returns {"success": true, "status": number, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
*/
export async function getSongByNumber(bookId: number, songNumber: number) {
export async function getSongByNumber(rawSongBookId: number, rawSongNumber: string) {
try {
const songBookId = parseInt(rawSongBookId);
if (!songBookId) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songNumber = rawSongNumber?.trim() ?? null;
if(!songNumber){
return { success: false, status: 400, error: "Field 'SongNumber' ist required" };
}
const entry = await prisma.songBookSongs.findFirst({
where: {
songBookId: bookId,
songNumber: songNumber,
songBookId,
songNumber,
},
select: {
songNumber: true,
@@ -168,19 +209,19 @@ export async function getSongByNumber(bookId: number, songNumber: number) {
* @param songNumber - The song number to check
* @returns true if taken, false if available
*/
export async function isSongNumberTaken(songBookId: number, songNumber: number): Promise<boolean> {
export async function isSongNumberTaken(songBookId: number, songNumber: string): Promise<boolean> {
try {
const existing = await prisma.songBookSongs.findFirst({
where: {
songBookId,
songNumber,
songBookId: songBookId,
songNumber: songNumber,
},
select: {
songId: true,
},
});
return existing !== null;
return existing != null;
} catch (error) {
console.error("Error in isSongNumberTaken:", error);
return true; // If Error->Number is taken