From 583ef90a7b4ffed60cf9b18282c87fc88460df3f Mon Sep 17 00:00:00 2001 From: Robin Schanbacher Date: Thu, 27 Aug 2026 16:26:45 +0200 Subject: [PATCH] fix: POST currentID --- app/api/stream/currentID/route.ts | 57 ++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/app/api/stream/currentID/route.ts b/app/api/stream/currentID/route.ts index 31abe54..7d1b2fa 100644 --- a/app/api/stream/currentID/route.ts +++ b/app/api/stream/currentID/route.ts @@ -1,7 +1,7 @@ // @wikiCategory: Stream import { NextResponse } from "next/server"; -import { getSong } from "@/lib/db/managing"; +import { getSongById } from "@/lib/db/song"; let currentSong = { songID: "", @@ -19,23 +19,43 @@ export async function GET() { export async function POST(request: Request) { try { const data = await request.json(); - const songID = data.songID; - const songbookID = data.songbookID; + const { songID, songbookID } = data; if (!songID || !songbookID) { - return NextResponse.json({ success: false, error: "Fields incomplete!" }, { status: 400 }); + return NextResponse.json( + { success: false, error: "Fields incomplete!" }, + { status: 400 } + ); } - const song = await getSong(Number(songbookID), String(songID)); - - if (song) { - currentSong = { - songID: String(songID), - songbook: song.songbook_name, - number: song.number, - title: song.song_title, - }; + const result = await getSongById(songID); + + if (result.success && result.song) { + const song = result.song; + + // looking for the songbook with matching ID + const matchedBook = song.songBooks.find( + (entry) => String(entry.book.id) === String(songbookID) + ); + + if (matchedBook) { + currentSong = { + songID: String(song.id), + songbook: matchedBook.book.title, + number: String(matchedBook.songNumber), + title: song.title, + }; + } else { + // song does exist but not in the given songbook + currentSong = { + songID: "", + songbook: "", + number: "", + title: "", + }; + } } else { + // song does not exist currentSong = { songID: "", songbook: "", @@ -43,10 +63,15 @@ export async function POST(request: Request) { title: "", }; } - console.log(currentSong); - return NextResponse.json({ success: true }); + console.log("Aktualisierter currentSong:", currentSong); + + return NextResponse.json({ success: true, currentSong }); } catch (error) { - return NextResponse.json({ success: false, error: "Server-Fehler" }, { status: 500 }); + console.error("API POST Error:", error); + return NextResponse.json( + { success: false, error: "Server-Fehler" }, + { status: 500 } + ); } } \ No newline at end of file