From def8ba4b9df81f6dd6854491e5b0ff14a1e5d40a Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 13:36:04 -0700 Subject: [PATCH 1/8] Added monitor router, controller, and model --- Server/controllers/monitorController.js | 19 ++++++++++ Server/index.js | 46 ++++++++++++++----------- Server/models/monitor.js | 37 ++++++++++++++++++++ Server/routes/monitorRoute.js | 8 +++++ Server/validation/joi.js | 12 ++++--- 5 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 Server/controllers/monitorController.js create mode 100644 Server/models/monitor.js create mode 100644 Server/routes/monitorRoute.js diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js new file mode 100644 index 000000000..1044d3f48 --- /dev/null +++ b/Server/controllers/monitorController.js @@ -0,0 +1,19 @@ +const { getMonitorsByIdValidation } = require("../validation/joi"); + +const getAllMonitors = async (req, res) => { + res.json({ message: "Hello from Monitor Controller" }); +}; + +const getMonitorById = async (req, res) => { + const { error } = getMonitorsByIdValidation.validate(req.params); + if (error) { + return res + .status(400) + .json({ success: false, msg: error.details[0].message }); + } + + const monitorId = req.params.monitorId; + return res.json({ success: true, msg: "Monitor found", data: { monitorId } }); +}; + +module.exports = { getAllMonitors, getMonitorById }; diff --git a/Server/index.js b/Server/index.js index 173c2c78d..3913d1a58 100644 --- a/Server/index.js +++ b/Server/index.js @@ -1,28 +1,32 @@ -const express = require('express') -const helmet = require('helmet') -const cors = require('cors') -const authRouter = require('./routes/authRoute') -const { connectDbAndRunServer } = require('./configs/db') -require('dotenv').config() +const express = require("express"); +const helmet = require("helmet"); +const cors = require("cors"); +const authRouter = require("./routes/authRoute"); +const monitorRouter = require("./routes/monitorRoute"); +const { connectDbAndRunServer } = require("./configs/db"); +require("dotenv").config(); // const { sendEmail } = require('./utils/sendEmail') /** * NOTES - * Email Service will be added + * Email Service will be added * Logger Service will be added (Winston or similar) */ -const app = express() +const app = express(); // middlewares -app.use(cors( - //We will add configuration later -)) -app.use(express.json()) -app.use(helmet()) +app.use( + cors() + //We will add configuration later +); +app.use(express.json()); +app.use(helmet()); //routes -app.use('/api/v1/auth', authRouter); +app.use("/api/v1/auth", authRouter); + +app.use("/api/v1/monitors", monitorRouter); // Testing email service // app.use('/sendEmail', async (req, res) => { @@ -31,12 +35,12 @@ app.use('/api/v1/auth', authRouter); // }) //health check -app.use('/api/v1/healthy', (req, res) => { - try { - return res.status(200).json({message:"Healthy"}) - } catch (error) { - return res.status(500).json({message:error.message}) - } -}) +app.use("/api/v1/healthy", (req, res) => { + try { + return res.status(200).json({ message: "Healthy" }); + } catch (error) { + return res.status(500).json({ message: error.message }); + } +}); connectDbAndRunServer(app); diff --git a/Server/models/monitor.js b/Server/models/monitor.js new file mode 100644 index 000000000..513d4e2be --- /dev/null +++ b/Server/models/monitor.js @@ -0,0 +1,37 @@ +const mongoose = require("mongoose"); + +const MonitorSchema = mongoose.Schema({ + userId: { + type: String, + }, + name: { + type: String, + required: true, + }, + description: { + type: String, + }, + url: { + type: String, + required: true, + }, + isActive: { + type: Boolean, + default: true, + }, + interval: { + // in milliseconds + type: Number, + default: 60000, + }, + updated_at: { + type: Date, + default: Date.now, + }, + created_at: { + type: Date, + default: Date.now, + }, +}); + +module.exports = mongoose.model("Monitor", MonitorSchema); diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js new file mode 100644 index 000000000..724ee79b6 --- /dev/null +++ b/Server/routes/monitorRoute.js @@ -0,0 +1,8 @@ +const router = require("express").Router(); +const monitorController = require("../controllers/monitorController"); +const { getMonitorsByIdValidation } = require("../validation/joi"); + +router.get("/", monitorController.getAllMonitors); +router.get("/:monitorId", monitorController.getMonitorById); + +module.exports = router; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index d28f9b7da..c5b2b58d1 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -1,10 +1,12 @@ -const joi = require('joi') - +const joi = require("joi"); const authValidation = joi.object({ - email: joi.string().email().required(), - password: joi.string().min(8).required(), + email: joi.string().email().required(), + password: joi.string().min(8).required(), }); +const getMonitorsByIdValidation = joi.object({ + monitorId: joi.string().required(), +}); -module.exports = {authValidation} \ No newline at end of file +module.exports = { authValidation, getMonitorsByIdValidation }; From 2e2d91937e533b1e0026324dc019018032227f9f Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 13:59:33 -0700 Subject: [PATCH 2/8] Added fake DB for monitor route testing --- Server/controllers/monitorController.js | 48 +++++++++++++++++++++--- Server/db/FakeDb.js | 46 +++++++++++++++++++++++ Server/models/{monitor.js => Monitor.js} | 0 Server/routes/monitorRoute.js | 2 +- Server/validation/joi.js | 11 +++++- 5 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 Server/db/FakeDb.js rename Server/models/{monitor.js => Monitor.js} (100%) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 1044d3f48..f7a85ce4c 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -1,9 +1,20 @@ -const { getMonitorsByIdValidation } = require("../validation/joi"); +const { + getMonitorsByIdValidation, + getMonitorsByUserIdValidation, +} = require("../validation/joi"); +const db = require("../db/FakeDb"); +// Gets all monitors const getAllMonitors = async (req, res) => { - res.json({ message: "Hello from Monitor Controller" }); + try { + const monitors = await db.getAllMonitors(); + return res.json({ success: true, msg: "Monitors found", data: monitors }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } }; +// Get a monitor by ID const getMonitorById = async (req, res) => { const { error } = getMonitorsByIdValidation.validate(req.params); if (error) { @@ -12,8 +23,35 @@ const getMonitorById = async (req, res) => { .json({ success: false, msg: error.details[0].message }); } - const monitorId = req.params.monitorId; - return res.json({ success: true, msg: "Monitor found", data: { monitorId } }); + try { + const monitorId = req.params.monitorId; + const monitor = await db.getMonitorById(monitorId); + return res.json({ success: true, msg: "Monitor found", data: monitor }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } }; -module.exports = { getAllMonitors, getMonitorById }; +// Gets a monitor by user ID +const getMonitorsByUserId = async (req, res) => { + const { error } = getMonitorsByUserIdValidation.validate(req.params); + if (error) { + return res + .status(400) + .json({ success: false, msg: error.details[0].message }); + } + + try { + const userId = req.params.userId; + const monitors = await db.getMonitorsByUserId(userId); + return res.json({ + success: true, + msg: `Monitors for user ${userId} found`, + data: monitors, + }); + } catch (error) { + return res.status(500).json({ success: false, msg: error.message }); + } +}; + +module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js new file mode 100644 index 000000000..b604a9294 --- /dev/null +++ b/Server/db/FakeDb.js @@ -0,0 +1,46 @@ +const Monitor = require("../models/Monitor"); + +const FAKE_MONITOR_DATA = []; + +for (let i = 0; i < 10; i++) { + FAKE_MONITOR_DATA.push( + new Monitor({ + userId: i % 2 === 0 ? 1 : 2, + name: `Monitor ${i}`, + description: `Description for Monitor ${i}`, + url: `https://monitor${i}.com`, + isActive: true, + interval: 60000, + updated_at: new Date(), + created_at: new Date(), + }) + ); +} + +const getAllMonitors = async () => { + return FAKE_MONITOR_DATA; +}; + +const getMonitorById = async (monitorId) => { + const idx = FAKE_MONITOR_DATA.findIndex((monitor) => { + return monitor.id === monitorId; + }); + if (idx === -1) { + throw new Error(`Monitor with id ${monitorId} not found`); + } + return FAKE_MONITOR_DATA[idx]; +}; + +const getMonitorsByUserId = async (userId) => { + const userMonitors = FAKE_MONITOR_DATA.filter((monitor) => { + return monitor.userId === userId; + }); + + if (userMonitors.length === 0) { + throw new Error(`Monitors for user ${userId} not found`); + } + + return userMonitors; +}; + +module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; diff --git a/Server/models/monitor.js b/Server/models/Monitor.js similarity index 100% rename from Server/models/monitor.js rename to Server/models/Monitor.js diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index 724ee79b6..7c6767364 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -1,8 +1,8 @@ const router = require("express").Router(); const monitorController = require("../controllers/monitorController"); -const { getMonitorsByIdValidation } = require("../validation/joi"); router.get("/", monitorController.getAllMonitors); router.get("/:monitorId", monitorController.getMonitorById); +router.get("/user/:userId", monitorController.getMonitorsByUserId); module.exports = router; diff --git a/Server/validation/joi.js b/Server/validation/joi.js index c5b2b58d1..2fb59a377 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -1,4 +1,5 @@ const joi = require("joi"); +const user = require("../models/user"); const authValidation = joi.object({ email: joi.string().email().required(), @@ -9,4 +10,12 @@ const getMonitorsByIdValidation = joi.object({ monitorId: joi.string().required(), }); -module.exports = { authValidation, getMonitorsByIdValidation }; +const getMonitorsByUserIdValidation = joi.object({ + userId: joi.string().required(), +}); + +module.exports = { + authValidation, + getMonitorsByIdValidation, + getMonitorsByUserIdValidation, +}; From 49a11b5abbe57b4526242f235c02e3f2af18d60e Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 14:03:36 -0700 Subject: [PATCH 3/8] Added some comments RE fake database --- Server/controllers/monitorController.js | 2 ++ Server/db/FakeDb.js | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index f7a85ce4c..deaf2d8ba 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -2,6 +2,8 @@ const { getMonitorsByIdValidation, getMonitorsByUserIdValidation, } = require("../validation/joi"); + +// Fake database, if we have standardized DB methods we can swap out databases easily const db = require("../db/FakeDb"); // Gets all monitors diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js index b604a9294..8eed7779c 100644 --- a/Server/db/FakeDb.js +++ b/Server/db/FakeDb.js @@ -1,3 +1,9 @@ +// ************************** +// FakeDB is used to simulate the database +// If we standarize the DB methods we can swap out databases easily +// Too bad JS doesn't have interfaces, that would be nice. TypeScript anyone? +// ************************** + const Monitor = require("../models/Monitor"); const FAKE_MONITOR_DATA = []; From 7d6d2d341b1edfd313e6cf1c97cac8d0b9b68143 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 14:04:13 -0700 Subject: [PATCH 4/8] Added one last comment --- Server/db/FakeDb.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js index 8eed7779c..d3b793f11 100644 --- a/Server/db/FakeDb.js +++ b/Server/db/FakeDb.js @@ -49,4 +49,5 @@ const getMonitorsByUserId = async (userId) => { return userMonitors; }; +// Conisder standardizing these methods so we can swap databases easily module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; From 8d6529d497c481935417d9dc187d03291d334f33 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 10 May 2024 14:34:04 -0700 Subject: [PATCH 5/8] Added some endpoint info to readme --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/README.md b/README.md index 19a0670e1..4b5375c8f 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,103 @@ BlueWave uptime monitoring application 1. Change directory to the `Server` directory 2. Install all depencies by running `npm install` +--- + +#### Configuration + +Configure the server with the following environmental variables + +| ENV Varialbe Name | Required/Optional | Type | Description | +| -------------------- | ----------------- | ------ | --------------------------------- | +| DB_CONNECTION_STRING | Required | string | Specfies URL for MongoDB Database | + +--- + #### Starting the Development Server 1. run `npm run dev` to start the development server +--- + +#### Endpoints + +All endpoints return a response in this format: + +| Name | Type | Notes | +| ------- | --------- | ----------------------------- | +| success | `boolean` | Success or failure of request | +| msg | `string` | Message describing response | +| data | `Object` | Arbitrary Payload | + +Example: + +``` +{success: true, msg: "Successful Request", data: {test: testData}} +``` + +##### Data Types + +###### Monitor + +| Name | Type | Notes | +| ----------- | --------- | ---------------------------------------- | +| userId | `string` | Unique ID identifying monitor creator | +| name | `string` | Name of the monitor | +| description | `string` | Description of the monitor | +| url | `string` | Url the monitor will ping | +| isActive | `boolean` | Whether or not the monitor is active | +| interval | `integer` | Interval with which to ping monitor (ms) | +| updatedAt | `Date` | Last time the monitor was updated | +| CreatedAt | `Date` | When the monitor was updated | + +--- + +##### GET /api/v1/monitors + +###### Response + +| Status Code | Type | Description | +| ----------- | --------------------- | -------------------------------- | +| 200 | Response with Payload | Response with a list of monitors | + +###### Payload + +| Type | Notes | +| ---------------- | ----------------- | +| `Array[Monitor]` | Array of monitors | + +--- + +##### GET /api/v1/monitor/:id + +###### Response + +| Status Code | Type | Description | +| ----------- | --------------------- | ------------------------------ | +| 200 | Response with Payload | Response with a single monitor | + +###### Payload + +| Type | Notes | +| --------- | --------------------------------------------------- | +| `Monitor` | Single monitor with the id in the request parameter | + +--- + +##### GET /api/v1/monitors/user/:userId + +| Status Code | Type | Description | +| ----------- | --------------------- | -------------------------------- | +| 200 | Response with Payload | Response with a list of monitors | + +###### Payload + +| Type | Notes | +| ---------------- | ------------------------------------------------------------------ | +| `Array[Monitor]` | Array of monitors created by user with userId specified in request | + ## Contributors + From 8cc92e6c40d44f73a6bb92b5492707f5a34c5921 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 13 May 2024 14:21:27 -0700 Subject: [PATCH 6/8] Implemented MongoDB example db, updated documentation --- Server/configs/db.js | 29 ++++----- Server/controllers/monitorController.js | 9 +-- Server/db/FakeDb.js | 38 ++++++++++-- Server/db/MongoDB.js | 80 +++++++++++++++++++++++++ Server/index.js | 27 ++++++++- Server/routes/monitorRoute.js | 1 - 6 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 Server/db/MongoDB.js diff --git a/Server/configs/db.js b/Server/configs/db.js index 174978ca3..ba3c8b660 100644 --- a/Server/configs/db.js +++ b/Server/configs/db.js @@ -1,18 +1,15 @@ -const mongoose = require('mongoose') +const PORT = process.env.PORT || 5000; -const PORT = process.env.PORT || 5000 +const connectDbAndRunServer = async (app, db) => { + try { + await db.connect(); + app.listen(PORT, () => { + console.log(`server started on port:${PORT}`); + }); + } catch (error) { + console.log("Failed to connect to DB"); + console.error(error); + } +}; -const connectDbAndRunServer = async (app) => { - await mongoose.connect(process.env.DB_CONNECTION_STRING).then(() => { - console.log("DB connected") - // run server - app.listen(PORT, () => { - console.log(`server started on port:${PORT}`) - }) - }).catch((err) => { - console.log(err) - }) -} - - -module.exports = { connectDbAndRunServer } \ No newline at end of file +module.exports = { connectDbAndRunServer }; diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index deaf2d8ba..dda18d8ff 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -3,13 +3,10 @@ const { getMonitorsByUserIdValidation, } = require("../validation/joi"); -// Fake database, if we have standardized DB methods we can swap out databases easily -const db = require("../db/FakeDb"); - // Gets all monitors const getAllMonitors = async (req, res) => { try { - const monitors = await db.getAllMonitors(); + const monitors = await req.db.getAllMonitors(); return res.json({ success: true, msg: "Monitors found", data: monitors }); } catch (error) { return res.status(500).json({ success: false, msg: error.message }); @@ -27,7 +24,7 @@ const getMonitorById = async (req, res) => { try { const monitorId = req.params.monitorId; - const monitor = await db.getMonitorById(monitorId); + const monitor = await req.db.getMonitorById(monitorId); return res.json({ success: true, msg: "Monitor found", data: monitor }); } catch (error) { return res.status(500).json({ success: false, msg: error.message }); @@ -45,7 +42,7 @@ const getMonitorsByUserId = async (req, res) => { try { const userId = req.params.userId; - const monitors = await db.getMonitorsByUserId(userId); + const monitors = await req.db.getMonitorsByUserId(userId); return res.json({ success: true, msg: `Monitors for user ${userId} found`, diff --git a/Server/db/FakeDb.js b/Server/db/FakeDb.js index d3b793f11..6dfa89ac9 100644 --- a/Server/db/FakeDb.js +++ b/Server/db/FakeDb.js @@ -1,7 +1,23 @@ // ************************** -// FakeDB is used to simulate the database -// If we standarize the DB methods we can swap out databases easily -// Too bad JS doesn't have interfaces, that would be nice. TypeScript anyone? +// The idea here is to provide a layer of abstraction between the database and whoever is using it. +// Instead of directly calling mongoose methods, we can call the methods on the DB object. +// If this were Typescript or Java or Golang an interface would be implemented to ensure the methods are available. +// But we do the best we can with Javascript. +// +// If the methods are consistent all we have to do to swap out one DB for another is simply change the import. +// +// Example: +// We start with the fake DB: +// +// const db = require("../db/FakeDb"); +// const monitors = await db.getAllMonitors(); +// +// And when we want to swtich to a real DB, all we have to do is swap the import +// +// const db = require("../db/MongoDb"); +// const monitors = await db.getAllMonitors(); +// +// The rest of the code is the same, as all the `db` methods are standardized. // ************************** const Monitor = require("../models/Monitor"); @@ -23,6 +39,14 @@ for (let i = 0; i < 10; i++) { ); } +const connect = async () => { + try { + await console.log("Connected to FakeDB"); + } catch (error) { + console.error(error); + } +}; + const getAllMonitors = async () => { return FAKE_MONITOR_DATA; }; @@ -49,5 +73,9 @@ const getMonitorsByUserId = async (userId) => { return userMonitors; }; -// Conisder standardizing these methods so we can swap databases easily -module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId }; +module.exports = { + connect, + getAllMonitors, + getMonitorById, + getMonitorsByUserId, +}; diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js new file mode 100644 index 000000000..3c48a448d --- /dev/null +++ b/Server/db/MongoDB.js @@ -0,0 +1,80 @@ +const Monitor = require("../models/Monitor"); +const mongoose = require("mongoose"); + +const seedDatabase = async () => { + const FAKE_MONITOR_DATA = []; + + for (let i = 0; i < 10; i++) { + FAKE_MONITOR_DATA.push( + new Monitor({ + userId: i % 2 === 0 ? 1 : 2, + name: `Monitor ${i}`, + description: `Description for Monitor ${i}`, + url: `https://monitor${i}.com`, + isActive: true, + interval: 60000, + updated_at: new Date(), + created_at: new Date(), + }) + ); + } + + for (const monitor of FAKE_MONITOR_DATA) { + await monitor.save(); + } +}; + +const connect = async () => { + try { + await mongoose.connect(process.env.DB_CONNECTION_STRING); + console.log("Connected to MongoDB"); + + const monitors = await Monitor.find(); + if (monitors.length === 0) { + console.log("Seeding database with fake data"); + await seedDatabase(); + } else { + console.log("Database already seeded"); + } + } catch (error) { + console.error("Failed to connect to MongoDB"); + throw error; + } +}; + +// Gets all monitors +const getAllMonitors = async (req, res) => { + try { + const monitors = await Monitor.find(); + return monitors; + } catch (error) { + throw error; + } +}; + +// Get a monitor by ID +const getMonitorById = async (req, res) => { + try { + const monitor = await Monitor.findById(req.params.monitorId); + return monitor; + } catch (error) { + throw error; + } +}; + +// Gets a monitor by user ID +const getMonitorsByUserId = async (req, res) => { + try { + const monitors = await Monitor.find({ userId: req.params.userId }); + return monitors; + } catch (error) { + throw error; + } +}; + +module.exports = { + connect, + getAllMonitors, + getMonitorById, + getMonitorsByUserId, +}; diff --git a/Server/index.js b/Server/index.js index 3913d1a58..c8136c2d0 100644 --- a/Server/index.js +++ b/Server/index.js @@ -3,10 +3,23 @@ const helmet = require("helmet"); const cors = require("cors"); const authRouter = require("./routes/authRoute"); const monitorRouter = require("./routes/monitorRoute"); -const { connectDbAndRunServer } = require("./configs/db"); +const { connectDbAndRunServer, connectViaInterface } = require("./configs/db"); require("dotenv").config(); // const { sendEmail } = require('./utils/sendEmail') +// ************************** +// Here is where we can swap out DBs easily. Spin up a mongoDB instance and try it out. +// Simply comment out the FakeDB and uncomment the MongoDB or vice versa. +// We can easily swap between any type of data source as long as the methods are implemented +// +// FakeDB +// const db = require("./db/FakeDb"); +// +// MongoDB +const db = require("./db/MongoDB"); +// +// ************************** + /** * NOTES * Email Service will be added @@ -23,6 +36,16 @@ app.use( app.use(express.json()); app.use(helmet()); +// ************************** +// Make DB accessible anywhere we have a Request object +// By adding the DB to the request object, we can access it in any route +// Thus we do not need to import it in every route file, and we can easily swap out DBs as there is only one place to change it +// ************************** +app.use((req, res, next) => { + req.db = db; + next(); +}); + //routes app.use("/api/v1/auth", authRouter); @@ -43,4 +66,4 @@ app.use("/api/v1/healthy", (req, res) => { } }); -connectDbAndRunServer(app); +connectDbAndRunServer(app, db); diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index 7c6767364..e12982905 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -1,6 +1,5 @@ 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); From 7957612a85a59229702e9e9652e7db895b30c1dc Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 13 May 2024 14:23:03 -0700 Subject: [PATCH 7/8] Removed unused method --- Server/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/index.js b/Server/index.js index c8136c2d0..ef8cdc982 100644 --- a/Server/index.js +++ b/Server/index.js @@ -3,7 +3,7 @@ const helmet = require("helmet"); const cors = require("cors"); const authRouter = require("./routes/authRoute"); const monitorRouter = require("./routes/monitorRoute"); -const { connectDbAndRunServer, connectViaInterface } = require("./configs/db"); +const { connectDbAndRunServer } = require("./configs/db"); require("dotenv").config(); // const { sendEmail } = require('./utils/sendEmail') From 4e12778f001b0e809228fe11a74a7e82ae6aba1c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 13 May 2024 15:22:35 -0700 Subject: [PATCH 8/8] Remove seed data, not required --- Server/db/MongoDB.js | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 3c48a448d..6ad783877 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -1,41 +1,10 @@ const Monitor = require("../models/Monitor"); const mongoose = require("mongoose"); -const seedDatabase = async () => { - const FAKE_MONITOR_DATA = []; - - for (let i = 0; i < 10; i++) { - FAKE_MONITOR_DATA.push( - new Monitor({ - userId: i % 2 === 0 ? 1 : 2, - name: `Monitor ${i}`, - description: `Description for Monitor ${i}`, - url: `https://monitor${i}.com`, - isActive: true, - interval: 60000, - updated_at: new Date(), - created_at: new Date(), - }) - ); - } - - for (const monitor of FAKE_MONITOR_DATA) { - await monitor.save(); - } -}; - const connect = async () => { try { await mongoose.connect(process.env.DB_CONNECTION_STRING); console.log("Connected to MongoDB"); - - const monitors = await Monitor.find(); - if (monitors.length === 0) { - console.log("Seeding database with fake data"); - await seedDatabase(); - } else { - console.log("Database already seeded"); - } } catch (error) { console.error("Failed to connect to MongoDB"); throw error;