implemented paging
This commit is contained in:
@ -25,22 +25,43 @@ router.get('/samples', (req, res, next) => {
|
||||
const {error, value: filters} = SampleValidate.query(req.query);
|
||||
if (error) return res400(error, res);
|
||||
|
||||
let conditions;
|
||||
|
||||
let status;
|
||||
if (filters.hasOwnProperty('status')) {
|
||||
if(filters.status === 'all') {
|
||||
conditions = {$or: [{status: globals.status.validated}, {status: globals.status.new}]}
|
||||
status = {$or: [{status: globals.status.validated}, {status: globals.status.new}]}
|
||||
}
|
||||
else {
|
||||
conditions = {status: globals.status[filters.status]};
|
||||
status = {status: globals.status[filters.status]};
|
||||
}
|
||||
}
|
||||
else { // default
|
||||
conditions = {status: globals.status.validated};
|
||||
status = {status: globals.status.validated};
|
||||
}
|
||||
const query = SampleModel.find(status);
|
||||
|
||||
if (filters['page-size']) {
|
||||
query.limit(filters['page-size']);
|
||||
}
|
||||
|
||||
SampleModel.find(conditions).lean().exec((err, data) => {
|
||||
if (filters['last-id']) {
|
||||
if (filters['to-page'] && filters['to-page'] < 0) {
|
||||
query.lte('_id', mongoose.Types.ObjectId(filters['last-id'])); // TODO: consider sorting
|
||||
query.sort({_id: -1});
|
||||
}
|
||||
else {
|
||||
query.gt('_id', mongoose.Types.ObjectId(filters['last-id'])); // TODO: consider sorting
|
||||
}
|
||||
}
|
||||
|
||||
if (filters['to-page']) {
|
||||
query.skip(Math.abs(filters['to-page']) * filters['page-size']); // TODO: check order for negative numbers
|
||||
}
|
||||
|
||||
query.lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
if (filters['to-page'] && filters['to-page'] < 0) {
|
||||
data.reverse();
|
||||
}
|
||||
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
||||
})
|
||||
});
|
||||
|
Reference in New Issue
Block a user