Archived
2

added /samples/count

This commit is contained in:
VLE2FE 2020-06-25 14:29:54 +02:00
parent 4dad680edf
commit 49f7a475b7
3 changed files with 64 additions and 1 deletions

View File

@ -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'

View File

@ -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, {

View File

@ -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;