From 237f8ae6c29bbf485c312b2fed4d5ab4f99a4eff Mon Sep 17 00:00:00 2001 From: Arne Rief Date: Sat, 20 Dec 2025 14:09:20 +0100 Subject: Map and loading robots --- backend/src/controllers/getRobots.ts | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 backend/src/controllers/getRobots.ts (limited to 'backend/src/controllers') diff --git a/backend/src/controllers/getRobots.ts b/backend/src/controllers/getRobots.ts new file mode 100644 index 0000000..b634306 --- /dev/null +++ b/backend/src/controllers/getRobots.ts @@ -0,0 +1,56 @@ +import { Request, Response } from "express"; +import { QueryResult } from "pg"; +import db from "../database/postgres.js"; +import redisClient from "../database/redis.js"; +import { ErrorResponse } from "../types/error.js"; +import { Robot, RobotsResponse } from "../types/robot.js"; + +const CACHE_TTL = 10; +const ROBOTS_CACHE_KEY = "allMyRobots"; + +async function getRobots(_req: Request, res: Response) { + try { + // Check if Redis has cached data to send to frontend + const cachedData = await redisClient.get(ROBOTS_CACHE_KEY); + + if (cachedData) { + console.log("Data served from Redis cache."); + const robots: Robot[] = JSON.parse(cachedData); + + const response: RobotsResponse = { + source: "cache", + data: robots, + }; + + return res.status(200).json(response); + } + + // Else query database, then save in Redis cache and send to frontend + const robotsQuery: QueryResult = await db.query( + "SELECT * FROM robots ORDER BY id;" + ); + const robots = robotsQuery.rows; + + await redisClient.set(ROBOTS_CACHE_KEY, JSON.stringify(robots), { + EX: CACHE_TTL, + }); + + console.log("Cache miss: data queried from PostgreSQL."); + const response: RobotsResponse = { + source: "database", + data: robots, + }; + + return res.status(200).json(response); + } catch (error) { + console.error("Failed to load the robots: ", error); + const errorResponse: ErrorResponse = { + message: "Internal server error while loading the robots.", + error, + }; + + return res.status(500).json(errorResponse); + } +} + +export default getRobots; -- cgit v1.2.3