47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
"use server";
|
|
|
|
//import { error } from "console";
|
|
import Papa from "papaparse";
|
|
import { createSong } from "@/lib/db/song";
|
|
//import { createSongBook } from "@/lib/db/songbook.ts";
|
|
import { addSongToBook } from "@/lib/db/songbooksongs.ts";
|
|
|
|
|
|
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"};
|
|
}
|
|
}
|