Commentaire pour moi le lendemain à 6h du matin (heure canadienne)
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import connect from "$root/databases/connect.ts";
|
||||
|
||||
// Rendre l'API plus simple car xlsx pour l'import c'est nul :/
|
||||
export const handler: Handlers = {
|
||||
// deno-lint-ignore require-await
|
||||
async GET() {
|
||||
try {
|
||||
using connection = connect("students");
|
||||
|
||||
const promotions = connection.database.prepare(
|
||||
"select id, name from promotions",
|
||||
).all();
|
||||
|
||||
const students = connection.database
|
||||
.prepare(
|
||||
`select userId, firstName, lastName, mail, promotionId from students`,
|
||||
)
|
||||
.all();
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ promotions, students }),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
return new Response("Failed to fetch data", { status: 500 });
|
||||
}
|
||||
},
|
||||
|
||||
async POST(request) {
|
||||
console.log("API /students/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");
|
||||
}
|
||||
|
||||
using connection = connect("students");
|
||||
|
||||
connection.database.prepare(
|
||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
||||
).run(promoName);
|
||||
|
||||
const promoIdRow: { id: number } = connection.database
|
||||
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||
.get(promoName)!;
|
||||
const promoId = promoIdRow.id;
|
||||
|
||||
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||
|
||||
const insertQuery = connection.database.prepare(
|
||||
`INSERT INTO students
|
||||
(userId, firstName, lastName, mail, promotionId)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
);
|
||||
|
||||
for (const student of data) {
|
||||
console.log("Inserting student:", student);
|
||||
insertQuery.run(
|
||||
student.Identifiant,
|
||||
student.Nom,
|
||||
student["Prénom"],
|
||||
student.Mail,
|
||||
promoId,
|
||||
);
|
||||
}
|
||||
|
||||
console.log("All data inserted successfully");
|
||||
return new Response("Data inserted successfully", { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Error inserting data:", error);
|
||||
return new Response("Failed to insert data", { status: 500 });
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user