Update student to have an ID
This commit is contained in:
@@ -13,7 +13,7 @@ interface Promotion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Mobility {
|
interface Mobility {
|
||||||
id: number;
|
id: number | null;
|
||||||
studentId: string;
|
studentId: string;
|
||||||
startDate: string | null;
|
startDate: string | null;
|
||||||
endDate: string | null;
|
endDate: string | null;
|
||||||
@@ -63,7 +63,20 @@ export default function EditMobility() {
|
|||||||
|
|
||||||
const updatedMobilities = prevData.mobilities?.map((mobility) => {
|
const updatedMobilities = prevData.mobilities?.map((mobility) => {
|
||||||
if (mobility.studentId === studentId) {
|
if (mobility.studentId === studentId) {
|
||||||
return { ...mobility, [field]: value };
|
const updatedMobility = { ...mobility, [field]: value };
|
||||||
|
|
||||||
|
if (field === "startDate" || field === "endDate") {
|
||||||
|
const startDate = new Date(updatedMobility.startDate || "");
|
||||||
|
const endDate = new Date(updatedMobility.endDate || "");
|
||||||
|
if (startDate && endDate && startDate <= endDate) {
|
||||||
|
const weeks = Math.ceil((endDate.getTime() - startDate.getTime()) / (7 * 24 * 60 * 60 * 1000));
|
||||||
|
updatedMobility.weeksCount = weeks;
|
||||||
|
} else {
|
||||||
|
updatedMobility.weeksCount = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedMobility;
|
||||||
}
|
}
|
||||||
return mobility;
|
return mobility;
|
||||||
}) || [];
|
}) || [];
|
||||||
@@ -86,7 +99,7 @@ export default function EditMobility() {
|
|||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
alert("Data saved successfully!");
|
alert("Data saved successfully!");
|
||||||
window.location.reload(); // Refresh the page to load updated data
|
window.location.reload();
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to save data: ${response.statusText}`);
|
throw new Error(`Failed to save data: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
@@ -131,6 +144,7 @@ export default function EditMobility() {
|
|||||||
?.filter((student) => student.promotionId === promo.id)
|
?.filter((student) => student.promotionId === promo.id)
|
||||||
.map((student) => {
|
.map((student) => {
|
||||||
const mobility = data.mobilities?.find((mob) => mob.studentId === student.id) || {
|
const mobility = data.mobilities?.find((mob) => mob.studentId === student.id) || {
|
||||||
|
id: null,
|
||||||
studentId: student.id,
|
studentId: student.id,
|
||||||
startDate: null,
|
startDate: null,
|
||||||
endDate: null,
|
endDate: null,
|
||||||
@@ -159,15 +173,7 @@ export default function EditMobility() {
|
|||||||
onChange={(e) => handleChange(student.id, "endDate", e.target.value)}
|
onChange={(e) => handleChange(student.id, "endDate", e.target.value)}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>{mobility.weeksCount ?? "N/A"}</td>
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
value={mobility.weeksCount || ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
handleChange(student.id, "weeksCount", Number(e.target.value) || null)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td>
|
<td>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
|
|||||||
@@ -65,15 +65,30 @@ export const handler: Handlers = {
|
|||||||
|
|
||||||
const connection = new Database("databases/data/mobility.db", { create: false });
|
const connection = new Database("databases/data/mobility.db", { create: false });
|
||||||
|
|
||||||
|
console.log("Attaching students database...");
|
||||||
|
connection.run("ATTACH DATABASE 'databases/data/students.db' AS students");
|
||||||
|
console.log("Students database attached successfully.");
|
||||||
|
|
||||||
|
const testStudents = connection.prepare("SELECT COUNT(*) AS count FROM students.students").get();
|
||||||
|
console.log(`Students table accessible, total records: ${testStudents.count}`);
|
||||||
|
|
||||||
const insertQuery = connection.prepare(
|
const insertQuery = connection.prepare(
|
||||||
`INSERT INTO mobility (
|
`INSERT INTO mobility (
|
||||||
studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT(id) DO UPDATE SET
|
||||||
|
startDate = excluded.startDate,
|
||||||
|
endDate = excluded.endDate,
|
||||||
|
weeksCount = excluded.weeksCount,
|
||||||
|
destinationCountry = excluded.destinationCountry,
|
||||||
|
destinationName = excluded.destinationName,
|
||||||
|
mobilityStatus = excluded.mobilityStatus`
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const mobility of data) {
|
for (const mobility of data) {
|
||||||
const {
|
const {
|
||||||
|
id = null,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -82,12 +97,35 @@ export const handler: Handlers = {
|
|||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = mobility;
|
||||||
|
|
||||||
|
let calculatedWeeksCount = weeksCount;
|
||||||
|
if (startDate && endDate) {
|
||||||
|
const start = new Date(startDate);
|
||||||
|
const end = new Date(endDate);
|
||||||
|
if (start <= end) {
|
||||||
|
calculatedWeeksCount = Math.ceil((end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000));
|
||||||
|
} else {
|
||||||
|
calculatedWeeksCount = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
||||||
|
|
||||||
|
const studentExists = connection
|
||||||
|
.prepare(`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`)
|
||||||
|
.get(studentId);
|
||||||
|
|
||||||
|
if (studentExists.count === 0) {
|
||||||
|
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
insertQuery.run(
|
insertQuery.run(
|
||||||
|
id,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
weeksCount,
|
calculatedWeeksCount,
|
||||||
destinationCountry,
|
destinationCountry,
|
||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus
|
mobilityStatus
|
||||||
@@ -95,12 +133,11 @@ export const handler: Handlers = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connection.close();
|
connection.close();
|
||||||
|
console.log("Mobility data inserted/updated successfully.");
|
||||||
console.log("Mobility data inserted successfully");
|
return new Response("Data inserted/updated successfully", { status: 200 });
|
||||||
return new Response("Data inserted successfully", { status: 201 });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error inserting mobility data:", error);
|
console.error("Error inserting mobility data:", error);
|
||||||
return new Response("Failed to insert data", { status: 500 });
|
return new Response("Failed to insert/update data", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -6,12 +6,11 @@ interface Promotion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface Student {
|
interface Student {
|
||||||
id: number;
|
userId: string;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName: string;
|
||||||
mail: string;
|
mail: string;
|
||||||
promotionId: number;
|
promotionId: number;
|
||||||
promotionName: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ConsultStudents() {
|
export default function ConsultStudents() {
|
||||||
@@ -27,6 +26,7 @@ export default function ConsultStudents() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
console.log("Fetched data:", result);
|
||||||
setData(result);
|
setData(result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error fetching data:", err);
|
console.error("Error fetching data:", err);
|
||||||
@@ -43,7 +43,7 @@ export default function ConsultStudents() {
|
|||||||
{error && <p className="error">{error}</p>}
|
{error && <p className="error">{error}</p>}
|
||||||
{data?.promotions.map((promo) => (
|
{data?.promotions.map((promo) => (
|
||||||
<div key={promo.id}>
|
<div key={promo.id}>
|
||||||
<h3>Promotion: {promo.id}</h3>
|
<h3>Promotion: {promo.name}</h3>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -57,8 +57,8 @@ export default function ConsultStudents() {
|
|||||||
{data.students
|
{data.students
|
||||||
.filter((student) => student.promotionId === promo.id)
|
.filter((student) => student.promotionId === promo.id)
|
||||||
.map((student) => (
|
.map((student) => (
|
||||||
<tr key={student.id}>
|
<tr key={student.userId}>
|
||||||
<td>{student.id}</td>
|
<td>{student.userId}</td>
|
||||||
<td>{student.firstName}</td>
|
<td>{student.firstName}</td>
|
||||||
<td>{student.lastName}</td>
|
<td>{student.lastName}</td>
|
||||||
<td>{student.mail}</td>
|
<td>{student.mail}</td>
|
||||||
|
|||||||
@@ -33,10 +33,12 @@ export default function UploadStudents() {
|
|||||||
for (const sheetName of workbook.SheetNames) {
|
for (const sheetName of workbook.SheetNames) {
|
||||||
const sheet = workbook.Sheets[sheetName];
|
const sheet = workbook.Sheets[sheetName];
|
||||||
const data = XLSX.utils.sheet_to_json(sheet, {
|
const data = XLSX.utils.sheet_to_json(sheet, {
|
||||||
header: ["Nom", "Prénom", "Mail"],
|
header: ["Identifiant", "Nom", "Prénom", "Mail"],
|
||||||
range: 1, // Ignorer les en-têtes
|
range: 1, // Ignorer les en-têtes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(`Data from sheet ${sheetName}:`, data);
|
||||||
|
|
||||||
const response = await fetch("/students/api/insert_students", {
|
const response = await fetch("/students/api/insert_students", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import { Database } from "@db/sqlite";
|
||||||
import connect from "$root/databases/connect.ts";
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
@@ -7,7 +8,7 @@ export const handler: Handlers = {
|
|||||||
using connection = connect("students");
|
using connection = connect("students");
|
||||||
|
|
||||||
const promotions = connection.database.prepare(
|
const promotions = connection.database.prepare(
|
||||||
"select id from promotions",
|
"select id, name from promotions",
|
||||||
).all();
|
).all();
|
||||||
|
|
||||||
const students = connection.database
|
const students = connection.database
|
||||||
@@ -30,7 +31,7 @@ export const handler: Handlers = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async POST(request) {
|
async POST(request) {
|
||||||
console.log("API /mobility/api/insert_students called");
|
console.log("API /students/api/insert_students called");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
@@ -48,7 +49,7 @@ export const handler: Handlers = {
|
|||||||
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
||||||
).run(promoName);
|
).run(promoName);
|
||||||
|
|
||||||
const promoIdRow: {id: string} = connection.database
|
const promoIdRow: { id: number } = connection.database
|
||||||
.prepare("SELECT id FROM promotions WHERE name = ?")
|
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||||
.get(promoName)!;
|
.get(promoName)!;
|
||||||
const promoId = promoIdRow.id;
|
const promoId = promoIdRow.id;
|
||||||
@@ -56,12 +57,20 @@ export const handler: Handlers = {
|
|||||||
console.log(`Promotion ID for "${promoName}":`, promoId);
|
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||||
|
|
||||||
const insertQuery = connection.database.prepare(
|
const insertQuery = connection.database.prepare(
|
||||||
"INSERT INTO students (firstName, lastName, mail, promotionId) VALUES (?, ?, ?, ?)",
|
`INSERT INTO students
|
||||||
|
(userId, firstName, lastName, mail, promotionId)
|
||||||
|
VALUES (?, ?, ?, ?, ?)`,
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const student of data) {
|
for (const student of data) {
|
||||||
console.log("Inserting student:", student);
|
console.log("Inserting student:", student);
|
||||||
insertQuery.run(student.Nom, student["Prénom"], student.Mail, promoId);
|
insertQuery.run(
|
||||||
|
student.Identifiant,
|
||||||
|
student.Nom,
|
||||||
|
student["Prénom"],
|
||||||
|
student.Mail,
|
||||||
|
promoId,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("All data inserted successfully");
|
console.log("All data inserted successfully");
|
||||||
|
|||||||
Reference in New Issue
Block a user