first commit
This commit is contained in:
commit
7bf0cb4f05
27 files changed
+5490
No files matched your search
@@ -0,0 +1,43 @@
|
||||
"use server";
|
||||
|
||||
import { error } from "console";
|
||||
import Papa from "papaparse";
|
||||
import { addSongToSongbook } from "@/lib/db/managing";
|
||||
|
||||
interface CSVRow {
|
||||
nummer: string;
|
||||
titel: string;
|
||||
}
|
||||
|
||||
export async function uploadCSVAction(formData:FormData) {
|
||||
const songbookId = formData.get("songbookId") as String;
|
||||
console.log(songbookId);
|
||||
const file = formData.get("csvFile") as File;
|
||||
|
||||
if (!file) {
|
||||
return { success: false, error: "No file attached!"}
|
||||
}
|
||||
|
||||
try {
|
||||
const fileText = await file.text();
|
||||
|
||||
const parsedData = Papa.parse(fileText, {
|
||||
header: true,
|
||||
delimiter: ";",
|
||||
skipEmptyLines: true,
|
||||
});
|
||||
|
||||
const rows = parsedData.data as CSVRow[];
|
||||
|
||||
console.log("Data:", rows);
|
||||
rows.forEach(row => {
|
||||
//console.log(`Nummer: ${row.number}, Titel: ${row.titel}`);
|
||||
addSongToSongbook(String(row.titel), Number(songbookId), String(row.nummer))
|
||||
});
|
||||
|
||||
return { success: true, message: `${rows.length} Zeilen erfolgreich importiert.`};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return { success: false, error: "Error"};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user