Working DB for students insertion

This commit is contained in:
Clayzxr
2025-01-21 15:45:38 +01:00
parent 0672666bd3
commit e049056295
2 changed files with 34 additions and 25 deletions
@@ -6,8 +6,6 @@ export default function UploadStudents() {
const statusMessage = useSignal<string>("");
const fileData = useSignal<File | null>(null);
console.log("Component UploadStudents mounted");
const handleFileChange = (event: Event) => {
const input = event.target as HTMLInputElement;
if (input.files && input.files.length > 0) {
@@ -17,12 +15,11 @@ export default function UploadStudents() {
} else {
fileData.value = null;
statusMessage.value = "No file selected";
console.log("No file selected.");
}
};
const confirmUpload = async () => {
console.log("Confirm Upload clicked");
console.log("Confirm Upload");
if (!fileData.value) {
statusMessage.value = "Please select a file before confirming upload.";
console.error("Error: No file selected.");
@@ -45,8 +42,8 @@ export default function UploadStudents() {
});
console.log(`Data from sheet ${sheetName}:`, data);
const response = await fetch("/api/insert_students", {
const response = await fetch("/mobility/api/insert_students", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ promoName: sheetName, data }),
+31 -19
View File
@@ -1,12 +1,13 @@
import { Handlers } from "$fresh/server.ts";
import { Database } from "@db/sqlite";
import { Database } from "@db/sqlite";
export const handler: Handlers = {
async GET(_request, context) {
try {
const db = new Database("databases/data/mobility.db");
db.execute(`
db.prepare(
`
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName TEXT NOT NULL,
@@ -14,18 +15,16 @@ export const handler: Handlers = {
email TEXT NOT NULL,
promotion TEXT NOT NULL
);
`);
`
).run();
const students = [];
for (const [id, firstName, lastName, email, promotion] of db.query(
const rows = db.prepare(
"SELECT id, firstName, lastName, email, promotion FROM students"
)) {
students.push({ id, firstName, lastName, email, promotion });
}
).all();
db.close();
return new Response(JSON.stringify(students), {
return new Response(JSON.stringify(rows), {
status: 200,
headers: { "Content-Type": "application/json" },
});
@@ -36,13 +35,24 @@ export const handler: Handlers = {
},
async POST(request) {
console.log("API /mobility/api/insert_students called");
try {
const body = await request.json();
const { data, promoName } = body;
console.log("Received data:", { promoName, data });
if (!promoName || !Array.isArray(data)) {
throw new Error("Invalid request body");
}
const db = new Database("databases/data/mobility.db");
db.execute(`
console.log("Database opened successfully");
db.prepare(
`
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
firstName TEXT NOT NULL,
@@ -50,19 +60,21 @@ export const handler: Handlers = {
email TEXT NOT NULL,
promotion TEXT NOT NULL
);
`);
`
).run();
console.log("Table ensured successfully");
const insertQuery = db.prepare(
"INSERT INTO students (firstName, lastName, email, promotion) VALUES (?, ?, ?, ?)"
);
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("Inserting student:", student);
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoName);
}
console.log("All students inserted successfully");
db.close();
return new Response("Students inserted successfully", { status: 201 });