From 49f7a475b714303de23ace51142bc29835c82888 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 25 Jun 2020 14:29:54 +0200 Subject: [PATCH] added /samples/count --- api/sample.yaml | 19 +++++++++++++++++++ src/routes/sample.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ src/routes/sample.ts | 10 +++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/api/sample.yaml b/api/sample.yaml index b37702d..bb5d9be 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -71,6 +71,25 @@ 500: $ref: 'api.yaml#/components/responses/500' +/samples/count: + get: + summary: total number of samples + description: 'Auth: all, levels: read, write, maintain, dev, admin' + tags: + - /sample + responses: + 200: + description: sample count + content: + application/json: + schema: + properties: + count: + type: number + example: 864 + 500: + $ref: 'api.yaml#/components/responses/500' + /sample/{id}: parameters: - $ref: 'api.yaml#/components/parameters/Id' diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index bd3cb79..bdda00e 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -318,6 +318,42 @@ describe('/sample', () => { }); }); + describe('GET /samples/count', () => { + it('returns the correct number of samples', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/samples/count', + auth: {basic: 'admin'}, + httpStatus: 200 + }).end((err, res) => { + if (err) return done(err); + const json = require('../test/db.json'); + should(res.body.count).be.eql(json.collections.samples.length); + done(); + }); + }); + it('works with an API key', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/samples/count', + auth: {key: 'janedoe'}, + httpStatus: 200 + }).end((err, res) => { + if (err) return done(err); + const json = require('../test/db.json'); + should(res.body.count).be.eql(json.collections.samples.length); + done(); + }); + }); + it('rejects unauthorized requests', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/samples/count', + httpStatus: 401 + }); + }); + }); + describe('GET /sample/{id}', () => { it('returns the right sample', done => { TestHelper.request(server, done, { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 156725b..19ec993 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -62,7 +62,6 @@ 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 }) }); @@ -76,6 +75,15 @@ router.get('/samples/:state(new|deleted)', (req, res, next) => { }); }); +router.get('/samples/count', (req, res, next) => { + if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return; + + SampleModel.estimatedDocumentCount((err, data) => { + if (err) return next(err); + res.json({count: data}); + }); +}); + router.get('/sample/' + IdValidate.parameter(), (req, res, next) => { if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;