Skip to content

Commit

Permalink
Added near endpoint
Browse files Browse the repository at this point in the history
- Will get all points between max and min distance from the supplied point for apagones and lugares
  • Loading branch information
Froilan Irizarry committed Apr 14, 2019
1 parent 7ef60aa commit 3768d46
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,28 @@ router.get('/apagon/:id', async (req, res, next) => {
res.json(results);
});

router.patch('/apagon/:id', async (req, res, next) => {
router.get('/apagon/near', async (req, res, next) => {
const { lat, lng, max_distance, min_distance } = req.query;
const dbClient = await getApagonesDBClient();
const results = await dbClient.find(lugaresCollection);

res.json(results);
});

const query = {
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [parseFloat(lng), parseFloat(lat)]
},
$maxDistance: parseFloat(max_distance),
$minDistance: parseFloat(min_distance)
}
}
};
try {
res.json(await dbClient.find(apagonesCollection, query));
}catch(error) {
throw error;
}
})
// API routes
router.get('/lugar', async (req, res, next) => {
const dbClient = await getApagonesDBClient();
Expand All @@ -59,6 +74,29 @@ router.get('/lugar', async (req, res, next) => {
res.json({ data: results });
});

router.get('/lugar/near', async (req, res, next) => {
const { lat, lng, max_distance, min_distance } = req.query;
const dbClient = await getApagonesDBClient();

const query = {
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [parseFloat(lng), parseFloat(lat)]
},
$maxDistance: parseFloat(max_distance),
$minDistance: parseFloat(min_distance)
}
}
};
try {
res.json(await dbClient.find(lugaresCollection, query));
}catch(error) {
throw error;
}
});


router.post('/lugar', async (req, res, next) => {
const dbClient = await getApagonesDBClient();
Expand All @@ -73,5 +111,6 @@ router.post('/lugar', async (req, res, next) => {
}
});


module.exports = router;

0 comments on commit 3768d46

Please sign in to comment.