Finished routes, validation, and scaffold DB
This commit is contained in:
1 parent
90baac5067
commit
fdb8c6b20a
4 files changed
+280
-21
No files matched your search
@@ -1,40 +1,132 @@
|
||||
const express = require("express");
|
||||
const {} = require("../validation/joi");
|
||||
const logger = require("../utils/logger");
|
||||
require("dotenv").config();
|
||||
const { createAlertParamValidation } = require("../validation/joi");
|
||||
const {
|
||||
createAlertParamValidation,
|
||||
createAlertBodyValidation,
|
||||
getAlertsByUserIdParamValidation,
|
||||
getAlertsByMonitorIdParamValidation,
|
||||
getAlertByIdParamValidation,
|
||||
editAlertParamValidation,
|
||||
editAlertBodyValidation,
|
||||
deleteAlertParamValidation,
|
||||
} = require("../validation/joi");
|
||||
|
||||
const SERVICE_NAME = "alerts";
|
||||
|
||||
/**
|
||||
* Creates an alert for a monitor
|
||||
* @async
|
||||
* @param {Express.Request} req
|
||||
* @param {Express.Response} res
|
||||
* @returns {Promise<Express.Response>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const createAlert = async (req, res, next) => {
|
||||
try {
|
||||
await createAlertParamValidation.validateAsync(req.params);
|
||||
res
|
||||
.status(200)
|
||||
.json({ success: true, msg: "Create Alert", data: req.params.monitorId });
|
||||
await createAlertBodyValidation.validateAsync(req.body);
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
req.db.createAlert(req, res);
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
const getAlertsByUserId = (req, res, next) => {
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: "Get Alerts By UserID",
|
||||
data: req.params.userId,
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets all alerts for a user
|
||||
* @async
|
||||
* @param {Express.Request} req
|
||||
* @param {Express.Response} res
|
||||
* @returns {Promise<Express.Response>}
|
||||
* @throws {Error}
|
||||
*/
|
||||
const getAlertsByUserId = async (req, res, next) => {
|
||||
try {
|
||||
await getAlertsByUserIdParamValidation.validateAsync(req.params);
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
req.db.getAlertsByUserId(req, res);
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
const getAlertsByMonitorId = (req, res, next) => {
|
||||
res.status(200).json({ success: true, msg: "Get Alerts By MonitorID" });
|
||||
|
||||
const getAlertsByMonitorId = async (req, res, next) => {
|
||||
try {
|
||||
await getAlertsByMonitorIdParamValidation.validateAsync(req.params);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: "Get Alerts By MonitorID",
|
||||
data: req.params.monitorId,
|
||||
});
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
res.status(200).json({ success: true, msg: "Edit alert" });
|
||||
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
};
|
||||
const getAlertById = (req, res, next) => {
|
||||
res.status(200).json({ success: true, msg: "Get Alert By alertID" });
|
||||
const getAlertById = async (req, res, next) => {
|
||||
try {
|
||||
await getAlertByIdParamValidation.validateAsync(req.params);
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
msg: "Get Alert By alertID",
|
||||
data: req.params.alertId,
|
||||
});
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
};
|
||||
const editAlert = (req, res, next) => {
|
||||
res.status(200).json({ success: true, msg: "Edit alert" });
|
||||
const editAlert = async (req, res, next) => {
|
||||
try {
|
||||
await editAlertParamValidation(req.params);
|
||||
await editAlertBodyValidation(req.body);
|
||||
res.status(200).json({ success: true, msg: "Edit alert" });
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
};
|
||||
const deleteAlert = (req, res, next) => {
|
||||
res.status(200).json({ success: true, msg: "Delete alert" });
|
||||
const deleteAlert = async (req, res, next) => {
|
||||
try {
|
||||
await deleteAlertParamValidation(req.params);
|
||||
res.status(200).json({ success: true, msg: "Delete alert" });
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.message = error.details[0].message;
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in new issue
Block a user