/api/v2/ref now available

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-06-17 23:04:42 +01:00
parent a04570c9e4
commit 34d00ad16f
6 changed files with 69 additions and 17 deletions

15
app.js
View File

@ -30,7 +30,8 @@ const statRtr = require('./src/routes/stats.routes');
const regRtr = require('./src/routes/registration.routes');
const pisRtr = require('./src/routes/pis.routes');
const trainRtr = require('./src/routes/train.routes');
const pis2Rtr = require('./src/routes/pis2.routes'); // API Version 2 Routes
const pis2Rtr = require('./src/routes/pis2.routes'); // API Version 2 Routes
const ref2Rtr = require('./src/routes/ref2.routes'); // API Version 2 Routes
const miscRtr = require('./src/routes/misc.routes'); // Non-Public API Routes
// Set Server Configurations
@ -49,15 +50,6 @@ log.out(`app: Starting OwlBoard in ${mode} mode`, 'init');
log.out(`app: Starting OwlBoard - Backend Version: ${version.app} - ` +
`API versions: ${version.api}`, 'init');
// Test for required vars:
// const varTest = require('./src/utils/varTest.utils');
// var startTest = await varTest.varTest();
//console.log("Required Vars Missing:", startTest.missing_required);
//console.log("Desired Vars Missing:", startTest.missing_desired);
// if startTest.pass == false
// console.log("Unable to start, missing required vars")
// exit app
// Express Error Handling:
app.use((err, req, res, next) => {
const statusCode = err.statuscode || 500;
@ -73,7 +65,8 @@ app.use(compression()); // Compress API Data if supported by client
app.use(limiter);
// 2023 Rationalisation Routes (/api/v2, /misc)
app.use('/api/v2/pis', pis2Rtr); // API Version 2 Routes
app.use('/api/v2/pis', authenticate, pis2Rtr); // API Version 2
app.use('/api/v2/ref', ref2Rtr); // API Version 2
app.use('/misc', miscRtr); // Non public-api endpoints (Stats, Issue, etc.)
// Unauthenticated Routes

View File

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

View File

@ -0,0 +1,52 @@
/* API V2 Exclusive Controller */
const ldb = require('../services/ldb.services');
const find = require('../services/find.services');
async function getReasonCode(req, res, next) {
try {
const code = req.params.code;
if (code === 'all') {
res.json(await ldb.getReasonCodeList());
}
res.json(await ldb.getReasonCode(code));
} catch (err) {
console.error('ERROR', err.message);
err.status = 500;
next(err);
}
}
async function getLocationReference(req, res, next) {
try {
const searchType = req.params.searchType;
const id = req.params.id;
switch (searchType) {
case 'name':
res.json(await find.name(id));
break;
case 'crs': // Same as 3alpha
case '3alpha':
res.json(await find.crs(id));
break;
case 'nlc':
res.json(await find.nlc(id));
break;
case 'tiploc':
res.json(await find.tiploc(id));
break;
case 'stanox':
res.json(await find.stanox(id));
break;
}
} catch (err) {
console.error('ERROR', err.message);
err.status = 500;
next(err);
}
}
module.exports = {
getReasonCode,
getLocationReference
};

View File

@ -22,9 +22,7 @@ router.get('/live/train/:searchType/:id');
// Timetable
router.get('/timetable/train/:date/:searchType/:id');
// Ref
router.get('/reasonCode/:code');
router.get('/locationCode/:searchType/:id');
// User
router.post('/user');

View File

@ -4,6 +4,6 @@ const pisCtr = require('../controllers/pis.controllers');
// PIS
router.get('/byCode/:code', pisCtr.byCode);
router.get('/:startCrs/:endCrs', pisCtr.byStartEndCRS);
router.get('/byStartEnd/:startCrs/:endCrs', pisCtr.byStartEndCRS);
module.exports = router;

View File

@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
const refCtr = require('../controllers/ref.controllers');
// Ref
router.get('/reasonCode/:code', refCtr.getReasonCode);
router.get('/locationCode/:searchType/:id', refCtr.getLocationReference);
module.exports = router;