From 36c5c9cf39ce086abf5272cf2287bc65ca2b6edb Mon Sep 17 00:00:00 2001 From: Clayzxr Date: Wed, 22 Jan 2025 11:35:47 +0100 Subject: [PATCH] Test to access student DB in other apps (working) --- fresh.gen.ts | 6 ++ .../(_islands)/ConsultStudents_test.tsx | 73 +++++++++++++++++++ routes/(apps)/mobility/(_props)/props.ts | 3 +- .../mobility/api/insert_students_test.ts | 31 ++++++++ .../(admin)/consult_students_test.tsx | 17 +++++ routes/(apps)/mobility/partials/index.tsx | 10 +-- 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx create mode 100644 routes/(apps)/mobility/api/insert_students_test.ts create mode 100644 routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx diff --git a/fresh.gen.ts b/fresh.gen.ts index be51c53..654e3be 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -5,6 +5,7 @@ import * as $_apps_layout from "./routes/(apps)/_layout.tsx"; import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx"; import * as $_apps_mobility_partials_admin_mobility from "./routes/(apps)/mobility/partials/(admin)/mobility.tsx"; +import * as $_apps_mobility_partials_admin_consult_students_test from "./routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx"; import * as $_apps_mobility_partials_index from "./routes/(apps)/mobility/partials/index.tsx"; import * as $_apps_mobility_partials_overview from "./routes/(apps)/mobility/partials/overview.tsx"; import * as $_apps_notes_index from "./routes/(apps)/notes/index.tsx"; @@ -29,6 +30,7 @@ import * as $logout from "./routes/logout.tsx"; import * as $_islands_AppNavigator from "./routes/(_islands)/AppNavigator.tsx"; import * as $_islands_Navbar from "./routes/(_islands)/Navbar.tsx"; import * as $_apps_mobility_islands_ConsultMobility from "./routes/(apps)/mobility/(_islands)/ConsultMobility.tsx"; +import * as $_apps_mobility_islands_ConsultStudents_test from "./routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx"; import * as $_apps_mobility_islands_EditMobility from "./routes/(apps)/mobility/(_islands)/EditMobility.tsx"; import * as $_apps_mobility_islands_ImportFile from "./routes/(apps)/mobility/(_islands)/ImportFile.tsx"; import * as $_apps_students_islands_ConsultStudents from "./routes/(apps)/students/(_islands)/ConsultStudents.tsx"; @@ -42,6 +44,8 @@ const manifest = { "./routes/(apps)/mobility/index.tsx": $_apps_mobility_index, "./routes/(apps)/mobility/partials/(admin)/mobility.tsx": $_apps_mobility_partials_admin_mobility, + "./routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx": + $_apps_mobility_partials_admin_consult_students_test, "./routes/(apps)/mobility/partials/index.tsx": $_apps_mobility_partials_index, "./routes/(apps)/mobility/partials/overview.tsx": @@ -78,6 +82,8 @@ const manifest = { "./routes/(_islands)/Navbar.tsx": $_islands_Navbar, "./routes/(apps)/mobility/(_islands)/ConsultMobility.tsx": $_apps_mobility_islands_ConsultMobility, + "./routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx": + $_apps_mobility_islands_ConsultStudents_test, "./routes/(apps)/mobility/(_islands)/EditMobility.tsx": $_apps_mobility_islands_EditMobility, "./routes/(apps)/mobility/(_islands)/ImportFile.tsx": diff --git a/routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx b/routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx new file mode 100644 index 0000000..f05ba34 --- /dev/null +++ b/routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx @@ -0,0 +1,73 @@ +import { useEffect, useState } from "preact/hooks"; + +interface Promotion { + id: number; + name: string; +} + +interface Student { + id: number; + firstName: string; + lastName: string; + mail: string; + promotionId: number; + promotionName: string; +} + +export default function ConsultStudents_test() { + const [data, setData] = useState<{ promotions: Promotion[]; students: Student[] } | null>(null); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchData = async () => { + try { + const response = await fetch("/students/api/insert_students"); + if (!response.ok) { + throw new Error(`Error fetching data: ${response.statusText}`); + } + + const result = await response.json(); + setData(result); + } catch (err) { + console.error("Error fetching data:", err); + setError("Failed to load data. Please try again later."); + } + }; + + fetchData(); + }, []); + + return ( +
+

Consult Students

+ {error &&

{error}

} + {data?.promotions.map((promo) => ( +
+

Promotion: {promo.id}

+ + + + + + + + + + + {data.students + .filter((student) => student.promotionId === promo.id) + .map((student) => ( + + + + + + + ))} + +
IDFirst NameLast NameEmail
{student.id}{student.firstName}{student.lastName}{student.mail}
+
+ ))} +
+ ); +} diff --git a/routes/(apps)/mobility/(_props)/props.ts b/routes/(apps)/mobility/(_props)/props.ts index 6003875..82165f2 100644 --- a/routes/(apps)/mobility/(_props)/props.ts +++ b/routes/(apps)/mobility/(_props)/props.ts @@ -8,8 +8,9 @@ const properties: AppProperties = { index: "Homepage", overview: "Mobility overview", mobility: "Mobility management", + consult_students_test: "Test consult students", }, - adminOnly: ["mobility"], + adminOnly: ["mobility", "consult_students_test"], }; export default properties; diff --git a/routes/(apps)/mobility/api/insert_students_test.ts b/routes/(apps)/mobility/api/insert_students_test.ts new file mode 100644 index 0000000..d0123af --- /dev/null +++ b/routes/(apps)/mobility/api/insert_students_test.ts @@ -0,0 +1,31 @@ +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 }); + } + }, +}; diff --git a/routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx b/routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx new file mode 100644 index 0000000..ee5d9c1 --- /dev/null +++ b/routes/(apps)/mobility/partials/(admin)/consult_students_test.tsx @@ -0,0 +1,17 @@ +import ConsultStudents_test from "$root/routes/(apps)/mobility/(_islands)/ConsultStudents_test.tsx"; +import { getPartialsConfig, makePartials } from "$root/defaults/makePartials.tsx"; +import { FreshContext } from "$fresh/server.ts"; +import { State } from "$root/routes/_middleware.ts"; +//import EditStudents from "../(_islands)/EditStudents.tsx"; + +async function Mobility(_request: Request, _context: FreshContext) { + return ( + <> +

Test consult students

+ + + ); +} + +export const config = getPartialsConfig(); +export default makePartials(Mobility); diff --git a/routes/(apps)/mobility/partials/index.tsx b/routes/(apps)/mobility/partials/index.tsx index a5cffdb..2971e0e 100644 --- a/routes/(apps)/mobility/partials/index.tsx +++ b/routes/(apps)/mobility/partials/index.tsx @@ -2,12 +2,12 @@ import { getPartialsConfig, makePartials, } from "$root/defaults/makePartials.tsx"; -import { EmptyObject } from "$root/defaults/interfaces.ts"; +import { FreshContext } from "$fresh/server.ts"; +import { State } from "$root/routes/_middleware.ts"; -type MobilityIndexProps = EmptyObject; - -export function Index(_props: MobilityIndexProps) { - return

Nothing to see here...

; +// deno-lint-ignore require-await +export async function Index(_request: Request, context: FreshContext) { + return

Welcome to {context.state.session?.displayName}.

; } export const config = getPartialsConfig();