diff options
Diffstat (limited to 'backend/src/controllers')
| -rw-r--r-- | backend/src/controllers/createRobot.ts | 18 | ||||
| -rw-r--r-- | backend/src/controllers/getRobots.ts | 14 | ||||
| -rw-r--r-- | backend/src/controllers/loginUser.ts | 32 | ||||
| -rw-r--r-- | backend/src/controllers/moveAllRobots.ts | 25 | ||||
| -rw-r--r-- | backend/src/controllers/moveRobot.ts | 30 | ||||
| -rw-r--r-- | backend/src/controllers/stopAllRobots.ts | 21 | ||||
| -rw-r--r-- | backend/src/controllers/stopRobot.ts | 27 |
7 files changed, 148 insertions, 19 deletions
diff --git a/backend/src/controllers/createRobot.ts b/backend/src/controllers/createRobot.ts index fa483af..dbc1959 100644 --- a/backend/src/controllers/createRobot.ts +++ b/backend/src/controllers/createRobot.ts @@ -1,14 +1,16 @@ import { Request, Response } from "express"; import { QueryResult } from "pg"; +import { Server } from "socket.io"; import db from "../database/postgres.js"; import redisClient from "../database/redis.js"; +import { ErrorResponse } from "../types/error.js"; import { CreateRequest } from "../types/request.js"; -import { Robot } from "../types/robot.js"; +import { CreateRobotResponse, Robot } from "../types/robot.js"; const ROBOTS_CACHE_KEY = "allMyRobots"; async function createRobot(req: Request, res: Response) { - const io = req.app.get("io"); + const io: Server = req.app.get("io"); const { name } = req.body as CreateRequest; @@ -44,16 +46,20 @@ async function createRobot(req: Request, res: Response) { io.emit("robots_update", allRobots); console.log("WebSocket update with newly created robot."); - return res.status(201).json({ + const createRobotResponse: CreateRobotResponse = { message: "Robot successfully created.", robot: newRobot, - }); + }; + + return res.status(201).json(createRobotResponse); } catch (error) { console.error("Error creating the robot: ", error); - return res.status(500).json({ + + const errorResponse: ErrorResponse = { message: "Internal server error during robot creation.", error, - }); + }; + return res.status(500).json(errorResponse); } } diff --git a/backend/src/controllers/getRobots.ts b/backend/src/controllers/getRobots.ts index b634306..80b430f 100644 --- a/backend/src/controllers/getRobots.ts +++ b/backend/src/controllers/getRobots.ts @@ -17,9 +17,14 @@ async function getRobots(_req: Request, res: Response) { console.log("Data served from Redis cache."); const robots: Robot[] = JSON.parse(cachedData); + const simulationRunning = robots?.every( + (robot) => robot.status === "moving" + ); + const response: RobotsResponse = { source: "cache", - data: robots, + robots, + simulationRunning, }; return res.status(200).json(response); @@ -31,6 +36,10 @@ async function getRobots(_req: Request, res: Response) { ); const robots = robotsQuery.rows; + const simulationRunning = robots?.every( + (robot) => robot.status === "moving" + ); + await redisClient.set(ROBOTS_CACHE_KEY, JSON.stringify(robots), { EX: CACHE_TTL, }); @@ -38,7 +47,8 @@ async function getRobots(_req: Request, res: Response) { console.log("Cache miss: data queried from PostgreSQL."); const response: RobotsResponse = { source: "database", - data: robots, + robots, + simulationRunning, }; return res.status(200).json(response); diff --git a/backend/src/controllers/loginUser.ts b/backend/src/controllers/loginUser.ts index 860bce2..9cfb992 100644 --- a/backend/src/controllers/loginUser.ts +++ b/backend/src/controllers/loginUser.ts @@ -3,15 +3,20 @@ import { Request, Response } from "express"; import jwt from "jsonwebtoken"; import { QueryResult } from "pg"; import db from "../database/postgres.js"; +import { ErrorResponse } from "../types/error.js"; import type { LoginRequest } from "../types/request.js"; -import type { AuthorizedUser, DatabaseUser } from "../types/user.js"; +import type { + AuthorizedUser, + DatabaseUser, + LoginResponse, +} from "../types/user.js"; async function loginUser(req: Request, res: Response) { const { email, password } = req.body as LoginRequest; if (!email || !password) { return res.status(400).json({ - message: "E-Mail und Passwort sind erforderlich.", + message: "E-mail and password are required.", }); } @@ -25,7 +30,7 @@ async function loginUser(req: Request, res: Response) { const user = queryResult.rows[0]; if (!user) { - return res.status(401).json({ message: "Login Daten ungültig." }); + return res.status(401).json({ message: "Login data invalid." }); } // Check if password is correct @@ -36,7 +41,7 @@ async function loginUser(req: Request, res: Response) { if (!isValidPassword) { return res.status(401).json({ - message: "Das Passwort ist nicht korrekt.", + message: "The password is incorrect.", }); } @@ -49,16 +54,21 @@ async function loginUser(req: Request, res: Response) { // Create token for authentication const token = jwt.sign(userData, process.env.JWT_SECRET!); - return res.status(200).json({ - message: "Erfolgreiche Anmeldung.", + const loginResponse: LoginResponse = { + message: "Successful login.", user: userData, token, - }); + }; + + return res.status(200).json(loginResponse); } catch (error) { - console.error("Fehler beim Login: ", error); - return res - .status(500) - .json({ message: "Interner Serverfehler beim Login.", error }); + console.error("Error on login attempt: ", error); + + const errorResponse: ErrorResponse = { + message: "Internal server error on login attempt.", + error, + }; + return res.status(500).json(errorResponse); } } diff --git a/backend/src/controllers/moveAllRobots.ts b/backend/src/controllers/moveAllRobots.ts new file mode 100644 index 0000000..e4d1252 --- /dev/null +++ b/backend/src/controllers/moveAllRobots.ts @@ -0,0 +1,25 @@ +import { Request, Response } from "express"; +import { Server } from "socket.io"; +import { setAllRobotsMoving } from "../simulation/robotMovementSimulator"; +import { ErrorResponse } from "../types/error"; +import { SimulationResponse } from "../types/robot"; + +async function moveAllRobots(req: Request, res: Response) { + const io: Server = req.app.get("io"); + + try { + const result: SimulationResponse = await setAllRobotsMoving(io); + return res.status(200).json(result); + } catch (error) { + console.error("Error on trying to set all robots moving:", error); + + const errorResponse: ErrorResponse = { + message: + "Internal server error on trying to set all robots moving.", + error, + }; + return res.status(500).json(errorResponse); + } +} + +export default moveAllRobots; diff --git a/backend/src/controllers/moveRobot.ts b/backend/src/controllers/moveRobot.ts new file mode 100644 index 0000000..1b53f17 --- /dev/null +++ b/backend/src/controllers/moveRobot.ts @@ -0,0 +1,30 @@ +import { Request, Response } from "express"; +import { Server } from "socket.io"; +import { setRobotMoving } from "../simulation/robotMovementSimulator"; +import { ErrorResponse } from "../types/error"; +import { SimulationResponse } from "../types/robot"; + +async function moveRobot(req: Request, res: Response) { + const io: Server = req.app.get("io"); + + const robotId = Number(req.params.id); + + if (!robotId || Number.isNaN(robotId)) { + return res.status(400).json({ message: "Invalid robot ID." }); + } + + try { + const result: SimulationResponse = await setRobotMoving(io, robotId); + return res.status(200).json(result); + } catch (error) { + console.error(`Error on trying to start robot ID ${robotId}: `, error); + + const errorResponse: ErrorResponse = { + message: `Internal server error on trying to start robot ID ${robotId}.`, + error, + }; + return res.status(500).json(errorResponse); + } +} + +export default moveRobot; diff --git a/backend/src/controllers/stopAllRobots.ts b/backend/src/controllers/stopAllRobots.ts new file mode 100644 index 0000000..7218e66 --- /dev/null +++ b/backend/src/controllers/stopAllRobots.ts @@ -0,0 +1,21 @@ +import { Request, Response } from "express"; +import { setAllRobotsIdle } from "../simulation/robotMovementSimulator"; +import { ErrorResponse } from "../types/error"; +import { SimulationResponse } from "../types/robot"; + +async function stopAllRobots(_req: Request, res: Response) { + try { + const result: SimulationResponse = await setAllRobotsIdle(); + return res.status(200).json(result); + } catch (error) { + console.error("Error on trying to stop all robots:", error); + + const errorResponse: ErrorResponse = { + message: "Internal server error on trying to stop all robots.", + error, + }; + return res.status(500).json(errorResponse); + } +} + +export default stopAllRobots; diff --git a/backend/src/controllers/stopRobot.ts b/backend/src/controllers/stopRobot.ts new file mode 100644 index 0000000..d0d7c4f --- /dev/null +++ b/backend/src/controllers/stopRobot.ts @@ -0,0 +1,27 @@ +import { Request, Response } from "express"; +import { setRobotIdle } from "../simulation/robotMovementSimulator"; +import { ErrorResponse } from "../types/error"; +import { SimulationResponse } from "../types/robot"; + +async function stopRobot(req: Request, res: Response) { + const robotId = Number(req.params.id); + + if (!robotId || Number.isNaN(robotId)) { + return res.status(400).json({ message: "Ungültige Roboter ID." }); + } + + try { + const result: SimulationResponse = await setRobotIdle(robotId); + return res.status(200).json(result); + } catch (error) { + console.error(`Error on trying to stop robot ID ${robotId}: `, error); + + const errorResponse: ErrorResponse = { + message: `Internal server error on trying to stop robot ID ${robotId}.`, + error, + }; + return res.status(500).json(errorResponse); + } +} + +export default stopRobot; |
