Files
psalmtafel-webservice/lib/db/songbooksongs.ts
2026-08-27 14:59:19 +02:00

230 lines
7.8 KiB
TypeScript

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: string | undefined
}, song: {id: number, title: string} | undefined }
| { success: false; status: number; error: 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
* @returns {"success": boolean, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}}
*/
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" };
}
const entry = await prisma.songBookSongs.create({
data: {
songId,
songBookId,
songNumber,
},
});
return { success: true, status: 200, entry };
} catch (error) {
if (error.code === "P2002") {
return { success: false, status: 409, error: "Song is already in this book" };
}
console.error("Error in addSongToBook:", error);
return { success: false, status: 500, 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, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string}
*/
export async function updateSongInBook(rawSongId: number, rawSongBookId: number, data: {number: string}) {
try {
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" };
}
const entry = await prisma.songBookSongs.update({
where: {
songBookId_songId: {
songId,
songBookId,
},
},
data: {
songNumber,
},
select: {
songId: true,
songBookId: true,
songNumber: true,
},
});
return { success: true, status: 200, entry };
} catch (error: any) {
// P2025 = Record to update not found
if (error?.code === "P2025") {
return { success: false, status: 404, error: "Song is not assigned to this songbook" };
}
// P2002 = Unique Constraint Violation
if (error?.code === "P2002") {
return { success: false, status: 409, error: "Song number already exists in this songbook" };
}
console.error("Error in updateSongInBook:", error);
return { success: false, status: 500, 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
* @returns {"success": boolean, "status": number}
*/
export async function removeSongFromBook(rawSongId: number, rawSongBookId: number) {
try {
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,
songBookId,
},
},
});
return { success: true, status: 200, entry };
} catch (error) {
if (error.code === "P2025") {
return { success: false, status: 404, error: "Song assignment not found" };
}
console.error("Error in removeSongFromBook:", error);
return { success: false, status: 500, 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, "status": number, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
*/
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,
songNumber,
},
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
});
if (!entry) {
return { success: false, status: 404, error: "No song found for this number" };
}
return { success: true, status: 200, entry };
} catch (error) {
console.error("Error in getSongByNumber:", error);
return { success: false, status: 500, 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(songBookId: number, songNumber: string): Promise<boolean> {
try {
const existing = await prisma.songBookSongs.findFirst({
where: {
songBookId: songBookId,
songNumber: songNumber,
},
select: {
songId: true,
},
});
return existing != null;
} catch (error) {
console.error("Error in isSongNumberTaken:", error);
return true; // If Error->Number is taken
}
}