Files
psalmtafel-webservice/lib/db/songbook.ts
T
rudi a81ea69a8a Added Statuscodes
Status Rückmeldungen den 'returns' hinzugefügt.
Schanbro/PsalmTafel#30
2026-07-31 13:57:05 +02:00

252 lines
7.2 KiB
TypeScript

import { prisma } from "@/lib/db/prisma";
/**
* GetSongBooks
* Returns all available songbooks
*
* @returns {"success": boolean, "songBooks": [{"id": number, "title": string, "language": string},...]}
*/
export async function getSongBooks() {
try {
const songBooks = await prisma.songBook.findMany({
select: {
id: true,
title: true,
language: true,
},
orderBy: {
title: "asc",
},
});
return { success: true, songBooks };
} catch (error) {
console.error("Error in getSongBooks:", error);
return { success: false, error: "Error while loading SongBooks" };
}
}
/**
* GetSongBookById
* Returns a songbook by its ID along with all included songs sorted by song number
*
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function getSongBookById(id: number) {
try {
const songBook = await prisma.songBook.findUnique({
where: { id },
select: {
id: true,
title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc",
},
},
},
});
if (!songBook) {
return { success: false, error: "SongBook not found" };
}
return { success: true, songBook };
} catch (error) {
console.error("Error in getSongBookById:", error);
return { success: false, error: "Error while loading SongBook" };
}
}
/**
* SearchSongBooksByTitle
* Searches for songbooks matching the given title query, including their songs.
*
* @param query - The search term for the songbook title.
* @param limit - Max number of songbooks returned for performance (default: 20).
* @returns Object with success state and an array of matching songbooks.
*/
export async function searchSongBookByTitle(query: string, limit: number = 20) {
try {
const cleanQuery = query.trim();
if (!cleanQuery) {
return { success: true, songBooks: [] };
}
const songBooks = await prisma.songBook.findMany({
where: {
title: {
contains: cleanQuery,
mode: "insensitive", // Textsearch
},
},
select: {
id: true,
title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc",
},
},
},
take: limit,
});
return { success: true, songBooks };
} catch (error) {
console.error("Error in searchSongBooksByTitle:", error);
return { success: false, error: "Error while searching SongBooks" };
}
}
/**
* CreateSongBook
* Creates a new songbook
*
* @param data - object with title and language
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
*/
export async function createSongBook(data: { title: string; language: string }) {
try {
const songBook = await prisma.songBook.create({
data: {
title: data.title,
language: data.language,
},
select: {
id: true,
title: true,
language: true,
},
});
return { success: true, songBook };
} catch (error) {
if (error.code === "P2002") {
return { success: false, error: "SongBook title already exists" };
}
console.error("Error in createSongBook:", error);
return { success: false, error: "Error while creating SongBook" };
}
}
/**
* UpdateSongBook
* Updates an existing songbook by ID
*
* @param id - ID of the songbook to update
* @param data - object with optional title and language
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
*/
export async function updateSongBook(
id: number,
data: { title?: string; language?: string }
) {
try {
// Prepare update fields dynamically
const updateData: { title?: string; language?: string } = {};
if (data.title) {
updateData.title = data.title;
}
if (data.language) {
updateData.language = data.language;
}
// If no fields to update were provided
if (Object.keys(updateData).length === 0) {
return { success: false, error: "No fields provided to update" };
}
const songBook = await prisma.songBook.update({
where: { id },
data: updateData,
select: {
id: true,
title: true,
language: true,
},
});
return { success: true, songBook };
} catch (error: any) {
if (error.code === "P2025") {
return { success: false, error: "SongBook not found" };
}
if (error.code === "P2002") {
return { success: false, error: "SongBook title already exists" };
}
console.error("Error in updateSongBook:", error);
return { success: false, error: "Error while updating SongBook" };
}
}
/**
* DeleteSongBook
* Deletes a songbook by its ID and returns it
*
* @param id - ID indicating the songbook
* @returns {"success": boolean, "songBook": object}
*/
export async function deleteSongBook(id: number) {
try {
const songBook = await prisma.songBook.delete({
where: { id },
select: {
id: true,
title: true,
language: true,
songs: {
select: {
songNumber: true,
song: {
select: {
id: true,
title: true,
},
},
},
orderBy: {
songNumber: "asc",
},
},
},
});
if (!songBook) {
return { success: false, error: "SongBook not found" };
}
return { success: true, songBook };
} catch (error) {
console.error("Error in deleteSongBook:", error);
return { success: false, error: "Error while deleting SongBook" };
}
}