import { prisma } from "@/lib/db/prisma"; // For Error checking use this import: import { Prisma } from "@prisma/client"; /** * GetSongs * Returns all songs * * @returns {"success": boolean, "status": number, "songs": [{"id":number, "title": string},...]} */ export async function getSongs() { try{ const songs = await prisma.song.findMany({ select: { id: true, title: true, }, orderBy: [ { id: "asc" }, ], }); return { success: true, status: 200, songs }; } catch(error) { console.error("Error in getSongs", error); return { success: false, status: 500, error: "Error while loading Songs" }; } } /** * GetSongById * Returns a song by his ID * @param id - ID indicatin the song * @returns {"success": boolean, "status": number, "song": {"id":number, "title": string, "songBooks": [{"songNumber": number, "book": {"title": string, "language": string}},...]}} */ export async function getSongById(id: number) { try{ const song = await prisma.song.findUnique({ where: { id }, select: { id: true, title: true, songBooks: { select: { songNumber: true, book: { select: { title: true, language: true, } } }, }, } }); if (!song) { return { success: false, status: 404, error: "Song not found" }; } return { success: true, status: 200, song }; } catch(error) { console.error("Error in getSongById", error); return { success: false, status: 500, error: "Error while loading Song" }; } } /** * SearchSongsByTitle * Searches for songs matching the given title query. * * @param query - The search term for the song title. * @param limit - Max number of songs returned for performance (default: 20). * @returns @returns {"success": boolean, "status": number, "song": {"id":number, "title": string, "songBooks": [{"songNumber": number, "book": {"title": string, "language": string}},...]}} */ export async function searchSongsByTitle(query: string, limit: number = 20) { try { const cleanQuery = query.trim(); if (!cleanQuery) { return { success: true, songBooks: [] }; } const songs = await prisma.song.findMany({ where: { title: { contains: query.trim(), mode: "insensitive", // Textsearch }, }, select: { id: true, title: true, songBooks: { select: { songNumber: true, book: { select: { title: true, language: true, }, }, }, }, }, take: limit, }); return { success: true, status: 200, songs }; } catch (error) { console.error("Error in searchSongsByTitle:", error); return { success: false, status: 500, error: "Error while searching Songs" }; } } /** * CreateSong * Creates an song and returns this song * @param data - object with title * @returns {"success": boolean, "status": number, "song": {"id":number, "title": string}} */ export async function createSong(data: { title: string }) { try{ const song = await prisma.song.create({ data: { title: data.title }, select: { id: true, title: true, } }); if (!song) { return { success: false, status: 500, error: "Could not create song" }; } return { success: true, status: 201, song }; } catch(error) { console.error("Error in createSong", error); // P2002 = Unique Constraint Violation (username) if (error.code === "P2002") { return { success: false, status: 409, error: "Song already exists" }; } return { success: false, status: 500, error: "Error while creating Song" }; } } /** * UpdateSong * Updates an existing song and returns the updated song * @param id - ID indicatin the song * @param data - object with title * @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}} */ export async function updateSong( id: number, data: { title?: string } ) { try { // Prepare update fields dynamically const updateData: { title?: string } = {}; if (data.title) { updateData.title = data.title; } // 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 song = await prisma.song.update({ where: { id }, data: updateData, select: { id: true, title: true, } }); return { success: true, status: 200, song }; } catch (error) { console.error("Error in updateSong", error); // P2025 = Record to update not found if (error.code === "P2025") { return { success: false, status: 404, error: "Song not found" }; } // P2002 = Unique Constraint Violation (username) if (error.code === "P2002") { return { success: false, status: 409, error: "Song already exists" }; } return { success: false, status: 500, error: "Error while updating Song" }; } } /** * DeleteSong * Deletes an existing song and returns the deleted song * @param id - ID indicatin the song * @returns {"success": boolean, "status": number, "song": {"id": number, "title": string}} */ export async function deleteSong(id: number) { try { const song = await prisma.song.delete({ where: { id }, select: { id: true, title: true, } }); return { success: true, status: 200, song }; } catch (error) { console.error("Error in deleteSong", error); // P2025 = Record to update not found if (error.code === "P2025") { return { success: false, status: 404, error: "Song not found" }; } return { success: false, status: 500, error: "Error while deleting Song" }; } }