Added create and delete monitor routes, Added JSDocs to MongoDB

This commit is contained in:
Alex Holliday committed 2024-05-21 11:23:39 -07:00
1 parent d18b88aa15
commit e7cd1ae6ab
5 files changed
+191 -13

No files matched your search

+22 -2
View File
@@ -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,
};
+84 -6
View File
@@ -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<UserModel>}
* @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<UserModel>}
* @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<Array<Monitor>>}
* @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<Monitor>}
* @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<Array<Monitor>>}
* @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<Monitor>}
* @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<Monitor>}
* @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,
};