Transfering limit to get monitors by Id. Plus, readme file changes

This commit is contained in:
MuhammadKhalilzadeh
2024-07-11 10:20:39 +03:30
parent e7ea80f820
commit 6b8af0f669
3 changed files with 22 additions and 15 deletions
+3 -3
View File
@@ -48,7 +48,7 @@ You can see the designs [here](https://www.figma.com/design/RPSfaw66HjzSwzntKcgD
###### Monitors
- <code>GET</code> [/api/v1/monitors](#get-monitors)
- <code>GET</code> [/api/v1/monitor/{id}](#get-monitor-id)
- <code>GET</code> [/api/v1/monitors/user/{userId}](#get-monitors-user-userid)
- <code>GET</code> [/api/v1/monitors/user/{userId}?limit](#get-monitors-user-userid)
- <code>POST</code> [/api/v1/monitors](#post-monitors)
- <code>POST</code> [/api/v1/monitors/delete/{monitorId}](#post-monitors-del-id)
- <code>POST</code> [/api/v1/monitors/edit/{monitorId}](#post-monitors-edit-id)
@@ -749,7 +749,7 @@ curl --request GET \
</details>
<details>
<summary id='get-monitors-user-userid'><code>GET</code> <b>/api/v1/monitors/user/{userId}</b></summary>
<summary id='get-monitors-user-userid'><code>GET</code> <b>/api/v1/monitors/user/{userId}?limit</b></summary>
###### 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 '<bearer_token>' \
```
+18 -11
View File
@@ -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;
+1 -1
View File
@@ -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(