Tidy 'pis' related code
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
87532b001d
commit
a5a73812a9
@ -1,54 +0,0 @@
|
|||||||
const pis = require("../services/pis.services");
|
|
||||||
|
|
||||||
/* Used in /api/v2 */
|
|
||||||
async function byStartEndCRS(req, res, next) {
|
|
||||||
// if (!req.isAuthed) {
|
|
||||||
// const err = new Error("Unauthorized");
|
|
||||||
// err.status = 401;
|
|
||||||
// return next(err);
|
|
||||||
// }
|
|
||||||
try {
|
|
||||||
let startCrs = req.params.startCrs;
|
|
||||||
let endCrs = req.params.endCrs;
|
|
||||||
res.json(await pis.findPisByOrigDest(startCrs, endCrs));
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Unknown Error", err.message);
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Used in /api/v2 */
|
|
||||||
async function byCode(req, res, next) {
|
|
||||||
// if (!req.isAuthed) {
|
|
||||||
// const err = new Error("Unauthorized");
|
|
||||||
// err.status = 401;
|
|
||||||
// return next(err);
|
|
||||||
// }
|
|
||||||
try {
|
|
||||||
let code = req.params.code;
|
|
||||||
res.json(await pis.findPisByCode(code));
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Unknown Error", err.message);
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function random(req, res, next) {
|
|
||||||
// if (!req.isAuthed) {
|
|
||||||
// const err = new Error("Unauthorized");
|
|
||||||
// err.status = 401;
|
|
||||||
// return next(err);
|
|
||||||
// }
|
|
||||||
try {
|
|
||||||
res.json(await pis.findRandom());
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Unknown Error", err.message);
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
byStartEndCRS,
|
|
||||||
byCode,
|
|
||||||
random,
|
|
||||||
};
|
|
38
src/controllers/pis.controllers.ts
Normal file
38
src/controllers/pis.controllers.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { findPisByOrigDest, findPisByCode } from "../services/pis.services";
|
||||||
|
|
||||||
|
async function byStartEndCRS(req: Request, res: Response, next: NextFunction) {
|
||||||
|
// if (!req.isAuthed) {
|
||||||
|
// const err = new Error("Unauthorized");
|
||||||
|
// err.status = 401;
|
||||||
|
// return next(err);
|
||||||
|
// }
|
||||||
|
try {
|
||||||
|
let startCrs = req.params.startCrs;
|
||||||
|
let endCrs = req.params.endCrs;
|
||||||
|
res.json(await findPisByOrigDest(startCrs, endCrs));
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error("Unknown Error", err.message);
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function byCode(req: Request, res: Response, next: NextFunction) {
|
||||||
|
// if (!req.isAuthed) {
|
||||||
|
// const err = new Error("Unauthorized");
|
||||||
|
// err.status = 401;
|
||||||
|
// return next(err);
|
||||||
|
// }
|
||||||
|
try {
|
||||||
|
let code = req.params.code;
|
||||||
|
res.json(await findPisByCode(code));
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error("Unknown Error", err.message);
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
byStartEndCRS,
|
||||||
|
byCode,
|
||||||
|
};
|
@ -15,7 +15,7 @@ import { Document } from "mongodb";
|
|||||||
|
|
||||||
export const supported = ["GW", "UK"];
|
export const supported = ["GW", "UK"];
|
||||||
|
|
||||||
async function findPisByOrigDest(start: string, end: string) {
|
export async function findPisByOrigDest(start: string, end: string) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`pisServices.findPisByOrigDest: Searching for Orig: ${start}, Dest: ${end}`
|
`pisServices.findPisByOrigDest: Searching for Orig: ${start}, Dest: ${end}`
|
||||||
);
|
);
|
||||||
@ -40,7 +40,7 @@ async function findPisByOrigDest(start: string, end: string) {
|
|||||||
return search;
|
return search;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findPisByCode(
|
export async function findPisByCode(
|
||||||
code: string
|
code: string
|
||||||
): Promise<OB_Pis_SimpleObject | null> {
|
): Promise<OB_Pis_SimpleObject | null> {
|
||||||
logger.debug(`pisServices.findPisByCode: Searching for PIS code: ${code}`);
|
logger.debug(`pisServices.findPisByCode: Searching for PIS code: ${code}`);
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
//const log = require('../utils/log.utils');
|
|
||||||
|
|
||||||
import { logger } from "./logger.utils";
|
import { logger } from "./logger.utils";
|
||||||
|
|
||||||
function removeNonAlphanumeric(inputString: string) {
|
function removeNonAlphanumeric(inputString: string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user