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