API: Implement /api/v1/find
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
4c6d66c5d9
commit
2147d2f106
4
app.js
4
app.js
@ -14,7 +14,8 @@ const version = require('./src/configs/version.configs'); // Version Strings
|
|||||||
const listRtr = require('./src/routes/list.routes'); // /list endpoints
|
const listRtr = require('./src/routes/list.routes'); // /list endpoints
|
||||||
const ldbRtr = require('./src/routes/ldb.routes'); // /ldb endpoints
|
const ldbRtr = require('./src/routes/ldb.routes'); // /ldb endpoints
|
||||||
const kubeRtr = require('./src/routes/kube.routes'); // /kube endpoints
|
const kubeRtr = require('./src/routes/kube.routes'); // /kube endpoints
|
||||||
const initDb = require('./src/utils/dbinit.utils') // DB Init Utility
|
const findRtr = require('./src/routes/find.routes'); // /find endpoints
|
||||||
|
const initDb = require('./src/utils/dbinit.utils'); // DB Init Utility
|
||||||
|
|
||||||
// Set Server Configurations
|
// Set Server Configurations
|
||||||
const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"
|
const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"
|
||||||
@ -51,6 +52,7 @@ app.use(express.static('static')); //Serve static content from /static
|
|||||||
app.use('/api/v1/list', listRtr);
|
app.use('/api/v1/list', listRtr);
|
||||||
app.use('/api/v1/ldb', ldbRtr);
|
app.use('/api/v1/ldb', ldbRtr);
|
||||||
app.use('/api/v1/kube', kubeRtr);
|
app.use('/api/v1/kube', kubeRtr);
|
||||||
|
app.use('/api/v1/find', findRtr);
|
||||||
|
|
||||||
// Start Express
|
// Start Express
|
||||||
app.listen(srvPort, srvListen, (error) =>{
|
app.listen(srvPort, srvListen, (error) =>{
|
||||||
|
58
src/controllers/find.controllers.js
Normal file
58
src/controllers/find.controllers.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
const find = require('../services/find.services');
|
||||||
|
|
||||||
|
async function findName(req, res, next){
|
||||||
|
try {
|
||||||
|
var id = req.params.id
|
||||||
|
res.json(await find.name(id))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Unknown Error`, err.message);
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findCrs(req, res, next){
|
||||||
|
try {
|
||||||
|
var id = req.params.id
|
||||||
|
res.json(await find.crs(id))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Unknown Error`, err.message);
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findNlc(req, res, next){
|
||||||
|
try {
|
||||||
|
var id = req.params.id
|
||||||
|
res.json(await find.nlc(id))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Unknown Error`, err.message);
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findTiploc(req, res, next){
|
||||||
|
try {
|
||||||
|
var id = req.params.id
|
||||||
|
res.json(await find.tiploc(id))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Unknown Error`, err.message);
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findStanox(req, res, next){
|
||||||
|
try {
|
||||||
|
var id = req.params.id
|
||||||
|
res.json(await find.stanox(id))
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`Unknown Error`, err.message);
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
findName,
|
||||||
|
findCrs,
|
||||||
|
findNlc,
|
||||||
|
findTiploc,
|
||||||
|
findStanox
|
||||||
|
}
|
23
src/routes/find.routes.js
Normal file
23
src/routes/find.routes.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const findController = require('../controllers/find.controllers');
|
||||||
|
|
||||||
|
/* GET programming languages. */
|
||||||
|
//router.get('/', programmingLanguagesController.get);
|
||||||
|
|
||||||
|
/* POST programming language */
|
||||||
|
//router.post('/', programmingLanguagesController.create);
|
||||||
|
|
||||||
|
/* PUT programming language */
|
||||||
|
//router.put('/:id', programmingLanguagesController.update);
|
||||||
|
|
||||||
|
/* DELETE programming language */
|
||||||
|
//router.delete('/:id', programmingLanguagesController.remove);
|
||||||
|
|
||||||
|
router.get('/name/:id', findController.findName);
|
||||||
|
router.get('/crs/:id', findController.findCrs);
|
||||||
|
router.get('/nlc/:id', findController.findNlc);
|
||||||
|
router.get('/tiploc/:id', findController.findTiploc);
|
||||||
|
router.get('/stanox/:id', findController.findStanox);
|
||||||
|
|
||||||
|
module.exports = router;
|
59
src/services/find.services.js
Normal file
59
src/services/find.services.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// Parse and return a find request
|
||||||
|
|
||||||
|
const log = require('../utils/log.utils'); // Log Helper
|
||||||
|
const db = require('../services/dbAccess.services');
|
||||||
|
const san = require('../utils/sanitizer.utils')
|
||||||
|
|
||||||
|
// DB Query: query(collection, query)
|
||||||
|
|
||||||
|
// Define collection as all queries are for the "corpus" collection.
|
||||||
|
const col = "corpus"
|
||||||
|
|
||||||
|
async function name(id){
|
||||||
|
log.out(`findServices.name: Finding station name: ${id}`)
|
||||||
|
var name = san.cleanApiEndpointTxt(id.toUpperCase())
|
||||||
|
query = {NLCDESC: name}
|
||||||
|
var data = await db.query(col,query)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
async function crs(id){
|
||||||
|
log.out(`findServices.crs: Finding crs: ${id}`)
|
||||||
|
var crs = san.cleanApiEndpointTxt(id.toUpperCase())
|
||||||
|
query = {'3ALPHA': crs}
|
||||||
|
var data = await db.query(col,query)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
async function nlc(id){
|
||||||
|
log.out(`findServices.nlc: Finding nlc: ${id}`)
|
||||||
|
var nlc = san.cleanApiEndpointNum(id)
|
||||||
|
query = {NLC: parseInt(nlc)}
|
||||||
|
log.out(`findServices.nlc: NLC Converted to int: ${query}`)
|
||||||
|
var data = await db.query(col,query)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
async function tiploc(id){
|
||||||
|
log.out(`findServices.tiploc: Finding tiploc: ${id}`)
|
||||||
|
var tiploc = san.cleanApiEndpointTxt(id.toUpperCase())
|
||||||
|
query = {TIPLOC: tiploc}
|
||||||
|
var data = await db.query(col,query)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
async function stanox(id){
|
||||||
|
log.out(`findServices.stanox: Finding stanox: ${id}`)
|
||||||
|
var stanox = san.cleanApiEndpointNum(id)
|
||||||
|
query = {STANOX: String(stanox)}
|
||||||
|
var data = await db.query(col,query)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name,
|
||||||
|
crs,
|
||||||
|
nlc,
|
||||||
|
tiploc,
|
||||||
|
stanox
|
||||||
|
}
|
@ -16,7 +16,7 @@ const ldbKey = process.env.OWL_LDB_KEY
|
|||||||
const ldbsvKey = process.env.OWL_LDB_SVKEY
|
const ldbsvKey = process.env.OWL_LDB_SVKEY
|
||||||
|
|
||||||
async function get(body, id){
|
async function get(body, id){
|
||||||
var cleanId = san.cleanApiEndpoint(id);
|
var cleanId = san.cleanApiEndpointTxt(id);
|
||||||
var obj = await util.checkCrs(cleanId);
|
var obj = await util.checkCrs(cleanId);
|
||||||
try {
|
try {
|
||||||
var crs = obj[0]['3ALPHA'];
|
var crs = obj[0]['3ALPHA'];
|
||||||
|
@ -61,7 +61,7 @@ async function build(db){ // `db` must be one of: `corpus`, `stations`, `all`.
|
|||||||
await dbAccess.dropCollection("corpus");
|
await dbAccess.dropCollection("corpus");
|
||||||
dbAccess.putCorpus(corpusAll);
|
dbAccess.putCorpus(corpusAll);
|
||||||
|
|
||||||
log.out(`dbInitUtils.build: Updateing corpus meta`)
|
log.out(`dbInitUtils.build: Updating corpus meta`)
|
||||||
dbAccess.updateMeta("collection", "corpus", time.jsUnix(Date.now()));
|
dbAccess.updateMeta("collection", "corpus", time.jsUnix(Date.now()));
|
||||||
}
|
}
|
||||||
if (db === "stations") {
|
if (db === "stations") {
|
||||||
|
@ -16,14 +16,23 @@ string.addDash("@abcd efgh"); // @abcd-efgh
|
|||||||
string.removeSpace("@abcd efgh"); // @abcdefgh
|
string.removeSpace("@abcd efgh"); // @abcdefgh
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function cleanApiEndpoint(input) {
|
function cleanApiEndpointTxt(input) {
|
||||||
var output = clean.sanitize(input)
|
var output = clean.sanitize.keepSpace(input)
|
||||||
if (output != input){
|
if (output != input){
|
||||||
log.out(`sanitizerUtils.cleanApiEndpoint: WARN: Sanitizing changed string. Input = ${input}`);
|
log.out(`sanitizerUtils.cleanApiEndpoint: WARN: Sanitizing changed string. Input = ${input}`);
|
||||||
}
|
}
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanApiEndpointNum(input) {
|
||||||
|
var output = clean.sanitize.keepNumber(input)
|
||||||
|
if (output != input){
|
||||||
|
log.out(`sanitizerUtils.cleanApiEndpointNum: WARN: Sanitizing changed string. Input = ${input}`);
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
cleanApiEndpoint
|
cleanApiEndpointTxt,
|
||||||
|
cleanApiEndpointNum
|
||||||
}
|
}
|
Reference in New Issue
Block a user