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