Adding table promotion (consult not working yet)
This commit is contained in:
@@ -2,8 +2,7 @@ import { Handlers } from "$fresh/server.ts";
|
||||
import { Database } from "@db/sqlite";
|
||||
|
||||
export const handler: Handlers = {
|
||||
// deno-lint-ignore require-await
|
||||
async GET(_request, _context) {
|
||||
async GET(_request, context) {
|
||||
try {
|
||||
const db = new Database("databases/data/mobility.db");
|
||||
|
||||
@@ -14,14 +13,17 @@ export const handler: Handlers = {
|
||||
firstName TEXT NOT NULL,
|
||||
lastName TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
promotion TEXT NOT NULL
|
||||
promotionId INTEGER NOT NULL,
|
||||
FOREIGN KEY (promotionId) REFERENCES promotions (id)
|
||||
);
|
||||
`,
|
||||
`
|
||||
).run();
|
||||
|
||||
const rows = db.prepare(
|
||||
"SELECT id, firstName, lastName, email, promotion FROM students",
|
||||
).all();
|
||||
const rows = db
|
||||
.prepare(
|
||||
"SELECT students.id, firstName, lastName, email, promotionId FROM students"
|
||||
)
|
||||
.all();
|
||||
|
||||
db.close();
|
||||
|
||||
@@ -52,6 +54,15 @@ export const handler: Handlers = {
|
||||
|
||||
console.log("Database opened successfully");
|
||||
|
||||
db.prepare(
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS promotions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL
|
||||
);
|
||||
`
|
||||
).run();
|
||||
|
||||
db.prepare(
|
||||
`
|
||||
CREATE TABLE IF NOT EXISTS students (
|
||||
@@ -59,25 +70,33 @@ export const handler: Handlers = {
|
||||
firstName TEXT NOT NULL,
|
||||
lastName TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
promotion TEXT NOT NULL
|
||||
promotionId INTEGER NOT NULL,
|
||||
FOREIGN KEY (promotionId) REFERENCES promotions (id)
|
||||
);
|
||||
`,
|
||||
`
|
||||
).run();
|
||||
|
||||
console.log("Table ensured successfully");
|
||||
console.log("Tables ensured successfully");
|
||||
|
||||
// Insérer ou récupérer l'ID de la promotion
|
||||
db.prepare(
|
||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)"
|
||||
).run(promoName);
|
||||
|
||||
const promoIdRow = db
|
||||
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||
.get(promoName);
|
||||
const promoId = promoIdRow.id;
|
||||
|
||||
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||
|
||||
const insertQuery = db.prepare(
|
||||
"INSERT INTO students (firstName, lastName, email, promotion) VALUES (?, ?, ?, ?)",
|
||||
"INSERT INTO students (firstName, lastName, email, promotionId) VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
|
||||
for (const student of data) {
|
||||
console.log("Inserting student:", student);
|
||||
insertQuery.run(
|
||||
student.Nom,
|
||||
student["Prénom"],
|
||||
student.Mail,
|
||||
promoName,
|
||||
);
|
||||
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
||||
}
|
||||
|
||||
console.log("All students inserted successfully");
|
||||
|
||||
Reference in New Issue
Block a user