summaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorArne Rief <riearn@proton.me>2025-12-22 12:28:33 +0100
committerArne Rief <riearn@proton.me>2025-12-22 12:29:13 +0100
commit3818739c5901cc3f1d4596b24cfe1b827a2eca23 (patch)
tree18e0c755386e6598f1cfe4193866b0b62a8f368d /backend
parent237f8ae6c29bbf485c312b2fed4d5ab4f99a4eff (diff)
FE Sidebar, create & move requests, BE create controller
Diffstat (limited to 'backend')
-rw-r--r--backend/src/controllers/createRobot.ts61
-rw-r--r--backend/src/routes/router.ts4
-rw-r--r--backend/src/types/express.d.ts6
-rw-r--r--backend/src/types/request.ts5
4 files changed, 76 insertions, 0 deletions
diff --git a/backend/src/controllers/createRobot.ts b/backend/src/controllers/createRobot.ts
new file mode 100644
index 0000000..fa483af
--- /dev/null
+++ b/backend/src/controllers/createRobot.ts
@@ -0,0 +1,61 @@
+import { Request, Response } from "express";
+import { QueryResult } from "pg";
+import db from "../database/postgres.js";
+import redisClient from "../database/redis.js";
+import { CreateRequest } from "../types/request.js";
+import { Robot } from "../types/robot.js";
+
+const ROBOTS_CACHE_KEY = "allMyRobots";
+
+async function createRobot(req: Request, res: Response) {
+ const io = req.app.get("io");
+
+ const { name } = req.body as CreateRequest;
+
+ if (!name || !name.trim()) {
+ return res.status(400).json({
+ message: "Robot name is required.",
+ });
+ }
+
+ try {
+ const createNewRobotQuery: QueryResult<Robot> = await db.query(
+ `
+ INSERT INTO robots
+ (name, status, lat, lon, robot_positions)
+ VALUES ($1, $2, $3, $4, $5)
+ RETURNING
+ id, name, status, lat, lon, robot_positions, created_at, updated_at;`,
+ [name.trim(), "idle", 51.340863, 12.375919, JSON.stringify([])]
+ );
+
+ const newRobot = createNewRobotQuery.rows[0];
+
+ // Delete old Redis cache, get all robots again and broadcast update to the frontend
+ await redisClient.del(ROBOTS_CACHE_KEY);
+ console.log("Redis cache deleted after robot creation.");
+
+ const allRobotsQuery: QueryResult<Robot> = await db.query(
+ "SELECT * FROM robots ORDER BY id;"
+ );
+
+ const allRobots = allRobotsQuery.rows;
+
+ io.emit("robots_update", allRobots);
+ console.log("WebSocket update with newly created robot.");
+
+ return res.status(201).json({
+ message: "Robot successfully created.",
+ robot: newRobot,
+ });
+ } catch (error) {
+ console.error("Error creating the robot: ", error);
+ return res.status(500).json({
+ message: "Internal server error during robot creation.",
+ error,
+ });
+ }
+}
+
+export default createRobot;
+
diff --git a/backend/src/routes/router.ts b/backend/src/routes/router.ts
index e67a64b..b526a9b 100644
--- a/backend/src/routes/router.ts
+++ b/backend/src/routes/router.ts
@@ -1,4 +1,5 @@
import { Router } from "express";
+import createRobot from "../controllers/createRobot.js";
import generateAdmin from "../controllers/generateAdmin.js";
import getRobots from "../controllers/getRobots.js";
import loginUser from "../controllers/loginUser.js";
@@ -12,5 +13,8 @@ router.get("/admin-generation", generateAdmin);
router.post("/auth/login", loginUser);
// Get robots from database; protected route
router.get("/robots", authenticateUser, getRobots);
+// Create a new robot; protected route
+router.post("/robots", authenticateUser, createRobot);
export default router;
+
diff --git a/backend/src/types/express.d.ts b/backend/src/types/express.d.ts
index 7ac6ca0..266a9f9 100644
--- a/backend/src/types/express.d.ts
+++ b/backend/src/types/express.d.ts
@@ -1,7 +1,12 @@
import type { AuthorizedUser } from "./user.js";
+import { Server } from "socket.io";
declare global {
namespace Express {
+ interface Application {
+ io: Server;
+ }
+
interface Request {
user?: AuthorizedUser;
}
@@ -9,3 +14,4 @@ declare global {
}
export {};
+
diff --git a/backend/src/types/request.ts b/backend/src/types/request.ts
index ef80738..6524b16 100644
--- a/backend/src/types/request.ts
+++ b/backend/src/types/request.ts
@@ -1,4 +1,9 @@
+export type CreateRequest = {
+ name: string;
+};
+
export type LoginRequest = {
email: string;
password: string;
};
+