Move int param parsing to common function

This commit is contained in:
Maciej Ziarkowski 2020-01-02 12:56:16 +00:00
parent f351c87756
commit 5a6091f13f
3 changed files with 18 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import express from 'express';
import { parseIntParam } from '../helpers';
import asyncController from '../routes/asyncController';
import * as buildingService from '../services/building';
import * as userService from '../services/user';
@ -34,7 +35,7 @@ const getBuildingsByReference = asyncController(async (req: express.Request, res
// GET individual building, POST building updates
const getBuildingById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
try {
const result = await buildingService.getBuildingById(buildingId);
@ -62,7 +63,7 @@ const updateBuildingById = asyncController(async (req: express.Request, res: exp
});
async function updateBuilding(req: express.Request, res: express.Response, userId: string) {
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
const buildingUpdate = req.body;
@ -83,7 +84,7 @@ async function updateBuilding(req: express.Request, res: express.Response, userI
// GET building UPRNs
const getBuildingUPRNsById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
try {
const result = await buildingService.getBuildingUPRNsById(buildingId);
@ -104,7 +105,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
return res.send({ like: false }); // not logged in, so cannot have liked
}
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
try {
const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id);
@ -117,7 +118,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
});
const getBuildingEditHistoryById = asyncController(async (req: express.Request, res: express.Response) => {
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
try {
const editHistory = await buildingService.getBuildingEditHistory(buildingId);
@ -133,7 +134,7 @@ const updateBuildingLikeById = asyncController(async (req: express.Request, res:
return res.send({ error: 'Must be logged in' });
}
const buildingId = parseBuildingId(req.params.building_id);
const buildingId = parseIntParam(req.params.building_id);
const { like } = req.body;
try {
@ -162,14 +163,6 @@ const getLatestRevisionId = asyncController(async (req: express.Request, res: ex
}
});
function parseBuildingId(building_id: string) {
const result = parseInt(building_id, 10);
if(isNaN(result)) {
throw new Error('Invalid building ID format');
}
return result;
}
export default {
getBuildingsByLocation,
getBuildingsByReference,

View File

@ -1,5 +1,6 @@
import express from 'express';
import { parseIntParam } from '../helpers';
import asyncController from '../routes/asyncController';
import * as dataExtractService from '../services/dataExtract';
@ -15,7 +16,7 @@ const getAllDataExtracts = asyncController(async function(req: express.Request,
const getDataExtract = asyncController(async function(req: express.Request, res: express.Response) {
try {
const extractId = req.params.extract_id;
const extractId = parseIntParam(req.params.extract_id);
const extract = await dataExtractService.getDataExtractById(extractId);
res.send({ extract: extract });
} catch (err) {

9
app/src/api/helpers.ts Normal file
View File

@ -0,0 +1,9 @@
import { strictParseInt } from '../parse';
export function parseIntParam(param: string) {
const result = strictParseInt(param);
if (isNaN(result)) {
throw new Error('Invalid parameter format: not an integer');
}
return result;
}