From ab93c9959d03296dbe42286c517b47b2d105f289 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Sun, 21 Sep 2025 00:48:28 +0200 Subject: [PATCH] fullfilled landingpage --- admin/src/components/API/Landingpage.tsx | 204 ++++++++++++++++++++++- backend/routes/apiV2.js | 9 + backend/services/database.js | 10 ++ 3 files changed, 222 insertions(+), 1 deletion(-) diff --git a/admin/src/components/API/Landingpage.tsx b/admin/src/components/API/Landingpage.tsx index c0eef14..a32d66e 100644 --- a/admin/src/components/API/Landingpage.tsx +++ b/admin/src/components/API/Landingpage.tsx @@ -1,9 +1,211 @@ import React from "react"; +import { useEffect } from "react"; +import { useState } from "react"; +import { Spinner, Text, VStack, Box } from "@chakra-ui/react"; +import { Table, Heading } from "@chakra-ui/react"; +import { formatDateTime } from "@/utils/userFuncs"; const Landingpage: React.FC = () => { + const [isLoading, setIsLoading] = useState(false); + const [loans, setLoans] = useState([]); + + useEffect(() => { + setIsLoading(true); + fetch("http://localhost:8002/apiV2/allLoans") + .then((response) => response.json()) + .then((data) => { + setLoans(data); + setIsLoading(false); + }) + .catch((error) => { + console.error("Error fetching loans:", error); + setIsLoading(false); + }); + }, []); + return ( <> -

Übersicht über alle Gegenstände und Ausleihen

+ + Matthias-Claudius-Schule Technik + + + Alle Ausleihen + + {isLoading && ( + + + Loading... + + )} + {!isLoading && ( + + + + + + + + + + + + # + + + Username + + + Start Date + + + End Date + + + Loaned Items + + + Returned Date + + + Take Date + + + + + {loans.map((loan) => ( + + + {loan.id} + + {loan.username} + + {formatDateTime(loan.start_date)} + + + {formatDateTime(loan.end_date)} + + + {loan.loaned_items_name} + + + {formatDateTime(loan.returned_date)} + + + {formatDateTime(loan.take_date)} + + + ))} + + + + + )} ); }; diff --git a/backend/routes/apiV2.js b/backend/routes/apiV2.js index faecec2..1cfb906 100644 --- a/backend/routes/apiV2.js +++ b/backend/routes/apiV2.js @@ -6,6 +6,7 @@ import { setReturnDateV2, setTakeDateV2, getLoanByCodeV2, + getAllLoansV2, } from "../services/database.js"; dotenv.config(); @@ -90,4 +91,12 @@ router.post("/setTakeDate/:key/:loan_code", async (req, res) => { } }); +router.get("/allLoans", async (req, res) => { + const result = await getAllLoansV2(); + if (result.success) { + return res.status(200).json(result.data); + } + return res.status(500).json({ message: "Failed to fetch loans" }); +}); + export default router; diff --git a/backend/services/database.js b/backend/services/database.js index 8a83a0e..4e1492a 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -447,3 +447,13 @@ export const updateItemByID = async (itemId, item_name, can_borrow_role) => { if (result.affectedRows > 0) return { success: true }; return { success: false }; }; + +export const getAllLoansV2 = async () => { + const [rows] = await pool.query( + "SELECT id, username, start_date, end_date, loaned_items_name, returned_date, take_date FROM loans" + ); + if (rows.length > 0) { + return { success: true, data: rows }; + } + return { success: false }; +};