diff --git a/app/admin/page.tsx b/app/admin/page.tsx index 95e699e..72af9c2 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -1,7 +1,7 @@ "use client"; import { uploadCSVAction } from "./actions"; -import { useEffect, useState } from "react"; +import { JSXElementConstructor, Key, ReactElement, ReactNode, ReactPortal, useEffect, useState } from "react"; import Card from "@/components/Card" import { Input } from "@/components/FormElements"; @@ -13,6 +13,20 @@ interface Songbook { language: string; } +interface Song { + song_id: number; + songbook_id: number; + song_number: string; + title: string; +} + +interface SongInfo { + songbook_id: number; + songbook_name: string; + song_number: string; + title: string; +} + export default function AdminPage() { const [songbooks, setSongbooks] = useState([]); const [selectedSongbookId, setSelectedSongbookId] = useState(""); @@ -25,6 +39,9 @@ export default function AdminPage() { const [uploadStatus, setUploadStatus] = useState(null); + const [selectedSong, setSelectedSong] = useState(null); + const [songInfos, setSongInfos] = useState([]) + async function handleCSVUpload(formData: FormData) { setUploadStatus("Verarbeite..."); const result = await uploadCSVAction(formData); @@ -36,6 +53,27 @@ export default function AdminPage() { } } + useEffect(() => { + if (!selectedSong) { + setSongInfos([]); + return; + } + + async function fetchSongInfos() { + try { + const res = await fetch(`/api/songs/${selectedSong?.song_id}`); + if (res.ok) { + const data = await res.json(); + setSongInfos(data); + } + } catch (error) { + console.error("Fehler beim Laden der Songinfos:", error); + } + } + + fetchSongInfos(); + }, [selectedSong]); + useEffect(() => { async function loadSongbooks() { try { @@ -79,6 +117,57 @@ 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("Lied erfolgreich zum Liederbuch hinzugefügt!"); + } else { + alert("Fehler beim Senden."); + } + } catch (error) { + console.error("Failed sending current songID:", error); + } + } + + const removeSong = async () => { + if (!selectedSongbookId || !selectedSong) { + alert("Alle Felder ausfüllen!"); + return; + } + + try { + const res = await fetch("/api/songs/remove", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + songbookId: selectedSongbookId, + songId: selectedSong.song_id + }) + }); + if (res.ok) { + alert("Lied erfolgreich aus dem Liederbuch gelöscht!"); + } else { + alert("Fehler beim Senden."); + } + } catch (error) { + console.error("Failed sending current songID:", error); + } + } + const addSongbook = async () => { if (!newBookName) { alert("Der Name des Liederbuches darf nicht leer sein!"); @@ -189,7 +278,7 @@ export default function AdminPage() { -
+
@@ -233,11 +322,89 @@ export default function AdminPage() {
console.log(song)} + onSelectSong={(song) => setSelectedSong(song)} className="" /> - - + + + +
+

Lied-ID: {selectedSong?.song_id ?? "-"}

+

In Liederbüchern:

+ {songInfos.length > 0 ? ( +
    + {songInfos.map((info) => ( +
  • + {info.songbook_name} (Nr. {info.song_number}) +
  • + ))} +
+ ) : ( +

Keine Daten geladen

+ )} +
+
+ + +
+
+ Ziel-Liederbuch + setSelectedSongbookId(e.target.value)} + > + + {songbooks.map((book) => ( + + ))} + +
+
+ Nummer + setNumber(e.target.value)} + /> +
+ + Lied hinzufügen + +
+
+ +
+
+ Ziel-Liederbuch + setSelectedSongbookId(e.target.value)} + > + + {songInfos.length > 0 ? ( + songInfos.map((info) => ( + + )) + ): ""} + +
+ + Lied aus Liederbuch löschen + +
+
+
diff --git a/app/api/songs/[id]/route.ts b/app/api/songs/[id]/route.ts new file mode 100644 index 0000000..7b43176 --- /dev/null +++ b/app/api/songs/[id]/route.ts @@ -0,0 +1,20 @@ +import { NextResponse } from "next/server"; +import { getSongInfos } from "@/lib/db/managing"; + +export async function GET( + request: Request, + { params }: { params: Promise<{ id: string }> } +) { + try { + const { id } = await params; + const songInfos = await getSongInfos(id); + + return NextResponse.json(songInfos || []); + } catch (error) { + console.error("API Error:", error); + return NextResponse.json( + { error: "Fehler beim Laden der Songinfos" }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/api/songs/attach/route.ts b/app/api/songs/attach/route.ts new file mode 100644 index 0000000..cceaddb --- /dev/null +++ b/app/api/songs/attach/route.ts @@ -0,0 +1,40 @@ +import { NextResponse } from "next/server"; +import { attachSongToSongbook } from "@/lib/db/managing"; + +export async function POST(request: Request) { + try { + const body = await request.json(); + const { songId, songbookId, number } = body; + + if (isNaN(songId) || isNaN(songbookId) || !number) { + return NextResponse.json( + { error: "Ungültige IDs oder fehlende Nummer." }, + { status: 400 } + ); + } + + const result = await attachSongToSongbook( + Number(songId), + Number(songbookId), + String(number) + ); + + if (result.success) { + return NextResponse.json( + { message: "Lied erfolgreich verknüpft!" }, + { status: 200 } + ); + } else { + return NextResponse.json( + { error: "Fehler beim Verknüpfen in der Datenbank." }, + { status: 500 } + ); + } + } catch (error) { + console.error("API Error in /api/songs/attach:", error); + return NextResponse.json( + { error: "Ungültiges JSON oder Serverfehler." }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/api/songs/remove/route.ts b/app/api/songs/remove/route.ts new file mode 100644 index 0000000..42745b6 --- /dev/null +++ b/app/api/songs/remove/route.ts @@ -0,0 +1,39 @@ +import { NextResponse } from "next/server"; +import { removeSongfromSongbook } from "@/lib/db/managing"; + +export async function POST(request: Request) { + try { + const body = await request.json(); + const { songId, songbookId } = body; + + if (isNaN(songId) || isNaN(songbookId)) { + return NextResponse.json( + { error: "Ungültige IDs." }, + { status: 400 } + ); + } + + const result = await removeSongfromSongbook( + Number(songId), + Number(songbookId) + ); + + if (result.success) { + return NextResponse.json( + { message: "Lied erfolgreich aus dem Liederbuch gelöscht!" }, + { status: 200 } + ); + } else { + return NextResponse.json( + { error: "Fehler beim Löschen aus dem Liederbuch." }, + { status: 500 } + ); + } + } catch (error) { + console.error("API Error in /api/songs/remove:", error); + return NextResponse.json( + { error: "Ungültiges JSON oder Serverfehler." }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/lib/db/managing.ts b/lib/db/managing.ts index 11b2071..f3c69b1 100644 --- a/lib/db/managing.ts +++ b/lib/db/managing.ts @@ -1,5 +1,4 @@ import Database from "better-sqlite3"; -import { Noto_Sans_Tamil_Supplement } from "next/font/google"; import path from "path"; const dbPath = path.join(process.cwd(), "songs.db"); @@ -69,6 +68,32 @@ export async function getSong(songbookId: number, songId: string) { } } +export async function getSongInfos(songId: string) { + try { + const songRecords = db.prepare(` + SELECT + ss.songbook_id, + sb.name AS songbook_name, + ss.song_number, + s.title + FROM songbook_songs ss + JOIN songbooks sb ON ss.songbook_id = sb.id + JOIN songs s ON ss.song_id = s.id + WHERE s.id = ? + `).all(songId) as { + songbook_id: number; + songbook_name: string; + song_number: string; + title: string; + }[] | undefined + + return songRecords || null; + } catch (error) { + console.error("Fehler in getSong:", error); + return null; + } +} + export async function addSongToSongbook(title:string, songbookId:number, number:string) { try { const runTransaction = db.transaction(() => { @@ -95,6 +120,46 @@ export async function addSongToSongbook(title:string, songbookId:number, number: } } +export async function attachSongToSongbook(songId: number, songbookId: number, number: string) { + try { + 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 attachSongToSongbook:", error); + return { success: false }; + } +} + +export async function removeSongfromSongbook(songId: number, songbookId: number) { + try { + const info = db.prepare(` + DELETE FROM songbook_songs + WHERE songbook_id = ? AND song_id = ? + `).run(songbookId, songId); + + return { success: info.changes > 0 }; + } catch (error) { + console.error("Error in removeSongfromSongbook:", error); + return { success: false }; + } +} + export async function getSongbooks() { try { const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all(); @@ -104,7 +169,7 @@ export async function getSongbooks() { } } -export async function addSongbook(name:String, language:String) { +export async function addSongbook(name: string, language: string) { try { const info = db.prepare(` INSERT INTO songbooks (name, language)