Added fake DB for monitor route testing

This commit is contained in:
Alex Holliday committed 2024-05-10 13:59:33 -07:00
1 parent def8ba4b9d
commit 2e2d91937e
5 files changed
+100 -7

No files matched your search

+43 -5
View File
@@ -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 };
+46
View File
@@ -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 };
File renamed without changes.
+1 -1
View File
@@ -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;
+10 -1
View File
@@ -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,
};