// @wikiCategory: Stream import { NextResponse } from "next/server"; import { getSongById } from "@/lib/db/song"; let currentSong = { songID: "", songbook: "", number: "", title: "" }; // @wikiDescription: Akutell gestreamtes Lied abrufen export async function GET() { return NextResponse.json(currentSong) } // @wikiDescription: Lied zum Streamen festlegen export async function POST(request: Request) { try { const data = await request.json(); const { songID, songbookID } = data; if (!songID || !songbookID) { return NextResponse.json( { success: false, error: "Fields incomplete!" }, { status: 400 } ); } 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: "", number: "", title: "", }; } console.log("Aktualisierter currentSong:", currentSong); return NextResponse.json({ success: true, currentSong }); } catch (error) { console.error("API POST Error:", error); return NextResponse.json( { success: false, error: "Server-Fehler" }, { status: 500 } ); } }