feat: add abilty to attach song to songbook

This commit is contained in:
robin committed 2026-07-28 15:53:51 +02:00
1 parent 001a0d6ef8
commit e3115df0bd
3 files changed
+46 -14

No files matched your search

+25 -3
View File
@@ -118,7 +118,29 @@ export default function AdminPage() {
}; };
const attachSong = async () => { 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 () => { const addSongbook = async () => {
@@ -319,8 +341,8 @@ export default function AdminPage() {
<Input.Text <Input.Text
type="text" type="text"
placeholder="12a" placeholder="12a"
value={title} value={number}
onChange={(e) => setTitle(e.target.value)} onChange={(e) => setNumber(e.target.value)}
/> />
</div> </div>
<Input.Button <Input.Button
+2 -2
View File
@@ -6,9 +6,9 @@ export async function POST(request: Request) {
const body = await request.json(); const body = await request.json();
const { songId, songbookId, number } = body; const { songId, songbookId, number } = body;
if (!songId || !songbookId || !number) { if (isNaN(songId) || isNaN(songbookId) || !number) {
return NextResponse.json( return NextResponse.json(
{ error: "songId, songbookId und number sind erforderlich." }, { error: "Ungültige IDs oder fehlende Nummer." },
{ status: 400 } { status: 400 }
); );
} }
+19 -9
View File
@@ -122,16 +122,26 @@ export async function addSongToSongbook(title:string, songbookId:number, number:
export async function attachSongToSongbook(songId: number, songbookId: number, number: string) { export async function attachSongToSongbook(songId: number, songbookId: number, number: string) {
try { try {
const runTransaction = db.transaction(() => { const songExists = db.prepare("SELECT id FROM songs WHERE id = ?").get(songId);
db.prepare(` if (!songExists) {
INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number) console.error(`Song mit ID ${songId} existiert nicht in DB.`);
VALUES (?, ?, ?) return { success: false, error: "Song nicht gefunden." };
`).run(songbookId, songId, number); }
})
runTransaction(); const bookExists = db.prepare("SELECT id FROM songbooks WHERE id = ?").get(songbookId);
return { success: true } 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) { } catch (error) {
console.error("Error in addSongToSongbook:", error); console.error("Error in attachSongToSongbook:", error);
return { success: false }; return { success: false };
} }
} }