routes: backend endpoint

This commit is contained in:
muerwre 2018-12-12 17:32:08 +07:00
parent d11dd9043c
commit c69da00b2a
10 changed files with 106 additions and 15 deletions

View file

@ -1,10 +1,12 @@
const express = require('express');
const post = require('./route/post');
const get = require('./route/get');
const list = require('./route/list');
const router = express.Router();
router.post('/', post);
router.get('/', get);
router.get('/list', list);
module.exports = router;

View file

@ -0,0 +1,50 @@
const { Route, User } = require('../../models');
module.exports = async (req, res) => {
const {
query: {
id, token, title, distance, author, starred,
}
} = req;
const user = await User.findOne({ _id: id, token });
let criteria = {};
if (title) {
criteria = {
...criteria,
$or: [
{ title: new RegExp(title, 'ig') },
{ _id: new RegExp(title, 'ig') },
],
};
}
if (!author || !user || (user._id !== author)) {
criteria = {
...criteria,
public: true,
};
}
const list = await Route.find({
...criteria,
distance: {
$gte: parseInt(distance[0], 10),
$lte: parseInt(distance[1], 10),
},
public: true,
}, '_id title distance owner updated_at', { limit: 500 }).populate('owner');
const filtered = list.filter(item => (
!author || item.owner._id === author
));
res.send({
success: true,
list: filtered,
});
};