271 lines
7.4 KiB
JavaScript
271 lines
7.4 KiB
JavaScript
var router = require('express').Router(),
|
|
mongoose = require('mongoose'),
|
|
Canopy = mongoose.model('Canopy'),
|
|
Jump = mongoose.model('Jump'),
|
|
User = mongoose.model('User');
|
|
const auth = require('../auth');
|
|
|
|
router.get('/', auth.required, function (req, res, next) {
|
|
var query = {};
|
|
var limit = 25;
|
|
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.slug !== 'undefined') {
|
|
query.slug = req.query.slug;
|
|
}
|
|
|
|
User.findById(req.payload.id).then(function (user) {
|
|
if (!user) {
|
|
return res.sendStatus(401);
|
|
}
|
|
if (user.role == 'Admin') {
|
|
return res.sendStatus(403);
|
|
}
|
|
Jump.find(query)
|
|
.limit(Number(limit))
|
|
.skip(Number(offset))
|
|
.exec()
|
|
.then(function (result) {
|
|
var canopies = result;
|
|
return res.json({
|
|
canopies: canopies,
|
|
canopiesCount: canopies.length
|
|
});
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
router.get('/allBySize', auth.required, function (req, res, next) {
|
|
var limit = 25;
|
|
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 = [
|
|
{
|
|
'$match': {
|
|
'$expr': {
|
|
'$eq': [ '$author' , { '$toObjectId': user._id.toString() } ]
|
|
}
|
|
}
|
|
}, {
|
|
'$group': {
|
|
'_id': {
|
|
'taille': '$taille'
|
|
},
|
|
'count': {
|
|
'$sum': 1
|
|
}
|
|
}
|
|
}, {
|
|
'$sort':
|
|
{
|
|
'_id': 1
|
|
},
|
|
}
|
|
];
|
|
Promise.all([
|
|
Jump.aggregate(query)
|
|
.limit(Number(limit))
|
|
.skip(Number(offset))
|
|
.exec()
|
|
]).then(function (results) {
|
|
var canopies = results[0];
|
|
return res.json({
|
|
canopies: canopies,
|
|
canopiesCount: canopies.length
|
|
});
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
router.get('/allByYear', auth.required, function (req, res, next) {
|
|
User.findById(req.payload.id).then(function (user) {
|
|
if (!user) {
|
|
return res.sendStatus(401);
|
|
}
|
|
var limit = 25;
|
|
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,
|
|
'taille': 1,
|
|
'year': {
|
|
'$year': '$date'
|
|
}
|
|
}
|
|
}, {
|
|
'$group': {
|
|
'_id': {
|
|
'taille': '$taille',
|
|
'year': '$year'
|
|
},
|
|
'count': {
|
|
'$sum': 1
|
|
}
|
|
}
|
|
}, {
|
|
'$sort': {
|
|
'_id': 1
|
|
}
|
|
}
|
|
];
|
|
|
|
Jump.aggregate(query)
|
|
.limit(Number(limit))
|
|
.skip(Number(offset))
|
|
.exec()
|
|
.then(function (result) {
|
|
var canopies = result;
|
|
//console.log(dropzones);
|
|
return res.json({
|
|
canopies: canopies,
|
|
canopiesCount: canopies.length
|
|
});
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
router.get('/allModelBySize', auth.required, function (req, res, next) {
|
|
var limit = 25;
|
|
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 = [
|
|
{
|
|
'$match': {
|
|
'$expr': {
|
|
'$eq': [ '$author' , { '$toObjectId': user._id.toString() } ]
|
|
}
|
|
}
|
|
}, {
|
|
'$group': {
|
|
'_id': {
|
|
'taille': '$taille',
|
|
'voile': '$voile'
|
|
},
|
|
'count': {
|
|
'$sum': 1
|
|
}
|
|
}
|
|
}, {
|
|
'$sort':
|
|
{
|
|
'_id': 1
|
|
},
|
|
}
|
|
];
|
|
Promise.all([
|
|
Jump.aggregate(query)
|
|
.limit(Number(limit))
|
|
.skip(Number(offset))
|
|
.exec()
|
|
]).then(function (results) {
|
|
var canopies = results[0];
|
|
return res.json({
|
|
canopies: canopies,
|
|
canopiesCount: canopies.length
|
|
});
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
router.get('/allModelByYear', auth.required, function (req, res, next) {
|
|
User.findById(req.payload.id).then(function (user) {
|
|
if (!user) {
|
|
return res.sendStatus(401);
|
|
}
|
|
var limit = 25;
|
|
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,
|
|
'voile': 1,
|
|
'taille': 1,
|
|
'year': {
|
|
'$year': '$date'
|
|
}
|
|
}
|
|
}, {
|
|
'$group': {
|
|
'_id': {
|
|
'taille': '$taille',
|
|
'voile': '$voile',
|
|
'year': '$year'
|
|
},
|
|
'count': {
|
|
'$sum': 1
|
|
}
|
|
}
|
|
}, {
|
|
'$sort': {
|
|
'_id': 1
|
|
}
|
|
}
|
|
];
|
|
|
|
Jump.aggregate(query)
|
|
.limit(Number(limit))
|
|
.skip(Number(offset))
|
|
.exec()
|
|
.then(function (result) {
|
|
var canopies = result;
|
|
//console.log(dropzones);
|
|
return res.json({
|
|
canopies: canopies,
|
|
canopiesCount: canopies.length
|
|
});
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
module.exports = router;
|