diff --git a/README.md b/README.md index 84874ea40..a11d6f770 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ You can see the designs [here](https://www.figma.com/design/RPSfaw66HjzSwzntKcgD ###### Monitors - GET [/api/v1/monitors](#get-monitors) - GET [/api/v1/monitor/{id}](#get-monitor-id) - - GET [/api/v1/monitors/user/{userId}](#get-monitors-user-userid) + - GET [/api/v1/monitors/user/{userId}?limit](#get-monitors-user-userid) - POST [/api/v1/monitors](#post-monitors) - POST [/api/v1/monitors/delete/{monitorId}](#post-monitors-del-id) - POST [/api/v1/monitors/edit/{monitorId}](#post-monitors-edit-id) @@ -749,7 +749,7 @@ curl --request GET \
-GET /api/v1/monitors/user/{userId} +GET /api/v1/monitors/user/{userId}?limit ###### Method/Headers @@ -767,7 +767,7 @@ curl --request GET \ ``` curl --request GET \ - --url http://localhost:5000/api/v1/monitors/user/6645079aae0b439371913972 \ + --url http://localhost:5000/api/v1/monitors/user/6645079aae0b439371913972?limit=25 \ --header '' \ ``` diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 75fd2310a..0ca7d0d65 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -270,15 +270,26 @@ const getMonitorById = async (req, res) => { */ const getMonitorsByUserId = async (req, res) => { try { + const limit = req.body.limit; const monitors = await Monitor.find({ userId: req.params.userId }); // Map each monitor to include its associated checks const monitorsWithChecks = await Promise.all( monitors.map(async (monitor) => { - // Checks are order oldest -> newest - const checks = await Check.find({ monitorId: monitor._id }).sort({ - createdAt: 1, - }); - return { ...monitor.toObject(), checks }; + + if(limit) { + // Checks are order oldest -> newest + const checks = await Check.find({ monitorId: monitor._id }).sort({ + createdAt: 1, + }).limit(limit);; + return { ...monitor.toObject(), checks }; + + } else { + // Checks are order oldest -> newest + const checks = await Check.find({ monitorId: monitor._id }).sort({ + createdAt: 1, + }); + return { ...monitor.toObject(), checks }; + } }) ); @@ -414,13 +425,9 @@ const createCheck = async (checkData) => { * @throws {Error} */ -const getChecks = async (monitorId, limit) => { +const getChecks = async (monitorId) => { try { - if (limit) { - const checks = await Check.find({ monitorId }).sort({ createdAt: -1 }).limit(limit); - return checks; - } - const checks = await Check.find({ monitorId }) + const checks = await Check.find({ monitorId }); return checks; } catch (error) { throw error; diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index ae7a5852e..d19aedad9 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -5,7 +5,7 @@ const Monitor = require("../models/Monitor"); router.get("/", monitorController.getAllMonitors); router.get("/:monitorId", monitorController.getMonitorById); -router.get("/user/:userId", monitorController.getMonitorsByUserId); +router.get("/user/:userId?limit", monitorController.getMonitorsByUserId); router.post("/", monitorController.createMonitor); router.post(