Adding table promotion (consult not working yet)
This commit is contained in:
@@ -1,64 +1,95 @@
|
|||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
|
interface Promotion {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface Student {
|
interface Student {
|
||||||
id: number;
|
id: number;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
email: string;
|
email: string;
|
||||||
promotion: string;
|
promotionId: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ConsultStudents() {
|
export default function ConsultStudents() {
|
||||||
const [students, setStudents] = useState<Student[]>([]);
|
const [promotions, setPromotions] = useState<Promotion[]>([]);
|
||||||
|
const [studentsByPromotion, setStudentsByPromotion] = useState<Record<number, Student[]>>({});
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchStudents = async () => {
|
const fetchPromotionsAndStudents = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/mobility/api/insert_students");
|
// Récupérer toutes les promotions
|
||||||
|
const promoResponse = await fetch("/mobility/api/promotions");
|
||||||
if (!response.ok) {
|
if (!promoResponse.ok) {
|
||||||
throw new Error(`Error fetching students: ${response.statusText}`);
|
throw new Error(`Error fetching promotions: ${promoResponse.statusText}`);
|
||||||
}
|
}
|
||||||
|
const promos: Promotion[] = await promoResponse.json();
|
||||||
|
setPromotions(promos);
|
||||||
|
|
||||||
const data: Student[] = await response.json();
|
// Récupérer les étudiants
|
||||||
setStudents(data);
|
const studentsResponse = await fetch("/mobility/api/insert_students");
|
||||||
|
if (!studentsResponse.ok) {
|
||||||
|
throw new Error(`Error fetching students: ${studentsResponse.statusText}`);
|
||||||
|
}
|
||||||
|
const students: Student[] = await studentsResponse.json();
|
||||||
|
|
||||||
|
// Grouper les étudiants par promotionId
|
||||||
|
const grouped: Record<number, Student[]> = {};
|
||||||
|
for (const student of students) {
|
||||||
|
if (!grouped[student.promotionId]) {
|
||||||
|
grouped[student.promotionId] = [];
|
||||||
|
}
|
||||||
|
grouped[student.promotionId].push(student);
|
||||||
|
}
|
||||||
|
setStudentsByPromotion(grouped);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error fetching students:", err);
|
console.error("Error fetching promotions or students:", err);
|
||||||
setError("Failed to load students. Please try again later.");
|
setError("Failed to load data. Please try again later.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchStudents();
|
fetchPromotionsAndStudents();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section>
|
<section>
|
||||||
<h2>Consult Students</h2>
|
<h2>Consult Students</h2>
|
||||||
{error && <p className="error">{error}</p>}
|
{error && <p className="error">{error}</p>}
|
||||||
{students.length === 0 ? <p>No students found.</p> : (
|
{promotions.length === 0 ? (
|
||||||
<table>
|
<p>No promotions found.</p>
|
||||||
<thead>
|
) : (
|
||||||
<tr>
|
promotions.map((promo) => (
|
||||||
<th>ID</th>
|
<div key={promo.id}>
|
||||||
<th>First Name</th>
|
<h3>Promotion: {promo.name}</h3>
|
||||||
<th>Last Name</th>
|
{studentsByPromotion[promo.id]?.length ? (
|
||||||
<th>Email</th>
|
<table>
|
||||||
<th>Promotion</th>
|
<thead>
|
||||||
</tr>
|
<tr>
|
||||||
</thead>
|
<th>ID</th>
|
||||||
<tbody>
|
<th>First Name</th>
|
||||||
{students.map((student) => (
|
<th>Last Name</th>
|
||||||
<tr key={student.id}>
|
<th>Email</th>
|
||||||
<td>{student.id}</td>
|
</tr>
|
||||||
<td>{student.firstName}</td>
|
</thead>
|
||||||
<td>{student.lastName}</td>
|
<tbody>
|
||||||
<td>{student.email}</td>
|
{studentsByPromotion[promo.id].map((student) => (
|
||||||
<td>{student.promotion}</td>
|
<tr key={student.id}>
|
||||||
</tr>
|
<td>{student.id}</td>
|
||||||
))}
|
<td>{student.firstName}</td>
|
||||||
</tbody>
|
<td>{student.lastName}</td>
|
||||||
</table>
|
<td>{student.email}</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
) : (
|
||||||
|
<p>No students in this promotion.</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))
|
||||||
)}
|
)}
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import { Handlers } from "$fresh/server.ts";
|
|||||||
import { Database } from "@db/sqlite";
|
import { Database } from "@db/sqlite";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
// deno-lint-ignore require-await
|
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");
|
||||||
|
|
||||||
@@ -14,14 +13,17 @@ export const handler: Handlers = {
|
|||||||
firstName TEXT NOT NULL,
|
firstName TEXT NOT NULL,
|
||||||
lastName TEXT NOT NULL,
|
lastName TEXT NOT NULL,
|
||||||
email TEXT NOT NULL,
|
email TEXT NOT NULL,
|
||||||
promotion TEXT NOT NULL
|
promotionId INTEGER NOT NULL,
|
||||||
|
FOREIGN KEY (promotionId) REFERENCES promotions (id)
|
||||||
);
|
);
|
||||||
`,
|
`
|
||||||
).run();
|
).run();
|
||||||
|
|
||||||
const rows = db.prepare(
|
const rows = db
|
||||||
"SELECT id, firstName, lastName, email, promotion FROM students",
|
.prepare(
|
||||||
).all();
|
"SELECT students.id, firstName, lastName, email, promotionId FROM students"
|
||||||
|
)
|
||||||
|
.all();
|
||||||
|
|
||||||
db.close();
|
db.close();
|
||||||
|
|
||||||
@@ -52,6 +54,15 @@ export const handler: Handlers = {
|
|||||||
|
|
||||||
console.log("Database opened successfully");
|
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(
|
db.prepare(
|
||||||
`
|
`
|
||||||
CREATE TABLE IF NOT EXISTS students (
|
CREATE TABLE IF NOT EXISTS students (
|
||||||
@@ -59,25 +70,33 @@ export const handler: Handlers = {
|
|||||||
firstName TEXT NOT NULL,
|
firstName TEXT NOT NULL,
|
||||||
lastName TEXT NOT NULL,
|
lastName TEXT NOT NULL,
|
||||||
email TEXT NOT NULL,
|
email TEXT NOT NULL,
|
||||||
promotion TEXT NOT NULL
|
promotionId INTEGER NOT NULL,
|
||||||
|
FOREIGN KEY (promotionId) REFERENCES promotions (id)
|
||||||
);
|
);
|
||||||
`,
|
`
|
||||||
).run();
|
).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(
|
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) {
|
for (const student of data) {
|
||||||
console.log("Inserting student:", student);
|
console.log("Inserting student:", student);
|
||||||
insertQuery.run(
|
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
||||||
student.Nom,
|
|
||||||
student["Prénom"],
|
|
||||||
student.Mail,
|
|
||||||
promoName,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("All students inserted successfully");
|
console.log("All students inserted successfully");
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import { Database } from "@db/sqlite";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async GET() {
|
||||||
|
try {
|
||||||
|
const db = new Database("databases/data/mobility.db");
|
||||||
|
|
||||||
|
db.prepare(
|
||||||
|
`
|
||||||
|
CREATE TABLE IF NOT EXISTS promotions (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT UNIQUE NOT NULL
|
||||||
|
);
|
||||||
|
`
|
||||||
|
).run();
|
||||||
|
|
||||||
|
const promotions = db.prepare("SELECT id, name FROM promotions").all();
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(promotions), {
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching promotions:", error);
|
||||||
|
return new Response("Failed to fetch promotions", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user