From 5ca801e9b40617d2fca0849e3225d85f022ed742 Mon Sep 17 00:00:00 2001 From: Mayo Date: Fri, 31 Jul 2026 15:56:27 +0200 Subject: [PATCH] Added status Codes to SongBook --- lib/db/songbook.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/db/songbook.ts b/lib/db/songbook.ts index f80c00d..e37604e 100644 --- a/lib/db/songbook.ts +++ b/lib/db/songbook.ts @@ -20,10 +20,10 @@ export async function getSongBooks() { }, }); - return { success: true, songBooks }; + return { success: true, status: 200, songBooks }; } catch (error) { console.error("Error in getSongBooks:", error); - return { success: false, error: "Error while loading SongBooks" }; + return { success: false, status: 500, error: "Error while loading SongBooks" }; } } @@ -60,13 +60,13 @@ export async function getSongBookById(id: number) { }); if (!songBook) { - return { success: false, error: "SongBook not found" }; + return { success: false, status: 404, error: "SongBook not found" }; } - return { success: true, songBook }; + return { success: true, status: 200, songBook }; } catch (error) { console.error("Error in getSongBookById:", error); - return { success: false, error: "Error while loading SongBook" }; + return { success: false, status: 500, error: "Error while loading SongBook" }; } } @@ -82,7 +82,7 @@ export async function searchSongBooksByTitle(query: string, limit: number = 20) try { const cleanQuery = query.trim(); if (!cleanQuery) { - return { success: true, songBooks: [] }; + return { success: true, status: 200, songBooks: [] }; } const songBooks = await prisma.songBook.findMany({ @@ -113,10 +113,10 @@ export async function searchSongBooksByTitle(query: string, limit: number = 20) take: limit, }); - return { success: true, songBooks }; + return { success: true, status: 200, songBooks }; } catch (error) { console.error("Error in searchSongBooksByTitle:", error); - return { success: false, error: "Error while searching SongBooks" }; + return { success: false, status: 500, error: "Error while searching SongBooks" }; } } @@ -141,14 +141,14 @@ export async function createSongBook(data: { title: string; language: string }) }, }); - return { success: true, songBook }; + return { success: true, status: 201, songBook }; } catch (error) { if (error.code === "P2002") { - return { success: false, error: "SongBook title already exists" }; + return { success: false, status: 409, error: "SongBook title already exists" }; } console.error("Error in createSongBook:", error); - return { success: false, error: "Error while creating SongBook" }; + return { success: false, status: 500, error: "Error while creating SongBook" }; } } @@ -178,7 +178,7 @@ export async function updateSongBook( // If no fields to update were provided if (Object.keys(updateData).length === 0) { - return { success: false, error: "No fields provided to update" }; + return { success: false, status: 400, error: "No fields provided to update" }; } const songBook = await prisma.songBook.update({ where: { id }, @@ -190,13 +190,13 @@ export async function updateSongBook( }, }); - return { success: true, songBook }; + return { success: true, status: 200, songBook }; } catch (error: any) { if (error.code === "P2025") { - return { success: false, error: "SongBook not found" }; + return { success: false, status: 404, error: "SongBook not found" }; } if (error.code === "P2002") { - return { success: false, error: "SongBook title already exists" }; + return { success: false, status: 500, error: "SongBook title already exists" }; } console.error("Error in updateSongBook:", error); @@ -237,13 +237,13 @@ export async function deleteSongBook(id: number) { }); if (!songBook) { - return { success: false, error: "SongBook not found" }; + return { success: false, status: 404, error: "SongBook not found" }; } - return { success: true, songBook }; + return { success: true, status: 200, songBook }; } catch (error) { console.error("Error in deleteSongBook:", error); - return { success: false, error: "Error while deleting SongBook" }; + return { success: false, status: 500, error: "Error while deleting SongBook" }; } }