fix: create song
This commit is contained in:
1 parent
583ef90a7b
commit
fde1f27745
4 files changed
+112
-97
No files matched your search
@@ -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.`};
|
||||
|
||||
+43
-32
@@ -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<string | null>(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() {
|
||||
<Input.Text
|
||||
type="text"
|
||||
placeholder="z.B. Gesangbuch"
|
||||
value={newBookName}
|
||||
onChange={(e) => setNewBookName(e.target.value)}
|
||||
value={newBookTitle}
|
||||
onChange={(e) => setNewBookTitle(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
@@ -248,7 +259,7 @@ export default function AdminPage() {
|
||||
<option value="">-- Bitte wählen --</option>
|
||||
{songbooks.map((book) => (
|
||||
<option key={book.id} value={String(book.id)}>
|
||||
{book.name}{book.language ? ` (${book.language})` : ""}
|
||||
{book.title}{book.language ? ` (${book.language})` : ""}
|
||||
</option>
|
||||
))}
|
||||
</Input.Select>
|
||||
@@ -290,7 +301,7 @@ export default function AdminPage() {
|
||||
<option value="">-- Bitte wählen --</option>
|
||||
{songbooks.map((book) => (
|
||||
<option key={book.id} value={String(book.id)}>
|
||||
{book.name}{book.language ? ` (${book.language})` : ""}
|
||||
{book.title}{book.language ? ` (${book.language})` : ""}
|
||||
</option>
|
||||
))}
|
||||
</Input.Select>
|
||||
@@ -335,7 +346,7 @@ export default function AdminPage() {
|
||||
<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})
|
||||
{info.songbook_title} (Nr. {info.song_number})
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
@@ -356,7 +367,7 @@ export default function AdminPage() {
|
||||
<option value="">-- Bitte wählen --</option>
|
||||
{songbooks.map((book) => (
|
||||
<option key={book.id} value={String(book.id)}>
|
||||
{book.name}{book.language ? ` (${book.language})` : ""}
|
||||
{book.title}{book.language ? ` (${book.language})` : ""}
|
||||
</option>
|
||||
))}
|
||||
</Input.Select>
|
||||
@@ -390,7 +401,7 @@ export default function AdminPage() {
|
||||
{songInfos.length > 0 ? (
|
||||
songInfos.map((info) => (
|
||||
<option key={info.songbook_id} value={String(info.songbook_id)}>
|
||||
{info.songbook_name} (Nr. {info.song_number})
|
||||
{info.songbook_title} (Nr. {info.song_number})
|
||||
</option>
|
||||
))
|
||||
): ""}
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<ComponentPropsWithoutRef<typeof Card>, "title" | "children"> {
|
||||
onSelectSong?: (song: Song) => void;
|
||||
@@ -9,7 +9,7 @@ interface SongsProps extends Omit<ComponentPropsWithoutRef<typeof Card>, "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<Song[]>([]);
|
||||
const [allSongs, setAllSongs] = useState<Song[]>([]);
|
||||
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
|
||||
|
||||
|
||||
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
|
||||
>
|
||||
<div className="mb-4">
|
||||
<Input.Text
|
||||
type="text"
|
||||
placeholder="Nach Songtitel oder Nummer suchen..."
|
||||
value={song_search_name}
|
||||
onChange={(e) => search(e.target.value)}
|
||||
type="text"
|
||||
placeholder="Nach Songtitel oder Nummer suchen..."
|
||||
value={song_search_name}
|
||||
onChange={(e) => search(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* TABELLE */}
|
||||
<div className="flex-1 min-h-0 overflow-auto border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-black">
|
||||
<table className="w-full text-left border-collapse text-sm">
|
||||
<thead className="bg-neutral-100 dark:bg-neutral-900 sticky top-0 border-b border-neutral-200 dark:border-neutral-800 z-10">
|
||||
<tr>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Titel</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Liederbuch</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-center">Nummer</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-right">Aktion</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Titel</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Liederbuch</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-center">Nummer</th>
|
||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-right">Aktion</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-neutral-200 dark:divide-neutral-800">
|
||||
{searchResults.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={4} className="p-8 text-center text-neutral-400 dark:text-neutral-500">Keine Lieder gefunden</td>
|
||||
<td colSpan={4} className="p-8 text-center text-neutral-400 dark:text-neutral-500">Keine Lieder gefunden</td>
|
||||
</tr>
|
||||
) : (
|
||||
searchResults.map((song) => {
|
||||
const songBook = songbooks.find((book) => book.id === song.songbook_id);
|
||||
return (
|
||||
<tr key={`${song.songbook_id}-${song.song_id}`} className="hover:bg-neutral-100/60 dark:hover:bg-neutral-900/40 transition">
|
||||
<td className="p-3 font-medium text-neutral-900 dark:text-white max-w-[180px] sm:max-w-none truncate">
|
||||
{song.title}
|
||||
<span className="block text-xs text-neutral-500 dark:text-neutral-400 sm:hidden mt-0.5">
|
||||
{songBook ? songBook.name : "Unbekannt"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="p-3 text-neutral-700 dark:text-neutral-300 hidden sm:table-cell">
|
||||
{songBook ? songBook.name : "Unbekannt"}
|
||||
</td>
|
||||
<td className="p-3 text-center font-mono text-blue-600 dark:text-blue-400 font-bold">
|
||||
{song.song_number}
|
||||
</td>
|
||||
<td className="p-3 text-right">
|
||||
<Input.Button
|
||||
onClick={() => 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}
|
||||
</Input.Button>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
const songBook = songbooks.find((book) => book.id === song.songbook_id);
|
||||
|
||||
return (
|
||||
<tr key={`${song.songbook_id}-${song.id}`} className="hover:bg-neutral-100/60 dark:hover:bg-neutral-900/40 transition">
|
||||
<td className="p-3 font-medium text-neutral-900 dark:text-white max-w-[180px] sm:max-w-none truncate">
|
||||
{song.title}
|
||||
<span className="block text-xs text-neutral-500 dark:text-neutral-400 sm:hidden mt-0.5">
|
||||
{songBook ? songBook.title : "Unbekannt"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="p-3 text-neutral-700 dark:text-neutral-300 hidden sm:table-cell">
|
||||
{songBook ? songBook.title : "Unbekannt"}
|
||||
</td>
|
||||
<td className="p-3 text-center font-mono text-blue-600 dark:text-blue-400 font-bold">
|
||||
{song.song_number}
|
||||
</td>
|
||||
<td className="p-3 text-right">
|
||||
<Input.Button
|
||||
onClick={() => 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}
|
||||
</Input.Button>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export const Screens = {
|
||||
Songs: Songs,
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user