Trying to make mobility works
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import connect from "$root/databases/connect.ts";
|
import { Database } from "@db/sqlite";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
async GET() {
|
async GET() {
|
||||||
try {
|
try {
|
||||||
using connection = connect("mobility");
|
const connection = new Database("databases/data/mobility.db", { create: false });
|
||||||
|
|
||||||
const mobilities = connection.database.prepare(
|
const mobilities = connection.prepare(
|
||||||
`SELECT
|
`SELECT
|
||||||
mobility.id,
|
mobility.id,
|
||||||
mobility.studentId,
|
mobility.studentId,
|
||||||
@@ -19,9 +19,11 @@ export const handler: Handlers = {
|
|||||||
mobility.destinationName,
|
mobility.destinationName,
|
||||||
mobility.mobilityStatus
|
mobility.mobilityStatus
|
||||||
FROM mobility
|
FROM mobility
|
||||||
LEFT JOIN students ON mobility.studentId = students.userId`,
|
LEFT JOIN students ON mobility.studentId = students.userId`
|
||||||
).all();
|
).all();
|
||||||
|
|
||||||
|
connection.close();
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ mobilities }),
|
JSON.stringify({ mobilities }),
|
||||||
{
|
{
|
||||||
@@ -36,7 +38,7 @@ export const handler: Handlers = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async POST(request) {
|
async POST(request) {
|
||||||
console.log("API /mobility/api/update_mobility called");
|
console.log("API /mobility/api/insert_mobility called");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
@@ -46,25 +48,17 @@ export const handler: Handlers = {
|
|||||||
throw new Error("Invalid request body");
|
throw new Error("Invalid request body");
|
||||||
}
|
}
|
||||||
|
|
||||||
using connection = connect("mobility");
|
const connection = new Database("databases/data/mobility.db", { create: false });
|
||||||
|
|
||||||
const updateQuery = connection.database.prepare(
|
const insertQuery = connection.prepare(
|
||||||
`INSERT INTO mobility (
|
`INSERT INTO mobility (
|
||||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
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,
|
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -74,8 +68,7 @@ export const handler: Handlers = {
|
|||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = mobility;
|
||||||
|
|
||||||
updateQuery.run(
|
insertQuery.run(
|
||||||
id,
|
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -86,11 +79,13 @@ export const handler: Handlers = {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Mobility data updated successfully");
|
connection.close();
|
||||||
return new Response("Data updated successfully", { status: 200 });
|
|
||||||
|
console.log("Mobility data inserted successfully");
|
||||||
|
return new Response("Data inserted successfully", { status: 201 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error updating mobility data:", error);
|
console.error("Error inserting mobility data:", error);
|
||||||
return new Response("Failed to update data", { status: 500 });
|
return new Response("Failed to insert data", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
|
||||||
import connect from "$root/databases/connect.ts";
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
|
||||||
async GET() {
|
|
||||||
try {
|
|
||||||
using connection = connect("students");
|
|
||||||
|
|
||||||
const promotions = connection.database.prepare(
|
|
||||||
"select id 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 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user