Using connect.ts to attach databases
This commit is contained in:
@@ -1,31 +1,17 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import { Database } from "@db/sqlite";
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
// deno-lint-ignore require-await
|
// deno-lint-ignore require-await
|
||||||
async GET() {
|
async GET() {
|
||||||
|
console.log("API /mobility/api/insert_mobility GET called");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("Connecting to mobility database...");
|
console.log("Connecting to mobility database...");
|
||||||
const connection = new Database("databases/data/mobility.db", {
|
using connection = connect("mobility");
|
||||||
create: false,
|
|
||||||
});
|
|
||||||
connection.run(
|
|
||||||
"ATTACH DATABASE 'databases/data/students.db' AS students",
|
|
||||||
);
|
|
||||||
console.log("Connected to databases.");
|
console.log("Connected to databases.");
|
||||||
|
|
||||||
const students = connection.prepare(
|
const mobilities = connection.database.prepare(
|
||||||
`SELECT
|
|
||||||
students.userId AS id,
|
|
||||||
students.firstName,
|
|
||||||
students.lastName,
|
|
||||||
students.promotionId AS promotionId,
|
|
||||||
promotions.name AS promotionName
|
|
||||||
FROM students.students
|
|
||||||
LEFT JOIN students.promotions ON students.promotionId = promotions.id`,
|
|
||||||
).all();
|
|
||||||
|
|
||||||
const mobilities = connection.prepare(
|
|
||||||
`SELECT
|
`SELECT
|
||||||
mobility.id,
|
mobility.id,
|
||||||
mobility.studentId,
|
mobility.studentId,
|
||||||
@@ -35,14 +21,23 @@ export const handler: Handlers = {
|
|||||||
mobility.destinationCountry,
|
mobility.destinationCountry,
|
||||||
mobility.destinationName,
|
mobility.destinationName,
|
||||||
mobility.mobilityStatus
|
mobility.mobilityStatus
|
||||||
FROM mobility`,
|
FROM mobility`
|
||||||
).all();
|
).all();
|
||||||
|
|
||||||
const promotions = connection.prepare(
|
const students = connection.database.prepare(
|
||||||
`SELECT id, name FROM students.promotions`,
|
`SELECT
|
||||||
|
students.userId AS id,
|
||||||
|
students.firstName,
|
||||||
|
students.lastName,
|
||||||
|
students.promotionId AS promotionId,
|
||||||
|
promotions.name AS promotionName
|
||||||
|
FROM students.students
|
||||||
|
LEFT JOIN students.promotions ON students.promotionId = promotions.id`
|
||||||
).all();
|
).all();
|
||||||
|
|
||||||
connection.close();
|
const promotions = connection.database.prepare(
|
||||||
|
`SELECT id, name FROM students.promotions`
|
||||||
|
).all();
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ mobilities, students, promotions }),
|
JSON.stringify({ mobilities, students, promotions }),
|
||||||
@@ -68,17 +63,10 @@ export const handler: Handlers = {
|
|||||||
throw new Error("Invalid request body");
|
throw new Error("Invalid request body");
|
||||||
}
|
}
|
||||||
console.log("Connecting to mobility database...");
|
console.log("Connecting to mobility database...");
|
||||||
const connection = new Database("databases/data/mobility.db", {
|
using connection = connect("mobility");
|
||||||
create: false,
|
console.log("Connected to databases.");
|
||||||
});
|
|
||||||
|
|
||||||
console.log("Attaching students database...");
|
const insertQuery = connection.database.prepare(
|
||||||
connection.run(
|
|
||||||
"ATTACH DATABASE 'databases/data/students.db' AS students",
|
|
||||||
);
|
|
||||||
console.log("Students database attached successfully.");
|
|
||||||
|
|
||||||
const insertQuery = connection.prepare(
|
|
||||||
`INSERT INTO mobility (
|
`INSERT INTO mobility (
|
||||||
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus
|
||||||
)
|
)
|
||||||
@@ -89,12 +77,12 @@ export const handler: Handlers = {
|
|||||||
weeksCount = excluded.weeksCount,
|
weeksCount = excluded.weeksCount,
|
||||||
destinationCountry = excluded.destinationCountry,
|
destinationCountry = excluded.destinationCountry,
|
||||||
destinationName = excluded.destinationName,
|
destinationName = excluded.destinationName,
|
||||||
mobilityStatus = excluded.mobilityStatus`,
|
mobilityStatus = excluded.mobilityStatus`
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const mobility of data) {
|
for (const mobility of data) {
|
||||||
const {
|
const {
|
||||||
id,
|
id = null,
|
||||||
studentId,
|
studentId,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
@@ -104,45 +92,18 @@ export const handler: Handlers = {
|
|||||||
mobilityStatus = "N/A",
|
mobilityStatus = "N/A",
|
||||||
} = mobility;
|
} = mobility;
|
||||||
|
|
||||||
console.log("Processing mobility data:", mobility);
|
|
||||||
|
|
||||||
const studentExists = connection
|
|
||||||
.prepare(
|
|
||||||
`SELECT COUNT(*) AS count FROM students.students WHERE userId = ?`,
|
|
||||||
)
|
|
||||||
.get(studentId);
|
|
||||||
|
|
||||||
console.log(`Student ${studentId} exists:`, studentExists.count > 0);
|
|
||||||
|
|
||||||
if (studentExists.count === 0) {
|
|
||||||
console.warn(`Skipping mobility for unknown studentId: ${studentId}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let calculatedWeeksCount = weeksCount;
|
let calculatedWeeksCount = weeksCount;
|
||||||
if (startDate && endDate) {
|
if (startDate && endDate) {
|
||||||
const start = new Date(startDate);
|
const start = new Date(startDate);
|
||||||
const end = new Date(endDate);
|
const end = new Date(endDate);
|
||||||
if (start <= end) {
|
if (start <= end) {
|
||||||
calculatedWeeksCount = Math.ceil(
|
calculatedWeeksCount = Math.ceil((end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000));
|
||||||
(end.getTime() - start.getTime()) / (7 * 24 * 60 * 60 * 1000),
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
calculatedWeeksCount = null;
|
calculatedWeeksCount = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Executing SQL insert/update query for:", {
|
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
||||||
id,
|
|
||||||
studentId,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
calculatedWeeksCount,
|
|
||||||
destinationCountry,
|
|
||||||
destinationName,
|
|
||||||
mobilityStatus,
|
|
||||||
});
|
|
||||||
|
|
||||||
insertQuery.run(
|
insertQuery.run(
|
||||||
id,
|
id,
|
||||||
studentId,
|
studentId,
|
||||||
@@ -151,15 +112,12 @@ export const handler: Handlers = {
|
|||||||
calculatedWeeksCount,
|
calculatedWeeksCount,
|
||||||
destinationCountry,
|
destinationCountry,
|
||||||
destinationName,
|
destinationName,
|
||||||
mobilityStatus,
|
mobilityStatus
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.close();
|
|
||||||
console.log("Mobility data inserted/updated successfully.");
|
console.log("Mobility data inserted/updated successfully.");
|
||||||
return new Response("Data inserted/updated successfully", {
|
return new Response("Data inserted/updated successfully", { status: 200 });
|
||||||
status: 200,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error inserting mobility data:", error);
|
console.error("Error inserting mobility data:", error);
|
||||||
return new Response("Failed to insert/update data", { status: 500 });
|
return new Response("Failed to insert/update data", { status: 500 });
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
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 = {
|
||||||
|
|||||||
Reference in New Issue
Block a user