Trying to use the DB (not working)
This commit is contained in:
@@ -1,12 +0,0 @@
|
|||||||
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
|
|
||||||
);
|
|
||||||
CREATE TABLE IF NOT EXISTS promotions (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
year INTEGER NOT NULL
|
|
||||||
);
|
|
||||||
|
|||||||
+22
-13
@@ -1,21 +1,30 @@
|
|||||||
import { Database } from "@db/sqlite";
|
import { DB } from "https://deno.land/x/sqlite/mod.ts";
|
||||||
|
|
||||||
export default async function insertIntoMobility(data: Array<{ firstName: string; lastName: string; email: string }>, promoName: string) {
|
export default function insertIntoMobility(
|
||||||
|
data: Array<{ firstName: string; lastName: string; email: string }>,
|
||||||
|
promoName: string
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const databasePath = "databases/data/mobility.db";
|
const db = new DB("databases/data/mobility.db");
|
||||||
const db = new Database(databasePath);
|
|
||||||
|
|
||||||
db.transaction(() => {
|
db.execute(`
|
||||||
for (const student of data) {
|
CREATE TABLE IF NOT EXISTS students (
|
||||||
db.query(
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
"INSERT INTO students (firstName, lastName, email, promotion) VALUES (?, ?, ?, ?)",
|
firstName TEXT NOT NULL,
|
||||||
[student.firstName, student.lastName, student.email, promoName]
|
lastName TEXT NOT NULL,
|
||||||
);
|
email TEXT NOT NULL,
|
||||||
}
|
promotion TEXT NOT NULL
|
||||||
})();
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Data for promotion ${promoName} inserted successfully.`);
|
||||||
|
|
||||||
db.close();
|
db.close();
|
||||||
console.log(`Data for promotion ${promoName} inserted successfully.`);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error inserting data into mobility database:", error);
|
console.error("Error inserting data into mobility database:", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,82 +0,0 @@
|
|||||||
import { useSignal } from "@preact/signals";
|
|
||||||
import Papa from "https://cdn.skypack.dev/papaparse";
|
|
||||||
|
|
||||||
type Student = {
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type Promotion = {
|
|
||||||
name: string;
|
|
||||||
students: Student[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function SaveStudents() {
|
|
||||||
const promotions = useSignal<Promotion[]>([]);
|
|
||||||
const statusMessage = useSignal<string>("");
|
|
||||||
|
|
||||||
const loadCSV = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch("/api/students"); // Assurez-vous que l'API est appelée correctement
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error("Failed to load CSV file");
|
|
||||||
}
|
|
||||||
const csvText = await response.text(); // Lire le contenu en texte
|
|
||||||
|
|
||||||
const parsedData = Papa.parse(csvText, {
|
|
||||||
header: true,
|
|
||||||
skipEmptyLines: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const groupedPromotions: Record<string, Student[]> = {};
|
|
||||||
parsedData.data.forEach((row: any) => {
|
|
||||||
const { promotion, firstName, lastName, email } = row;
|
|
||||||
if (!groupedPromotions[promotion]) {
|
|
||||||
groupedPromotions[promotion] = [];
|
|
||||||
}
|
|
||||||
groupedPromotions[promotion].push({ firstName, lastName, email });
|
|
||||||
});
|
|
||||||
|
|
||||||
const loadedPromotions = Object.entries(groupedPromotions).map(
|
|
||||||
([name, students]) => ({
|
|
||||||
name,
|
|
||||||
students,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
promotions.value = loadedPromotions;
|
|
||||||
statusMessage.value = "Data loaded successfully!";
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error loading CSV file:", error);
|
|
||||||
statusMessage.value = "Failed to load data. Please try again.";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Charger les données CSV dès le chargement du composant
|
|
||||||
loadCSV();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2>Loaded Promotions</h2>
|
|
||||||
<button onClick={loadCSV}>Actualiser</button>
|
|
||||||
<p>{statusMessage.value}</p>
|
|
||||||
<ul>
|
|
||||||
{promotions.value.map((promotion) => (
|
|
||||||
<li key={promotion.name}>
|
|
||||||
<strong>{promotion.name}</strong>
|
|
||||||
<ul>
|
|
||||||
{promotion.students.map((student, index) => (
|
|
||||||
<li key={index}>
|
|
||||||
{student.firstName} {student.lastName} - {student.email}
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -18,7 +18,7 @@ export default function UploadStudents() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUpload = async () => {
|
const handleUpload = () => {
|
||||||
if (!fileData.value) {
|
if (!fileData.value) {
|
||||||
statusMessage.value = "Please select a file before confirming upload.";
|
statusMessage.value = "Please select a file before confirming upload.";
|
||||||
return;
|
return;
|
||||||
@@ -39,13 +39,19 @@ export default function UploadStudents() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log(`Data from sheet ${sheetName}:`, data);
|
console.log(`Data from sheet ${sheetName}:`, data);
|
||||||
await insertIntoMobility(data as Array<{ firstName: string; lastName: string; email: string }>, sheetName);
|
await insertIntoMobility(
|
||||||
|
data as Array<
|
||||||
|
{ firstName: string; lastName: string; email: string }
|
||||||
|
>,
|
||||||
|
sheetName,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
statusMessage.value = "File uploaded and data inserted successfully!";
|
statusMessage.value = "File uploaded and data inserted successfully!";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error reading or inserting file:", error);
|
console.error("Error reading or inserting file:", error);
|
||||||
statusMessage.value = "Error processing the file. Please check its format.";
|
statusMessage.value =
|
||||||
|
"Error processing the file. Please check its format.";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
|
|
||||||
import { RouteConfig } from "$fresh/server.ts";
|
import { RouteConfig } from "$fresh/server.ts";
|
||||||
import UploadStudents from "../(_islands)/UploadStudents.tsx";
|
import UploadStudents from "../(_islands)/UploadStudents.tsx";
|
||||||
//import ConsultStudents from "../(_islands)/ConsultStudents.tsx";
|
//import ConsultStudents from "../(_islands)/ConsultStudents.tsx";
|
||||||
//import EditStudents from "../(_islands)/EditStudents.tsx";
|
//import EditStudents from "../(_islands)/EditStudents.tsx";
|
||||||
|
|
||||||
export const config: RouteConfig = {
|
export const config: RouteConfig = {
|
||||||
skipAppWrapper: false,
|
skipAppWrapper: false,
|
||||||
skipInheritedLayouts: false,
|
skipInheritedLayouts: false,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function Students() {
|
export default function Students() {
|
||||||
@@ -16,7 +14,6 @@ export default function Students() {
|
|||||||
<h1>Manage Promotions</h1>
|
<h1>Manage Promotions</h1>
|
||||||
<UploadStudents />
|
<UploadStudents />
|
||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user