pushed changes
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
|
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
|
||||||
import * as XLSX from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
|
import * as XLSX from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
|
||||||
import { useSignal } from "@preact/signals";
|
import { useSignal } from "@preact/signals";
|
||||||
import handleUpload from "../api/insert_students.ts";
|
|
||||||
|
|
||||||
export default function UploadStudents() {
|
export default function UploadStudents() {
|
||||||
const statusMessage = useSignal<string>("");
|
const statusMessage = useSignal<string>("");
|
||||||
@@ -18,8 +17,55 @@ export default function UploadStudents() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const confirmUpload = () => {
|
const confirmUpload = async () => {
|
||||||
statusMessage.value = handleUpload(fileData.value);
|
if (!fileData.value) {
|
||||||
|
statusMessage.value = "Please select a file before confirming upload.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const reader = new FileReader();
|
||||||
|
|
||||||
|
reader.onload = async (e) => {
|
||||||
|
try {
|
||||||
|
const arrayBuffer = e.target?.result as ArrayBuffer;
|
||||||
|
const workbook = XLSX.read(arrayBuffer, { type: "array" });
|
||||||
|
|
||||||
|
for (const sheetName of workbook.SheetNames) {
|
||||||
|
const sheet = workbook.Sheets[sheetName];
|
||||||
|
const data = XLSX.utils.sheet_to_json(sheet, {
|
||||||
|
header: ["Nom", "Prénom", "Mail"],
|
||||||
|
range: 1, // Ignorer les en-têtes
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await fetch("/api/insert_students", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ promoName: sheetName, data }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to insert data for promotion ${sheetName}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
statusMessage.value = "Data uploaded and inserted successfully!";
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing the file:", error);
|
||||||
|
statusMessage.value = "Error processing the file. Please check its format.";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.onerror = (e) => {
|
||||||
|
console.error("FileReader error:", e);
|
||||||
|
statusMessage.value = "Error reading the file.";
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.readAsArrayBuffer(fileData.value);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error uploading file:", error);
|
||||||
|
statusMessage.value = "An unexpected error occurred during upload.";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,69 +1,80 @@
|
|||||||
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import * as XLSX from "https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs";
|
|
||||||
import { Database } from "@db/sqlite";
|
import { Database } from "@db/sqlite";
|
||||||
|
|
||||||
export default function handleUpload(file: File | null): string {
|
export const handler: Handlers = {
|
||||||
if (!file) {
|
async GET(_request, context) {
|
||||||
return "Please select a file before confirming upload.";
|
try {
|
||||||
}
|
// Ouvre ou crée la base de données SQLite
|
||||||
|
const db = new Database("databases/data/mobility.db");
|
||||||
|
|
||||||
try {
|
// Crée la table si elle n'existe pas
|
||||||
const reader = new FileReader();
|
db.execute(`
|
||||||
let statusMessage = "";
|
CREATE TABLE IF NOT EXISTS students (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
firstName TEXT NOT NULL,
|
||||||
|
lastName TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
promotion TEXT NOT NULL
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
reader.onload = async (e) => {
|
// Récupère toutes les données
|
||||||
try {
|
const students = [];
|
||||||
const arrayBuffer = e.target?.result as ArrayBuffer;
|
for (const [id, firstName, lastName, email, promotion] of db.query(
|
||||||
const workbook = XLSX.read(arrayBuffer, { type: "array" });
|
"SELECT id, firstName, lastName, email, promotion FROM students"
|
||||||
|
)) {
|
||||||
const db = new Database("databases/data/mobility.db");
|
students.push({ id, firstName, lastName, email, promotion });
|
||||||
|
|
||||||
db.execute(`
|
|
||||||
CREATE TABLE IF NOT EXISTS students (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
firstName TEXT NOT NULL,
|
|
||||||
lastName TEXT NOT NULL,
|
|
||||||
email TEXT NOT NULL,
|
|
||||||
promotion TEXT NOT NULL
|
|
||||||
);
|
|
||||||
`);
|
|
||||||
|
|
||||||
for (const sheetName of workbook.SheetNames) {
|
|
||||||
const sheet = workbook.Sheets[sheetName];
|
|
||||||
const data = XLSX.utils.sheet_to_json(sheet, {
|
|
||||||
header: ["firstName", "lastName", "email"],
|
|
||||||
range: 1, // Ignorer les en-têtes
|
|
||||||
});
|
|
||||||
|
|
||||||
const insertQuery =
|
|
||||||
"INSERT INTO students (firstName, lastName, email, promotion) VALUES (?, ?, ?, ?)";
|
|
||||||
for (const student of data) {
|
|
||||||
db.query(insertQuery, [
|
|
||||||
student.firstName,
|
|
||||||
student.lastName,
|
|
||||||
student.email,
|
|
||||||
sheetName,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
db.close();
|
|
||||||
statusMessage = "Data uploaded and inserted successfully!";
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error reading or inserting file:", error);
|
|
||||||
statusMessage = "Error processing the file. Please check its format.";
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
reader.onerror = (e) => {
|
db.close();
|
||||||
console.error("FileReader error:", e);
|
|
||||||
statusMessage = "Error reading the file.";
|
|
||||||
};
|
|
||||||
|
|
||||||
reader.readAsArrayBuffer(file);
|
return new Response(JSON.stringify(students), {
|
||||||
return statusMessage;
|
status: 200,
|
||||||
} catch (error) {
|
headers: { "Content-Type": "application/json" },
|
||||||
console.error("Error uploading file:", error);
|
});
|
||||||
return "An unexpected error occurred during upload.";
|
} catch (error) {
|
||||||
}
|
console.error("Error fetching students:", error);
|
||||||
}
|
return new Response("Failed to fetch students", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async POST(request) {
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { data, promoName } = body;
|
||||||
|
|
||||||
|
// Ouvre ou crée la base de données SQLite
|
||||||
|
const db = new Database("databases/data/mobility.db");
|
||||||
|
|
||||||
|
// Crée la table si elle n'existe pas
|
||||||
|
db.execute(`
|
||||||
|
CREATE TABLE IF NOT EXISTS students (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
firstName TEXT NOT NULL,
|
||||||
|
lastName TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
promotion TEXT NOT NULL
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Prépare et insère les données
|
||||||
|
const insertQuery =
|
||||||
|
"INSERT INTO students (firstName, lastName, email, promotion) VALUES (?, ?, ?, ?)";
|
||||||
|
for (const student of data) {
|
||||||
|
db.query(insertQuery, [
|
||||||
|
student.firstName,
|
||||||
|
student.lastName,
|
||||||
|
student.email,
|
||||||
|
promoName,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
return new Response("Students inserted successfully", { status: 201 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error inserting students:", error);
|
||||||
|
return new Response("Failed to insert students", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { RouteConfig } from "$fresh/server.ts";
|
import { RouteConfig } from "$fresh/server.ts";
|
||||||
import UploadStudents from "../(_components)/UploadStudents.tsx";
|
import UploadStudents from "../(_components)/UploadStudents.tsx";
|
||||||
//import ConsultStudents from "../(_islands)/ConsultStudents.tsx";
|
//import ConsultStudents from "../(_components)/ConsultStudents.tsx";
|
||||||
//import EditStudents from "../(_islands)/EditStudents.tsx";
|
//import EditStudents from "../(_components)/EditStudents.tsx";
|
||||||
|
|
||||||
export const config: RouteConfig = {
|
export const config: RouteConfig = {
|
||||||
skipAppWrapper: false,
|
skipAppWrapper: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user