From 2e2d91937e533b1e0026324dc019018032227f9f Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 13:59:33 -0700 Subject: [PATCH] Added fake DB for monitor route testing --- Server/controllers/monitorController.js | 48 +++++++++++++++++++++--- Server/db/FakeDb.js | 46 +++++++++++++++++++++++ Server/models/{monitor.js => Monitor.js} | 0 Server/routes/monitorRoute.js | 2 +- Server/validation/joi.js | 11 +++++- 5 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 Server/db/FakeDb.js rename Server/models/{monitor.js => Monitor.js} (100%) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 1044d3f48..f7a85ce4c 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -1,9 +1,20 @@ -const { getMonitorsByIdValidation } = require("../validation/joi"); +const { + getMonitorsByIdValidation, + getMonitorsByUserIdValidation, +} = require("../validation/joi"); +const db = require("../db/FakeDb"); +// Gets all monitors const getAllMonitors = async (req, res) => { - res.json({ message: "Hello from Monitor Controller" }); + try { + const monitors = await db.getAllMonitors(); + return res.json({ success: true, msg: "Monitors found", data: monitors }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } }; +// Get a monitor by ID const getMonitorById = async (req, res) => { const { error } = getMonitorsByIdValidation.validate(req.params); if (error) { @@ -12,8 +23,35 @@ const getMonitorById = async (req, res) => { .json({ success: false, msg: error.details[0].message }); } - const monitorId = req.params.monitorId; - return res.json({ success: true, msg: "Monitor found", data: { monitorId } }); + try { + const monitorId = req.params.monitorId; + const monitor = await db.getMonitorById(monitorId); + return res.json({ success: true, msg: "Monitor found", data: monitor }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } }; -module.exports = { getAllMonitors, getMonitorById }; +// Gets a monitor by user ID +const getMonitorsByUserId = async (req, res) => { + const { error } = getMonitorsByUserIdValidation.validate(req.params); + if (error) { + return res + .status(400) + .json({ success: false, msg: error.details[0].message }); + } + + try { + const userId = req.params.userId; + const monitors = await db.getMonitorsByUserId(userId); + return res.json({ + success: true, + msg: `Monitors for user ${userId} found`, + data: monitors, + }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } +}; + +module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js new file mode 100644 index 000000000..b604a9294 --- /dev/null +++ b/Server/db/FakeDb.js @@ -0,0 +1,46 @@ +const Monitor = require("../models/Monitor"); + +const FAKE_MONITOR_DATA = []; + +for (let i = 0; i < 10; i++) { + FAKE_MONITOR_DATA.push( + new Monitor({ + userId: i % 2 === 0 ? 1 : 2, + name: `Monitor ${i}`, + description: `Description for Monitor ${i}`, + url: `https://monitor${i}.com`, + isActive: true, + interval: 60000, + updated_at: new Date(), + created_at: new Date(), + }) + ); +} + +const getAllMonitors = async () => { + return FAKE_MONITOR_DATA; +}; + +const getMonitorById = async (monitorId) => { + const idx = FAKE_MONITOR_DATA.findIndex((monitor) => { + return monitor.id === monitorId; + }); + if (idx === -1) { + throw new Error(`Monitor with id ${monitorId} not found`); + } + return FAKE_MONITOR_DATA[idx]; +}; + +const getMonitorsByUserId = async (userId) => { + const userMonitors = FAKE_MONITOR_DATA.filter((monitor) => { + return monitor.userId === userId; + }); + + if (userMonitors.length === 0) { + throw new Error(`Monitors for user ${userId} not found`); + } + + return userMonitors; +}; + +module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; diff --git a/Server/models/monitor.js b/Server/models/Monitor.js similarity index 100% rename from Server/models/monitor.js rename to Server/models/Monitor.js diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index 724ee79b6..7c6767364 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -1,8 +1,8 @@ const router = require("express").Router(); const monitorController = require("../controllers/monitorController"); -const { getMonitorsByIdValidation } = require("../validation/joi"); router.get("/", monitorController.getAllMonitors); router.get("/:monitorId", monitorController.getMonitorById); +router.get("/user/:userId", monitorController.getMonitorsByUserId); module.exports = router; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index c5b2b58d1..2fb59a377 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -1,4 +1,5 @@ const joi = require("joi"); +const user = require("../models/user"); const authValidation = joi.object({ email: joi.string().email().required(), @@ -9,4 +10,12 @@ const getMonitorsByIdValidation = joi.object({ monitorId: joi.string().required(), }); -module.exports = { authValidation, getMonitorsByIdValidation }; +const getMonitorsByUserIdValidation = joi.object({ + userId: joi.string().required(), +}); + +module.exports = { + authValidation, + getMonitorsByIdValidation, + getMonitorsByUserIdValidation, +};