From fde1f2774589887ebbdff79094c2c23998487922 Mon Sep 17 00:00:00 2001 From: Robin Schanbacher Date: Thu, 27 Aug 2026 17:29:03 +0200 Subject: [PATCH] fix: create song --- app/admin/actions.ts | 5 +- app/admin/page.tsx | 75 ++++++++++--------- app/api/songs/route.ts | 5 +- components/DatabaseOverview.tsx | 124 ++++++++++++++++---------------- 4 files changed, 112 insertions(+), 97 deletions(-) diff --git a/app/admin/actions.ts b/app/admin/actions.ts index 27fc923..1ed7e30 100644 --- a/app/admin/actions.ts +++ b/app/admin/actions.ts @@ -2,7 +2,6 @@ import { error } from "console"; import Papa from "papaparse"; -import { addSongToSongbook } from "@/lib/db/managing"; interface CSVRow { nummer: string; @@ -31,8 +30,8 @@ export async function uploadCSVAction(formData:FormData) { console.log("Data:", rows); rows.forEach(row => { - //console.log(`Nummer: ${row.number}, Titel: ${row.titel}`); - addSongToSongbook(String(row.titel), Number(songbookId), String(row.nummer)) + console.log(`Nummer: ${row.nummer}, Titel: ${row.titel}`); + //addSongToSongbook(String(row.titel), Number(songbookId), String(row.nummer)) }); return { success: true, message: `${rows.length} Zeilen erfolgreich importiert.`}; diff --git a/app/admin/page.tsx b/app/admin/page.tsx index 72af9c2..48608f0 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -9,7 +9,7 @@ import { Screens } from "@/components/DatabaseOverview"; interface Songbook { id: number; - name: string; + title: string; language: string; } @@ -22,7 +22,7 @@ interface Song { interface SongInfo { songbook_id: number; - songbook_name: string; + songbook_title: string; song_number: string; title: string; } @@ -34,7 +34,7 @@ export default function AdminPage() { const [number, setNumber] = useState(""); const [title, setTitle] = useState(""); - const [newBookName, setNewBookName] = useState(""); + const [newBookTitle, setNewBookTitle] = useState(""); const [newBookLanguage, setNewBookLanguage] = useState(""); const [uploadStatus, setUploadStatus] = useState(null); @@ -96,21 +96,34 @@ export default function AdminPage() { } try { - const res = await fetch("/api/songs", { + // Create song + const res1 = await fetch("/api/songs", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - songbookId: Number(selectedSongbookId), - number: number, + body: JSON.stringify({ title: title }) - }); - if (res.ok) { - alert("Lied erfolgreich hinzugefügt!"); - setNumber(""); - setTitle(""); + }) + if (res1.ok) { + console.log("Lied erfolgreich erstellt!"); } else { - alert("Fehler beim Senden."); + console.log("Lied erstellen fehlgeschlagen!"); + } + + // Attach song to songbook + const data = await res1.json() + const songId = data.song.id; + const res2 = await fetch(`/api/songbooks/${Number(selectedSongbookId)}/songs/${songId}`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + number: number + }) + }) + if (res2.ok) { + console.log("Lied erfolgreich dem Liederbuch zugewiesen!"); + } else { + console.log("Fehler"); } } catch (error) { console.error("Failed sending current songID:", error); @@ -124,19 +137,17 @@ export default function AdminPage() { } try { - const res = await fetch("/api/songs/attach", { + const res2 = await fetch(`/api/songbooks/${Number(selectedSongbookId)}/songs/${selectedSong.song_id}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ - songbookId: selectedSongbookId, - songId: selectedSong.song_id, - number: number, + number: number }) - }); - if (res.ok) { - alert("Lied erfolgreich zum Liederbuch hinzugefügt!"); + }) + if (res2.ok) { + console.log("Lied erfolgreich dem Liederbuch zugewiesen!"); } else { - alert("Fehler beim Senden."); + console.log("Fehler"); } } catch (error) { console.error("Failed sending current songID:", error); @@ -169,8 +180,8 @@ export default function AdminPage() { } const addSongbook = async () => { - if (!newBookName) { - alert("Der Name des Liederbuches darf nicht leer sein!"); + if (!newBookTitle) { + alert("Der Title des Liederbuches darf nicht leer sein!"); return; } @@ -178,14 +189,14 @@ export default function AdminPage() { const res = await fetch("/api/songbooks", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ name: newBookName, language: newBookLanguage }) + body: JSON.stringify({ title: newBookTitle, language: newBookLanguage }) }); if (res.ok) { const createdBook = await res.json(); setSongbooks([...songbooks, createdBook]); alert("Liederbuch erfolgreich erstellt!"); - setNewBookName(""); + setNewBookTitle(""); setNewBookLanguage(""); } else { alert("Failed adding songbook!"); @@ -215,8 +226,8 @@ export default function AdminPage() { setNewBookName(e.target.value)} + value={newBookTitle} + onChange={(e) => setNewBookTitle(e.target.value)} />
@@ -248,7 +259,7 @@ export default function AdminPage() { {songbooks.map((book) => ( ))} @@ -290,7 +301,7 @@ export default function AdminPage() { {songbooks.map((book) => ( ))} @@ -335,7 +346,7 @@ export default function AdminPage() {
    {songInfos.map((info) => (
  • - {info.songbook_name} (Nr. {info.song_number}) + {info.songbook_title} (Nr. {info.song_number})
  • ))}
@@ -356,7 +367,7 @@ export default function AdminPage() { {songbooks.map((book) => ( ))} @@ -390,7 +401,7 @@ export default function AdminPage() { {songInfos.length > 0 ? ( songInfos.map((info) => ( )) ): ""} diff --git a/app/api/songs/route.ts b/app/api/songs/route.ts index e2749ce..65f2634 100644 --- a/app/api/songs/route.ts +++ b/app/api/songs/route.ts @@ -110,7 +110,10 @@ export async function POST(request: Request) { if (!res.success) { return NextResponse.json( - { message: res.error }, + { + message: res.error, + song: res.song + }, { status: res.status } ); } diff --git a/components/DatabaseOverview.tsx b/components/DatabaseOverview.tsx index 1e327f7..7ff41d9 100644 --- a/components/DatabaseOverview.tsx +++ b/components/DatabaseOverview.tsx @@ -1,7 +1,7 @@ import { useState, useEffect, ComponentPropsWithoutRef } from "react"; -import Card from "./Card" -import { Input } from "./FormElements" +import Card from "./Card"; +import { Input } from "./FormElements"; interface SongsProps extends Omit, "title" | "children"> { onSelectSong?: (song: Song) => void; @@ -9,7 +9,7 @@ interface SongsProps extends Omit, "title" } interface Song { - song_id: number; + id: number; songbook_id: number; song_number: string; title: string; @@ -17,43 +17,42 @@ interface Song { interface Songbook { id: number; - name: string; + title: string; language: string; } -function Songs({ actionName, onSelectSong, className = "", ...props }: SongsProps) { +function Songs({ actionName = "Auswählen", onSelectSong, className = "", ...props }: SongsProps) { const [song_search_name, setSongSearchName] = useState(""); const [searchResults, setSearchResults] = useState([]); const [allSongs, setAllSongs] = useState([]); const [songbooks, setSongbooks] = useState([]); - const search = (query: string) => { setSongSearchName(query); if (query.trim() !== "") { - setSearchResults( - allSongs.filter((song) => - song.title.toLowerCase().includes(query.toLowerCase()) || - song.song_number.includes(query) - ) - ); + setSearchResults( + allSongs.filter((song) => + song.title.toLowerCase().includes(query.toLowerCase()) || + song.song_number.includes(query) + ) + ); } else { - setSearchResults(allSongs); + setSearchResults(allSongs); } }; useEffect(() => { const getSongslist = async () => { try { - const res = await fetch("/api/songs"); - if (res.ok) { - const data = await res.json(); - setAllSongs(data); - setSearchResults(data); - } + const res = await fetch("/api/songs"); + if (res.ok) { + const data = await res.json(); + setAllSongs(data); + setSearchResults(data); + } } catch (error) { - setAllSongs([]); - console.error("Failed fetching songslist!"); + setAllSongs([]); + console.error("Failed fetching songslist!", error); } }; @@ -61,13 +60,14 @@ function Songs({ actionName, onSelectSong, className = "", ...props }: SongsProp try { const res = await fetch("/api/songbooks"); if (res.ok) { - const data = await res.json(); - setSongbooks(data); + const data = await res.json(); + setSongbooks(data); } } catch (error) { console.error("Loading songbooks failed: ", error); } } + loadSongbooks(); getSongslist(); }, []); @@ -80,64 +80,66 @@ function Songs({ actionName, onSelectSong, className = "", ...props }: SongsProp >
search(e.target.value)} + type="text" + placeholder="Nach Songtitel oder Nummer suchen..." + value={song_search_name} + onChange={(e) => search(e.target.value)} />
+ {/* TABELLE */}
- - - - - - + + + + + + {searchResults.length === 0 ? ( - + ) : ( searchResults.map((song) => { - const songBook = songbooks.find((book) => book.id === song.songbook_id); - return ( - - - - - - - ); + const songBook = songbooks.find((book) => book.id === song.songbook_id); + + return ( + + + + + + + ); }) )}
TitelLiederbuchNummerAktion
TitelLiederbuchNummerAktion
Keine Lieder gefundenKeine Lieder gefunden
- {song.title} - - {songBook ? songBook.name : "Unbekannt"} - - - {songBook ? songBook.name : "Unbekannt"} - - {song.song_number} - - onSelectSong?.(song)} - className="bg-white dark:bg-black hover:bg-blue-600 dark:hover:bg-blue-600 border border-neutral-300 dark:border-neutral-700 hover:border-blue-600 dark:hover:border-blue-500 hover:text-white font-medium rounded-md px-3 py-1.5 transition text-xs" - > - {actionName} - -
+ {song.title} + + {songBook ? songBook.title : "Unbekannt"} + + + {songBook ? songBook.title : "Unbekannt"} + + {song.song_number} + + onSelectSong?.(song)} + className="bg-white dark:bg-black hover:bg-blue-600 dark:hover:bg-blue-600 border border-neutral-300 dark:border-neutral-700 hover:border-blue-600 dark:hover:border-blue-500 hover:text-white font-medium rounded-md px-3 py-1.5 transition text-xs" + > + {actionName} + +
- ) + ); } export const Screens = { Songs: Songs, -} \ No newline at end of file +}; \ No newline at end of file