Mise à jour de routes

This commit is contained in:
Julien Gautier
2024-01-11 16:53:43 +01:00
parent 54524591dd
commit e1a0ba340c
6 changed files with 503 additions and 43 deletions
+133 -21
View File
@@ -17,9 +17,12 @@ router.param('jump', function (req, res, next, slug) {
}).catch(next);
});
/**
* return all jumps
*/
router.get('/', auth.required, function (req, res, next) {
var query = {};
var limit = 20;
var limit = 25;
var offset = 0;
if (typeof req.query.limit !== 'undefined') {
limit = req.query.limit;
@@ -51,7 +54,7 @@ router.get('/', auth.required, function (req, res, next) {
Jump.find(query)
.limit(Number(limit))
.skip(Number(offset))
.sort({ numero: 'asc' })
.sort({ numero: 'desc' })
.populate('author')
.exec(),
Jump.count(query).exec(),
@@ -71,8 +74,71 @@ router.get('/', auth.required, function (req, res, next) {
}).catch(next);
});
router.get('/allByDate', auth.required, function (req, res, next) {
User.findById(req.payload.id).then(function (user) {
if (!user) {
return res.sendStatus(401);
}
var limit = 120;
var offset = 0;
if (typeof req.query.limit !== 'undefined') {
limit = req.query.limit;
}
if (typeof req.query.offset !== 'undefined') {
offset = req.query.offset;
}
var query = [
{
'$match': {
'$expr': {
'$eq': [ '$author' , { '$toObjectId': user._id.toString() } ]
}
}
}, {
'$project': {
'_id': 0,
'year': {
'$year': '$date'
},
'month': {
'$month': '$date'
}
}
}, {
'$group': {
'_id': {
'year': '$year',
'month': '$month'
},
'count': {
'$sum': 1
}
}
}, {
'$sort': {
'_id': 1
}
}
];
Jump.aggregate(query)
.limit(Number(limit))
.skip(Number(offset))
.exec()
.then(function (result) {
var jumps = result;
//console.log(jumps);
return res.json({
jumps: jumps,
jumpsCount: jumps.length
});
}).catch(next);
}).catch(next);
});
router.get('/feed', auth.required, function (req, res, next) {
var limit = 20;
var limit = 25;
var offset = 0;
if (typeof req.query.limit !== 'undefined') {
limit = req.query.limit;
@@ -106,6 +172,64 @@ router.get('/feed', auth.required, function (req, res, next) {
});
});
/**
* return last jump
*/
router.get('/last', auth.required, function (req, res, next) {
User.findById(req.payload.id).then(function (user) {
if (!user) {
return res.sendStatus(401);
}
/*Jump.findOne({ slug: slug })
.populate('author')
.then(function (jump) {
if (!jump) {
return res.sendStatus(404);
}
req.jump = jump;
return next();
}).catch(next);*/
Jump.find({})
.limit(1)
.skip(0)
.sort({ numero: 'desc' })
.populate('author')
.exec()
.then(function (jumps) {
if (!jumps) {
return res.sendStatus(404);
}
req.lastjump = jumps[0];
if (req.lastjump.author._id.toString() !== req.payload.id.toString()) {
return res.sendStatus(403);
}
return res.json({ jump: req.lastjump.toJSONFor(user) });
}).catch(next);
}).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);
});
/**
* save a jump
*/
router.post('/', auth.required, function (req, res, next) {
Promise.all([
req.payload ? User.findById(req.payload.id) : null
@@ -128,23 +252,9 @@ router.post('/', auth.required, function (req, res, next) {
}).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);
});
/**
* update a jump
*/
router.put('/:jump', auth.required, function (req, res, next) {
Promise.all([
req.payload ? User.findById(req.payload.id) : null
@@ -217,7 +327,9 @@ router.put('/:jump', auth.required, function (req, res, next) {
}).catch(next);
});
// delete jump
/**
* delete a jump
*/
router.delete('/:jump', auth.required, function (req, res, next) {
User.findById(req.payload.id).then(function (user) {
if (!user) {