Added route and DB scaffolds
This commit is contained in:
1 parent
b945c73c87
commit
1e398d9153
4 files changed
+137
No files matched your search
@@ -0,0 +1,37 @@
|
|||||||
|
var jwt = require("jsonwebtoken");
|
||||||
|
const SERVICE_NAME = "check";
|
||||||
|
|
||||||
|
const createCheck = (req, res, next) => {
|
||||||
|
try {
|
||||||
|
req.db.createCheck(req, res);
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getChecks = (req, res, next) => {
|
||||||
|
try {
|
||||||
|
req.db.getChecks(req, res);
|
||||||
|
// Return all checks for a monitor
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteChecks = (req, res, next) => {
|
||||||
|
try {
|
||||||
|
req.db.deleteChecks(req, res);
|
||||||
|
// Delete all checks for a monitor
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createCheck,
|
||||||
|
getChecks,
|
||||||
|
deleteChecks,
|
||||||
|
};
|
||||||
@@ -16,6 +16,10 @@ const connect = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Users
|
||||||
|
//****************************************
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a User
|
* Insert a User
|
||||||
* @async
|
* @async
|
||||||
@@ -55,6 +59,10 @@ const getUserByEmail = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Monitors
|
||||||
|
//****************************************
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all monitors
|
* Get all monitors
|
||||||
* @async
|
* @async
|
||||||
@@ -169,6 +177,69 @@ const editMonitor = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Checks
|
||||||
|
//****************************************
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a check for a monitor
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const createCheck = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// Create check with monitor id
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Create check", data: req.params.monitorId });
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all checks for a monitor
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const getChecks = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// TODO get all checks for a monitor from DB
|
||||||
|
res
|
||||||
|
.status(200)
|
||||||
|
.json({ success: true, msg: "Get checks", data: req.params.monitorId });
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete all checks for a monitor
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const deleteChecks = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// TODO Delete all checks for a monitor
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Deleted checks",
|
||||||
|
data: req.params.monitorId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -179,4 +250,7 @@ module.exports = {
|
|||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
editMonitor,
|
editMonitor,
|
||||||
|
createCheck,
|
||||||
|
getChecks,
|
||||||
|
deleteChecks,
|
||||||
};
|
};
|
||||||
@@ -3,6 +3,7 @@ const helmet = require("helmet");
|
|||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const authRouter = require("./routes/authRoute");
|
const authRouter = require("./routes/authRoute");
|
||||||
const monitorRouter = require("./routes/monitorRoute");
|
const monitorRouter = require("./routes/monitorRoute");
|
||||||
|
const checkRouter = require("./routes/checkRoute");
|
||||||
const { connectDbAndRunServer } = require("./configs/db");
|
const { connectDbAndRunServer } = require("./configs/db");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const logger = require("./utils/logger");
|
const logger = require("./utils/logger");
|
||||||
@@ -62,6 +63,7 @@ app.use((req, res, next) => {
|
|||||||
//routes
|
//routes
|
||||||
app.use("/api/v1/auth", authRouter);
|
app.use("/api/v1/auth", authRouter);
|
||||||
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
||||||
|
app.use("/api/v1/checks", verifyJWT, checkRouter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error handler middleware
|
* Error handler middleware
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
const router = require("express").Router();
|
||||||
|
const checkController = require("../controllers/checkController");
|
||||||
|
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||||
|
const Monitor = require("../models/Monitor");
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
"/:monitorId",
|
||||||
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
checkController.createCheck
|
||||||
|
);
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
"/:monitorId",
|
||||||
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
checkController.getChecks
|
||||||
|
);
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
"/delete/:monitorId",
|
||||||
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
checkController.deleteChecks
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
Reference in new issue
Block a user