fix: POST currentID

This commit is contained in:
robin committed 2026-08-27 16:26:45 +02:00
1 parent 2fb484ad26
commit 583ef90a7b
1 file changed
+40 -15
+40 -15
View File
@@ -1,7 +1,7 @@
// @wikiCategory: Stream // @wikiCategory: Stream
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { getSong } from "@/lib/db/managing"; import { getSongById } from "@/lib/db/song";
let currentSong = { let currentSong = {
songID: "", songID: "",
@@ -19,23 +19,43 @@ export async function GET() {
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
const data = await request.json(); const data = await request.json();
const songID = data.songID; const { songID, songbookID } = data;
const songbookID = data.songbookID;
if (!songID || !songbookID) { 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)); const result = await getSongById(songID);
if (song) { if (result.success && result.song) {
currentSong = { const song = result.song;
songID: String(songID),
songbook: song.songbook_name, // looking for the songbook with matching ID
number: song.number, const matchedBook = song.songBooks.find(
title: song.song_title, (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 { } else {
// song does not exist
currentSong = { currentSong = {
songID: "", songID: "",
songbook: "", songbook: "",
@@ -43,10 +63,15 @@ export async function POST(request: Request) {
title: "", title: "",
}; };
} }
console.log(currentSong);
return NextResponse.json({ success: true }); console.log("Aktualisierter currentSong:", currentSong);
return NextResponse.json({ success: true, currentSong });
} catch (error) { } 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 }
);
} }
} }