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
+38 -13
+38 -13
View File
@@ -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));
const result = await getSongById(songID);
if (song) {
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(songID),
songbook: song.songbook_name,
number: song.number,
title: song.song_title,
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 }
);
}
}