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.songBookModel.findMany({ // Name deines Prisma-Modells für Bücher 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.songBookModel.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 searchSongBooksByTitle(query: string, limit: number = 20) { try { const cleanQuery = query.trim(); if (!cleanQuery) { return { success: true, songBooks: [] }; } const songBooks = await prisma.songBookModel.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.songBookModel.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" }; } } /** * 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.songBookModel.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" }; } }