Added JSDocs for monitor controller, Updated readme, Added DB_TYPE environtmental variable for switching between DB types
This commit is contained in:
1 parent
9e32111d39
commit
ddc4021232
2 files changed
+42
-9
No files matched your search
@@ -2,20 +2,34 @@ const {
|
|||||||
getMonitorsByIdValidation,
|
getMonitorsByIdValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const logger = require('../utils/logger')
|
const logger = require("../utils/logger");
|
||||||
|
|
||||||
// Gets all monitors
|
/**
|
||||||
|
* Returns all monitors
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
const getAllMonitors = async (req, res) => {
|
const getAllMonitors = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const monitors = await req.db.getAllMonitors();
|
const monitors = await req.db.getAllMonitors();
|
||||||
return res.json({ success: true, msg: "Monitors found", data: monitors });
|
return res.json({ success: true, msg: "Monitors found", data: monitors });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error.message, { "service": "monitor" });
|
logger.error(error.message, { service: "monitor" });
|
||||||
return res.status(500).json({ success: false, msg: error.message });
|
return res.status(500).json({ success: false, msg: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get a monitor by ID
|
/**
|
||||||
|
* Returns monitor with matching ID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
const getMonitorById = async (req, res) => {
|
const getMonitorById = async (req, res) => {
|
||||||
const { error } = getMonitorsByIdValidation.validate(req.params);
|
const { error } = getMonitorsByIdValidation.validate(req.params);
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -29,12 +43,19 @@ const getMonitorById = async (req, res) => {
|
|||||||
const monitor = await req.db.getMonitorById(monitorId);
|
const monitor = await req.db.getMonitorById(monitorId);
|
||||||
return res.json({ success: true, msg: "Monitor found", data: monitor });
|
return res.json({ success: true, msg: "Monitor found", data: monitor });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error.message, { "service": "monitor" });
|
logger.error(error.message, { service: "monitor" });
|
||||||
return res.status(500).json({ success: false, msg: error.message });
|
return res.status(500).json({ success: false, msg: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Gets a monitor by user ID
|
/**
|
||||||
|
* Returns all monitors belong to User with UserID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
const getMonitorsByUserId = async (req, res) => {
|
const getMonitorsByUserId = async (req, res) => {
|
||||||
const { error } = getMonitorsByUserIdValidation.validate(req.params);
|
const { error } = getMonitorsByUserIdValidation.validate(req.params);
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -46,14 +67,17 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const userId = req.params.userId;
|
const userId = req.params.userId;
|
||||||
const monitors = await req.db.getMonitorsByUserId(userId);
|
const monitors = await req.db.getMonitorsByUserId(userId);
|
||||||
logger.info(`Monitors for user ${userId} found`, { "service": "monitor", "userId":userId });
|
logger.info(`Monitors for user ${userId} found`, {
|
||||||
|
service: "monitor",
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
return res.json({
|
return res.json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: `Monitors for user ${userId} found`,
|
msg: `Monitors for user ${userId} found`,
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error.message, { "service": "monitor" });
|
logger.error(error.message, { service: "monitor" });
|
||||||
return res.status(500).json({ success: false, msg: error.message });
|
return res.status(500).json({ success: false, msg: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+10
-1
@@ -17,10 +17,19 @@ const logger = require("./utils/logger");
|
|||||||
// const db = require("./db/FakeDb");
|
// const db = require("./db/FakeDb");
|
||||||
//
|
//
|
||||||
// MongoDB
|
// MongoDB
|
||||||
const db = require("./db/MongoDB");
|
// const db = require("./db/MongoDB");
|
||||||
//
|
//
|
||||||
// **************************
|
// **************************
|
||||||
|
|
||||||
|
const DB_TYPE = {
|
||||||
|
MongoDB: () => require("./db/MongoDB"),
|
||||||
|
FakedDB: () => require("./db/FakeDb"),
|
||||||
|
};
|
||||||
|
|
||||||
|
const db = DB_TYPE[process.env.DB_TYPE]
|
||||||
|
? DB_TYPE[process.env.DB_TYPE]()
|
||||||
|
: require("./db/FakeDb");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTES
|
* NOTES
|
||||||
* Email Service will be added
|
* Email Service will be added
|
||||||
|
|||||||
Reference in new issue
Block a user