From b590eb6cf5dbe5dafe764a218561ce1c50a19173 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 25 May 2023 21:27:37 +0100 Subject: [PATCH] Add random PIS return Signed-off-by: Fred Boniface --- src/configs/version.configs.js | 2 +- src/controllers/pis.controllers.js | 12 +++++++++++- src/routes/pis.routes.js | 1 + src/services/pis.services.js | 16 ++++++++++++++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/configs/version.configs.js b/src/configs/version.configs.js index d6399f1..e128aea 100644 --- a/src/configs/version.configs.js +++ b/src/configs/version.configs.js @@ -1,6 +1,6 @@ const version = { api: ['/api/v1/',], - app: '2023.5.2' + app: '2023.5.3' }; module.exports = version; \ No newline at end of file diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index a8da515..46f33f9 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -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 }; \ No newline at end of file diff --git a/src/routes/pis.routes.js b/src/routes/pis.routes.js index 2783433..b1ac1ec 100644 --- a/src/routes/pis.routes.js +++ b/src/routes/pis.routes.js @@ -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; \ No newline at end of file diff --git a/src/services/pis.services.js b/src/services/pis.services.js index c97bb90..be72507 100644 --- a/src/services/pis.services.js +++ b/src/services/pis.services.js @@ -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 }; \ No newline at end of file