diff --git a/app/admin/page.tsx b/app/admin/page.tsx index 99b4f3e..727c07b 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -118,7 +118,29 @@ export default function AdminPage() { }; const attachSong = async () => { - + if (!selectedSongbookId || !number || !selectedSong) { + alert("Alle Felder ausfüllen!"); + return; + } + + try { + const res = await fetch("/api/songs/attach", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + songbookId: selectedSongbookId, + songId: selectedSong.song_id, + number: number, + }) + }); + if (res.ok) { + alert("Loed erfolgreich zum Liederbuch hinzugefügt!"); + } else { + alert("Fehler beim Senden."); + } + } catch (error) { + console.error("Failed sending current songID:", error); + } } const addSongbook = async () => { @@ -319,8 +341,8 @@ export default function AdminPage() { setTitle(e.target.value)} + value={number} + onChange={(e) => setNumber(e.target.value)} /> { - db.prepare(` - INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number) - VALUES (?, ?, ?) - `).run(songbookId, songId, number); - }) - runTransaction(); - return { success: true } + const songExists = db.prepare("SELECT id FROM songs WHERE id = ?").get(songId); + if (!songExists) { + console.error(`Song mit ID ${songId} existiert nicht in DB.`); + return { success: false, error: "Song nicht gefunden." }; + } + + const bookExists = db.prepare("SELECT id FROM songbooks WHERE id = ?").get(songbookId); + if (!bookExists) { + console.error(`Songbook mit ID ${songbookId} existiert nicht in DB.`); + return { success: false, error: "Liederbuch nicht gefunden." }; + } + + db.prepare(` + INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number) + VALUES (?, ?, ?) + `).run(songbookId, songId, number); + + return { success: true }; } catch (error) { - console.error("Error in addSongToSongbook:", error); + console.error("Error in attachSongToSongbook:", error); return { success: false }; } }