diff --git a/api/sample.yaml b/api/sample.yaml index 4ebb61f..b37702d 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -12,14 +12,14 @@ schema: type: string example: all - - name: last-id - description: last id of current page, if not given the results are displayed from start + - name: from-id + description: first id of the requested page, if not given the results are displayed from start in: query schema: type: string example: 5ea0450ed851c30a90e70894 - name: to-page - description: relative change of pages, use negative values to get back, defaults to 0 (if last-id is given, the sample after is the first of the result, so the next page is selected automatically), works only together with page-size + description: relative change of pages, use negative values to get back, defaults to 0, works only together with page-size in: query schema: type: string diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index 4af6d55..bd3cb79 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -113,16 +113,16 @@ describe('/sample', () => { done(); }); }); - it('returns results starting after last-id', done => { + it('returns results starting from first-id', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&last-id=400000000000000000000002', + url: '/samples?status=all&from-id=400000000000000000000002', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { if (err) return done(err); - should(res.body[0]).have.property('_id', '400000000000000000000003'); - should(res.body[1]).have.property('_id', '400000000000000000000004'); + should(res.body[0]).have.property('_id', '400000000000000000000002'); + should(res.body[1]).have.property('_id', '400000000000000000000003'); done(); }); }); @@ -141,13 +141,13 @@ describe('/sample', () => { it('works with negative page numbers', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=-1&page-size=2&last-id=400000000000000000000004', + url: '/samples?status=all&to-page=-1&page-size=2&from-id=400000000000000000000004', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { if (err) return done(err); - should(res.body[0]).have.property('_id', '400000000000000000000001'); - should(res.body[1]).have.property('_id', '400000000000000000000002'); + should(res.body[0]).have.property('_id', '400000000000000000000002'); + should(res.body[1]).have.property('_id', '400000000000000000000003'); done(); }); }); @@ -167,7 +167,7 @@ describe('/sample', () => { it('returns an empty array for a page number out of negative range', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?status=all&to-page=-100&page-size=3&last-id=400000000000000000000004', + url: '/samples?status=all&to-page=-100&page-size=3&from-id=400000000000000000000004', auth: {basic: 'janedoe'}, httpStatus: 200 }).end((err, res) => { @@ -186,13 +186,13 @@ describe('/sample', () => { res: {status: 'Invalid body format', details: '"page-size" must be larger than or equal to 1'} }); }); - it('rejects an invalid last-id', done => { + it('rejects an invalid from-id', done => { TestHelper.request(server, done, { method: 'get', - url: '/samples?last-id=40000000000h000000000002', + url: '/samples?from-id=40000000000h000000000002', auth: {basic: 'janedoe'}, httpStatus: 400, - res: {status: 'Invalid body format', details: '"last-id" with value "40000000000h000000000002" fails to match the required pattern: /[0-9a-f]{24}/'} + res: {status: 'Invalid body format', details: '"from-id" with value "40000000000h000000000002" fails to match the required pattern: /[0-9a-f]{24}/'} }); }); it('rejects a to-page without page-size', done => { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 24f33cf..156725b 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -43,18 +43,18 @@ router.get('/samples', (req, res, next) => { query.limit(filters['page-size']); } - if (filters['last-id']) { + if (filters['from-id']) { if (filters['to-page'] && filters['to-page'] < 0) { - query.lte('_id', mongoose.Types.ObjectId(filters['last-id'])); // TODO: consider sorting + query.lt('_id', mongoose.Types.ObjectId(filters['from-id'])); // TODO: consider sorting query.sort({_id: -1}); } else { - query.gt('_id', mongoose.Types.ObjectId(filters['last-id'])); // TODO: consider sorting + query.gte('_id', mongoose.Types.ObjectId(filters['from-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.skip(Math.abs(filters['to-page'] + Number(filters['to-page'] < 0)) * filters['page-size']); // TODO: check order for negative numbers } query.lean().exec((err, data) => { @@ -62,6 +62,7 @@ router.get('/samples', (req, res, next) => { if (filters['to-page'] && filters['to-page'] < 0) { data.reverse(); } + console.log(data); res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors }) }); diff --git a/src/routes/validate/sample.ts b/src/routes/validate/sample.ts index f1f17f9..4b10a7c 100644 --- a/src/routes/validate/sample.ts +++ b/src/routes/validate/sample.ts @@ -130,7 +130,7 @@ export default class SampleValidate { static query (data) { return Joi.object({ status: Joi.string().valid('validated', 'new', 'all'), - 'last-id': IdValidate.get(), + 'from-id': IdValidate.get(), 'to-page': Joi.number().integer(), 'page-size': Joi.number().integer().min(1) }).with('to-page', 'page-size').validate(data);