Feat/admin options #14
No files matched your search
+172
-5
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { uploadCSVAction } from "./actions";
|
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 Card from "@/components/Card"
|
||||||
import { Input } from "@/components/FormElements";
|
import { Input } from "@/components/FormElements";
|
||||||
@@ -13,6 +13,20 @@ interface Songbook {
|
|||||||
language: string;
|
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() {
|
export default function AdminPage() {
|
||||||
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
|
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
|
||||||
const [selectedSongbookId, setSelectedSongbookId] = useState("");
|
const [selectedSongbookId, setSelectedSongbookId] = useState("");
|
||||||
@@ -25,6 +39,9 @@ export default function AdminPage() {
|
|||||||
|
|
||||||
const [uploadStatus, setUploadStatus] = useState<string | null>(null);
|
const [uploadStatus, setUploadStatus] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const [selectedSong, setSelectedSong] = useState<Song | null>(null);
|
||||||
|
const [songInfos, setSongInfos] = useState<SongInfo[]>([])
|
||||||
|
|
||||||
async function handleCSVUpload(formData: FormData) {
|
async function handleCSVUpload(formData: FormData) {
|
||||||
setUploadStatus("Verarbeite...");
|
setUploadStatus("Verarbeite...");
|
||||||
const result = await uploadCSVAction(formData);
|
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(() => {
|
useEffect(() => {
|
||||||
async function loadSongbooks() {
|
async function loadSongbooks() {
|
||||||
try {
|
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 () => {
|
const addSongbook = async () => {
|
||||||
if (!newBookName) {
|
if (!newBookName) {
|
||||||
alert("Der Name des Liederbuches darf nicht leer sein!");
|
alert("Der Name des Liederbuches darf nicht leer sein!");
|
||||||
@@ -189,7 +278,7 @@ export default function AdminPage() {
|
|||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<Card title="CSV-Massenimport">
|
<Card title="CSV-Massenimport">
|
||||||
<form action={handleCSVUpload} className="h-full flex flex-col justify-between">
|
<form action={handleCSVUpload} className="flex flex-col justify-between">
|
||||||
<input type="hidden" name="songbookId" value={selectedSongbookId} />
|
<input type="hidden" name="songbookId" value={selectedSongbookId} />
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<div>
|
<div>
|
||||||
@@ -233,11 +322,89 @@ export default function AdminPage() {
|
|||||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||||
<Screens.Songs
|
<Screens.Songs
|
||||||
actionName="Auswählen"
|
actionName="Auswählen"
|
||||||
onSelectSong={(song) => console.log(song)}
|
onSelectSong={(song) => setSelectedSong(song)}
|
||||||
className=""
|
className=""
|
||||||
/>
|
/>
|
||||||
<Card title="Lied zu Liederbuch hinzufügen">
|
<Card title="Ausgewähltes Lied">
|
||||||
<Input.Text disabled></Input.Text>
|
<Input.Text value={selectedSong ? selectedSong.title : ""} disabled></Input.Text>
|
||||||
|
<Card title="Informationen" className="mt-4">
|
||||||
|
<div className="text-sm space-y-1">
|
||||||
|
<p><span className="font-semibold">Lied-ID:</span> {selectedSong?.song_id ?? "-"}</p>
|
||||||
|
<p><span className="font-semibold">In Liederbüchern:</span></p>
|
||||||
|
{songInfos.length > 0 ? (
|
||||||
|
<ul className="list-disc list-inside pl-2 text-xs text-neutral-400">
|
||||||
|
{songInfos.map((info) => (
|
||||||
|
<li key={info.songbook_id}>
|
||||||
|
{info.songbook_name} (Nr. {info.song_number})
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
) : (
|
||||||
|
<p className="text-xs text-neutral-500">Keine Daten geladen</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
<Card title="Aktionen">
|
||||||
|
<Card title="Zu Liederbuch hinzufügen">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<Input.Label>Ziel-Liederbuch</Input.Label>
|
||||||
|
<Input.Select
|
||||||
|
value={selectedSongbookId}
|
||||||
|
onChange={(e) => setSelectedSongbookId(e.target.value)}
|
||||||
|
>
|
||||||
|
<option value="">-- Bitte wählen --</option>
|
||||||
|
{songbooks.map((book) => (
|
||||||
|
<option key={book.id} value={String(book.id)}>
|
||||||
|
{book.name}{book.language ? ` (${book.language})` : ""}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</Input.Select>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Input.Label>Nummer</Input.Label>
|
||||||
|
<Input.Text
|
||||||
|
type="text"
|
||||||
|
placeholder="12a"
|
||||||
|
value={number}
|
||||||
|
onChange={(e) => setNumber(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<Input.Button
|
||||||
|
onClick={attachSong}
|
||||||
|
className="w-full mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg px-4 py-2.5 transition text-sm shadow-sm"
|
||||||
|
>
|
||||||
|
Lied hinzufügen
|
||||||
|
</Input.Button>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
<Card title="Aus Liederbuch löschen">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<Input.Label>Ziel-Liederbuch</Input.Label>
|
||||||
|
<Input.Select
|
||||||
|
value={selectedSongbookId}
|
||||||
|
onChange={(e) => setSelectedSongbookId(e.target.value)}
|
||||||
|
>
|
||||||
|
<option value="">-- Bitte wählen --</option>
|
||||||
|
{songInfos.length > 0 ? (
|
||||||
|
songInfos.map((info) => (
|
||||||
|
<option key={info.songbook_id} value={String(info.songbook_id)}>
|
||||||
|
{info.songbook_name} (Nr. {info.song_number})
|
||||||
|
</option>
|
||||||
|
))
|
||||||
|
): ""}
|
||||||
|
</Input.Select>
|
||||||
|
</div>
|
||||||
|
<Input.Button
|
||||||
|
onClick={removeSong}
|
||||||
|
className="w-full mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg px-4 py-2.5 transition text-sm shadow-sm"
|
||||||
|
>
|
||||||
|
Lied aus Liederbuch löschen
|
||||||
|
</Input.Button>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
</Card>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -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 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+67
-2
@@ -1,5 +1,4 @@
|
|||||||
import Database from "better-sqlite3";
|
import Database from "better-sqlite3";
|
||||||
import { Noto_Sans_Tamil_Supplement } from "next/font/google";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
const dbPath = path.join(process.cwd(), "songs.db");
|
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) {
|
export async function addSongToSongbook(title:string, songbookId:number, number:string) {
|
||||||
try {
|
try {
|
||||||
const runTransaction = db.transaction(() => {
|
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() {
|
export async function getSongbooks() {
|
||||||
try {
|
try {
|
||||||
const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all();
|
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 {
|
try {
|
||||||
const info = db.prepare(`
|
const info = db.prepare(`
|
||||||
INSERT INTO songbooks (name, language)
|
INSERT INTO songbooks (name, language)
|
||||||
|
|||||||
Reference in new issue
Block a user