Add random PIS return

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-05-25 21:27:37 +01:00
parent a12951fe4f
commit b590eb6cf5
4 changed files with 27 additions and 4 deletions

View File

@ -1,6 +1,6 @@
const version = {
api: ['/api/v1/',],
app: '2023.5.2'
app: '2023.5.3'
};
module.exports = version;

View File

@ -21,8 +21,18 @@ async function byCode(req, res, next){
}
}
async function random(req, res, next){
try {
res.json(await pis.findRandom());
} catch (err) {
console.error('Unknown Error', err.message);
next(err);
}
}
module.exports = {
byOrigDest,
byCode
byCode,
random
};

View File

@ -4,5 +4,6 @@ const pisController = require('../controllers/pis.controllers');
router.get('/origdest/:start/:end', pisController.byOrigDest);
router.get('/code/:code', pisController.byCode);
router.get('/code/random', pisController.random);
module.exports = router;

View File

@ -28,7 +28,7 @@ async function findPisByOrigDest(start,end) {
}
async function findPisByCode(code) {
log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`);
log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`, 'dbug');
const cleanCode = clean.removeNonNumeric(code);
const query = {
'code': parseInt(cleanCode)
@ -37,9 +37,21 @@ async function findPisByCode(code) {
return await search;
}
async function findRandom() {
log.out('pisServices.findRandom: Finding five random PIS Codes', 'dbug');
const query = {
$sample: {
size: 5
}
};
const results = db.query('pis', query);
return results;
}
// Hopefully at some point, I will also be able to implement a find PIS code by headcode option.
module.exports = {
findPisByOrigDest,
findPisByCode
findPisByCode,
findRandom
};