diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 793542cbf..f694fdf71 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -1,8 +1,11 @@ const { - getMonitorsByIdValidation, + getMonitorByIdValidation, getMonitorsByUserIdValidation, + createMonitorValidation, } = require("../validation/joi"); + const logger = require("../utils/logger"); +const Monitor = require("../models/Monitor"); /** * Returns all monitors @@ -31,7 +34,7 @@ const getAllMonitors = async (req, res) => { * @throws {Error} */ const getMonitorById = async (req, res) => { - const { error } = getMonitorsByIdValidation.validate(req.params); + const { error } = getMonitorByIdValidation.validate(req.params); if (error) { return res .status(400) @@ -82,4 +85,68 @@ const getMonitorsByUserId = async (req, res) => { } }; -module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; +/** + * Creates a new monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ + +const createMonitor = async (req, res) => { + const { error } = createMonitorValidation.validate(req.body); + if (error) { + return res + .status(400) + .json({ success: false, msg: error.details[0].message }); + } + + try { + const monitor = await req.db.createMonitor(req, res); + return res + .status(200) + .json({ success: true, msg: "Monitor created", data: monitor }); + } catch (error) { + logger.error(error.message, { service: "monitor" }); + return res.status(500).json({ success: false, msg: error.message }); + } +}; + +// Delete Monitor +/** + * Delete a monitor by ID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ + +const deleteMonitor = async (req, res) => { + const { error } = getMonitorByIdValidation.validate(req.params); + if (error) { + return res + .status(400) + .json({ success: false, msg: error.details[0].message }); + } + try { + const monitor = await req.db.deleteMonitor(req, res); + return res + .status(200) + .json({ success: true, msg: "Monitor deleted", data: monitor }); + } catch (error) { + logger.error(error.message, { service: "monitor" }); + return res.status(500).json({ success: false, msg: error.message }); + } +}; + +// Edit Monitor + +module.exports = { + getAllMonitors, + getMonitorById, + getMonitorsByUserId, + createMonitor, + deleteMonitor, +}; diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js index a21b66ffd..1510bc55e 100644 --- a/Server/db/FakeDb.js +++ b/Server/db/FakeDb.js @@ -24,7 +24,7 @@ const Monitor = require("../models/Monitor"); const UserModel = require("../models/user"); const bcrypt = require("bcrypt"); -const FAKE_MONITOR_DATA = []; +let FAKE_MONITOR_DATA = []; const USERS = []; for (let i = 0; i < 10; i++) { @@ -99,10 +99,28 @@ const getMonitorsByUserId = async (userId) => { if (userMonitors.length === 0) { throw new Error(`Monitors for user ${userId} not found`); } - return userMonitors; }; +const createMonitor = async (req, res) => { + const monitor = new Monitor(req.body); + FAKE_MONITOR_DATA.push(monitor); + return monitor; +}; + +const deleteMonitor = async (req, res) => { + const monitorId = req.params.monitorId; + try { + const monitor = getMonitorById(monitorId); + FAKE_MONITOR_DATA = FAKE_MONITOR_DATA.filter((monitor) => { + return monitor.id !== monitorId; + }); + return monitor; + } catch (error) { + throw error; + } +}; + module.exports = { connect, insertUser, @@ -110,4 +128,6 @@ module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId, + createMonitor, + deleteMonitor, }; diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index b6a126ab8..7546cda37 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -1,7 +1,6 @@ const Monitor = require("../models/Monitor"); const mongoose = require("mongoose"); const UserModel = require("../models/user"); -const user = require("../models/user"); const connect = async () => { try { @@ -13,6 +12,14 @@ const connect = async () => { } }; +/** + * Insert a User + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const insertUser = async (req, res) => { try { const newUser = new UserModel({ ...req.body }); @@ -23,8 +30,16 @@ const insertUser = async (req, res) => { } }; -// Gets a user by Email. Not sure if we'll ever need this except for login. -// If not needed except for login, we can move password comparison here +/** + * Get User by Email + * Gets a user by Email. Not sure if we'll ever need this except for login. + * If not needed except for login, we can move password comparison here + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const getUserByEmail = async (req, res) => { try { // Returns null if no user is found @@ -36,7 +51,14 @@ const getUserByEmail = async (req, res) => { } }; -// Gets all monitors +/** + * Get all monitors + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise>} + * @throws {Error} + */ const getAllMonitors = async (req, res) => { try { const monitors = await Monitor.find(); @@ -46,7 +68,14 @@ const getAllMonitors = async (req, res) => { } }; -// Get a monitor by ID +/** + * Get a monitor by ID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const getMonitorById = async (req, res) => { try { const monitor = await Monitor.findById(req.params.monitorId); @@ -56,7 +85,14 @@ const getMonitorById = async (req, res) => { } }; -// Gets a monitor by user ID +/** + * Get monitors by UserID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise>} + * @throws {Error} + */ const getMonitorsByUserId = async (req, res) => { try { const monitors = await Monitor.find({ userId: req.params.userId }); @@ -66,6 +102,46 @@ const getMonitorsByUserId = async (req, res) => { } }; +/** + * Create a monitor + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ +const createMonitor = async (req, res) => { + try { + const monitor = new Monitor({ ...req.body }); + await monitor.save(); + return monitor; + } catch (error) { + throw error; + } +}; + +// Delete Monitor +/** + * Delete a monitor by ID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ +const deleteMonitor = async (req, res) => { + const monitorId = req.params.monitorId; + try { + const monitor = await Monitor.findByIdAndDelete(monitorId); + if (!monitor) { + throw new Error(`Monitor with id ${monitorId} not found`); + } + return monitor; + } catch (error) { + throw error; + } +}; + module.exports = { connect, insertUser, @@ -73,4 +149,6 @@ module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId, + createMonitor, + deleteMonitor, }; diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index e12982905..07c20e13f 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -1,7 +1,10 @@ const router = require("express").Router(); const monitorController = require("../controllers/monitorController"); + router.get("/", monitorController.getAllMonitors); router.get("/:monitorId", monitorController.getMonitorById); router.get("/user/:userId", monitorController.getMonitorsByUserId); +router.post("/", monitorController.createMonitor); +router.post("/delete/:monitorId", monitorController.deleteMonitor); module.exports = router; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 874daff93..61c8ffeb3 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -12,7 +12,7 @@ const registerValidation = joi.object({ password: joi.string().min(8).required(), }); -const getMonitorsByIdValidation = joi.object({ +const getMonitorByIdValidation = joi.object({ monitorId: joi.string().required(), }); @@ -20,9 +20,19 @@ const getMonitorsByUserIdValidation = joi.object({ userId: joi.string().required(), }); +const createMonitorValidation = joi.object({ + userId: joi.string().required(), + name: joi.string().required(), + description: joi.string().required(), + url: joi.string().uri().required(), + isActive: joi.boolean(), + interval: joi.number(), +}); + module.exports = { loginValidation, registerValidation, - getMonitorsByIdValidation, + getMonitorByIdValidation, getMonitorsByUserIdValidation, + createMonitorValidation, };