---|main| Initial commit
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Jump = mongoose.model('Jump'),
|
||||
User = mongoose.model('User');
|
||||
const auth = require('../auth');
|
||||
|
||||
// Preload jump objects on routes with ':jump'
|
||||
router.param('jump', function (req, res, next, slug) {
|
||||
Jump.findOne({ slug: slug })
|
||||
.populate('author')
|
||||
.then(function (jump) {
|
||||
if (!jump) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
req.jump = jump;
|
||||
return next();
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
router.get('/', auth.required, function (req, res, next) {
|
||||
var query = {};
|
||||
var limit = 20;
|
||||
var offset = 0;
|
||||
if (typeof req.query.limit !== 'undefined') {
|
||||
limit = req.query.limit;
|
||||
}
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
if (typeof req.query.title !== 'undefined') {
|
||||
const rgx = new RegExp(`.*${req.query.title}.*`);
|
||||
query = {
|
||||
$or: [
|
||||
{ title: { $regex: rgx, $options: "i" } },
|
||||
{ slug: { $regex: rgx, $options: "i" } },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
req.query.author ? User.findOne({ username: req.query.author }) : null,
|
||||
req.query.favorited ? User.findOne({ username: req.query.favorited }) : null
|
||||
]).then(function (users) {
|
||||
var author = users[0];
|
||||
|
||||
if (author) {
|
||||
query.author = author._id;
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
Jump.find(query)
|
||||
.limit(Number(limit))
|
||||
.skip(Number(offset))
|
||||
.sort({ numero: 'asc' })
|
||||
.populate('author')
|
||||
.exec(),
|
||||
Jump.count(query).exec(),
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
var jumps = results[0];
|
||||
var jumpsCount = results[1];
|
||||
var user = results[2];
|
||||
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(user);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
router.get('/feed', auth.required, function (req, res, next) {
|
||||
var limit = 20;
|
||||
var offset = 0;
|
||||
if (typeof req.query.limit !== 'undefined') {
|
||||
limit = req.query.limit;
|
||||
}
|
||||
if (typeof req.query.offset !== 'undefined') {
|
||||
offset = req.query.offset;
|
||||
}
|
||||
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
var query = { author: { $in: user.following } };
|
||||
Promise.all([
|
||||
Jump.find(query)
|
||||
.limit(Number(limit))
|
||||
.skip(Number(offset))
|
||||
.populate('author')
|
||||
.exec(),
|
||||
Jump.count(query)
|
||||
]).then(function (results) {
|
||||
var jumps = results[0];
|
||||
var jumpsCount = results[1];
|
||||
return res.json({
|
||||
jumps: jumps.map(function (jump) {
|
||||
return jump.toJSONFor(user);
|
||||
}),
|
||||
jumpsCount: jumpsCount
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
/*Jump
|
||||
.find({}).select('numero')
|
||||
.sort({"numero" : -1}).limit(1)
|
||||
.exec()*/
|
||||
]).then(function (results) {
|
||||
var user = results[0];
|
||||
//var numero = (results[1][0].numero + 1);
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
var jump = new Jump(req.body.jump);
|
||||
jump.author = user;
|
||||
//jump.numero = numero;
|
||||
return jump.save().then(function () {
|
||||
return res.json({ jump: jump.toJSONFor(user) });
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
// return a jump
|
||||
router.get('/:jump', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
/*req.jump.populate('author')*/
|
||||
]).then(function (results) {
|
||||
var user = results[0];
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
return res.json({ jump: req.jump.toJSONFor(user) });
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
router.put('/:jump', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
var user = results[0];
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
if (req.jump.author._id.toString() !== req.payload.id.toString()) {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
if (typeof req.body.jump.date !== 'undefined') {
|
||||
req.jump.date = req.body.jump.date;
|
||||
}
|
||||
if (typeof req.body.jump.numero !== 'undefined') {
|
||||
req.jump.numero = req.body.jump.numero;
|
||||
}
|
||||
if (typeof req.body.jump.lieu !== 'undefined') {
|
||||
req.jump.lieu = req.body.jump.lieu;
|
||||
}
|
||||
if (typeof req.body.jump.oaci !== 'undefined') {
|
||||
req.jump.oaci = req.body.jump.oaci;
|
||||
}
|
||||
if (typeof req.body.jump.aeronef !== 'undefined') {
|
||||
req.jump.aeronef = req.body.jump.aeronef;
|
||||
}
|
||||
if (typeof req.body.jump.imat !== 'undefined') {
|
||||
req.jump.imat = req.body.jump.imat;
|
||||
}
|
||||
if (typeof req.body.jump.hauteur !== 'undefined') {
|
||||
req.jump.hauteur = req.body.jump.hauteur;
|
||||
}
|
||||
if (typeof req.body.jump.voile !== 'undefined') {
|
||||
req.jump.voile = req.body.jump.voile;
|
||||
}
|
||||
if (typeof req.body.jump.taille !== 'undefined') {
|
||||
req.jump.taille = req.body.jump.taille;
|
||||
}
|
||||
if (typeof req.body.jump.categorie !== 'undefined') {
|
||||
req.jump.categorie = req.body.jump.categorie;
|
||||
}
|
||||
if (typeof req.body.jump.module !== 'undefined') {
|
||||
req.jump.module = req.body.jump.module;
|
||||
}
|
||||
if (typeof req.body.jump.participants !== 'undefined') {
|
||||
req.jump.participants = req.body.jump.participants;
|
||||
}
|
||||
if (typeof req.body.jump.sautants !== 'undefined') {
|
||||
req.jump.sautants = req.body.jump.sautants;
|
||||
}
|
||||
if (typeof req.body.jump.programme !== 'undefined') {
|
||||
req.jump.programme = req.body.jump.programme;
|
||||
}
|
||||
if (typeof req.body.jump.accessoires !== 'undefined') {
|
||||
req.jump.accessoires = req.body.jump.accessoires;
|
||||
}
|
||||
if (typeof req.body.jump.zone !== 'undefined') {
|
||||
req.jump.zone = req.body.jump.zone;
|
||||
}
|
||||
if (typeof req.body.jump.dossier !== 'undefined') {
|
||||
req.jump.dossier = req.body.jump.dossier;
|
||||
}
|
||||
if (typeof req.body.jump.video !== 'undefined') {
|
||||
req.jump.video = req.body.jump.video;
|
||||
}
|
||||
|
||||
return req.jump.save().then(function (jump) {
|
||||
return res.json({ jump: jump.toJSONFor(user) });
|
||||
});
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
// delete jump
|
||||
router.delete('/:jump', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
if (req.jump.author._id.toString() === req.payload.id.toString()) {
|
||||
return req.jump.remove().then(function () {
|
||||
return res.sendStatus(204);
|
||||
});
|
||||
} else {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user