import { prisma } from "@/lib/db/prisma"; import { parseInt } from "@/lib/helper/checkvar"; /** * 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, status: 200, songBooks }; } catch (error) { console.error("Error in getSongBooks:", error); return { success: false, status: 500, 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(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: { id: true, title: true, language: true, songs: { select: { songNumber: true, song: { select: { id: true, title: true, }, }, }, orderBy: { songNumber: "asc", }, }, }, }); if (!songBook) { return { success: false, status: 404, error: "SongBook not found" }; } return { success: true, status: 200, songBook }; } catch (error) { console.error("Error in getSongBookById:", error); return { success: false, status: 500, 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, status: 200, songBooks: [] }; } const songBooks = await prisma.songBook.findMany({ where: { title: { contains: cleanQuery, }, }, 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, status: 200, songBooks }; } catch (error) { console.error("Error in searchSongBooksByTitle:", error); return { success: false, status: 500, 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 { 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() ?? "", }, select: { id: true, title: true, language: true, }, }); return { success: true, status: 201, songBook }; } catch (error) { if (error.code === "P2002") { return { success: false, status: 409, error: "SongBook title already exists" }; } console.error("Error in createSongBook:", error); return { success: false, status: 500, 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( 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 } = {}; if (data.title) { updateData.title = data.title.trim(); } if (data.language) { updateData.language = data.language.trim(); } // If no fields to update were provided if (Object.keys(updateData).length === 0) { return { success: false, status: 400, 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, status: 200, songBook }; } catch (error: any) { if (error.code === "P2025") { return { success: false, status: 404, error: "SongBook not found" }; } if (error.code === "P2002") { return { success: false, status: 500, 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(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: { id: true, title: true, language: true, songs: { select: { songNumber: true, song: { select: { id: true, title: true, }, }, }, orderBy: { songNumber: "asc", }, }, }, }); if (!songBook) { return { success: false, status: 404, error: "SongBook not found" }; } return { success: true, status: 200, songBook }; } catch (error) { console.error("Error in deleteSongBook:", error); return { success: false, status: 500, error: "Error while deleting SongBook" }; } }