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

+3
View File
@@ -125,6 +125,9 @@ export async function searchSongsByTitle(query: string, limit: number = 20) {
*/
export async function createSong(data: { title: string }) {
try{
if(!data.title){
return { success: false, status: 400, error: "Field 'title' is required" };
}
const song = await prisma.song.create({
data: {
title: data.title.trim()
+20 -4
View File
@@ -1,4 +1,5 @@
import { prisma } from "@/lib/db/prisma";
import { parseInt } from "@/lib/helper/checkvar";
/**
@@ -34,8 +35,12 @@ export async function getSongBooks() {
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function getSongBookById(id: number) {
export async function getSongBookById(rawId: number) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBook = await prisma.songBook.findUnique({
where: { id },
select: {
@@ -129,10 +134,13 @@ export async function searchSongBooksByTitle(query: string, limit: number = 20)
*/
export async function createSongBook(data: { title: string; language: string }) {
try {
if(!data.title){
return { success: false, status: 400, error: "Field 'title' is required" };
}
const songBook = await prisma.songBook.create({
data: {
title: data.title.trim(),
language: data.language.trim(),
language: data.language?.trim() ?? "",
},
select: {
id: true,
@@ -161,10 +169,14 @@ export async function createSongBook(data: { title: string; language: string })
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
*/
export async function updateSongBook(
id: number,
rawId: number,
data: { title?: string; language?: string }
) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
// Prepare update fields dynamically
const updateData: { title?: string; language?: string } = {};
@@ -211,8 +223,12 @@ export async function updateSongBook(
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function deleteSongBook(id: number) {
export async function deleteSongBook(rawId: number) {
try {
const id = parseInt(rawId);
if (!id) {
return { success: false, status: 400, error: "Id has to be Integer" };
}
const songBook = await prisma.songBook.delete({
where: { id },
select: {
+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