Merge pull request #536 from mz8i/fix/parse-building-id
Fix/parse building
This commit is contained in:
commit
93a465a549
@ -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,9 +35,10 @@ 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 { building_id } = req.params;
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
|
||||
try {
|
||||
const result = await buildingService.getBuildingById(building_id);
|
||||
const result = await buildingService.getBuildingById(buildingId);
|
||||
res.send(result);
|
||||
} catch(error) {
|
||||
console.error(error);
|
||||
@ -61,11 +63,12 @@ const updateBuildingById = asyncController(async (req: express.Request, res: exp
|
||||
});
|
||||
|
||||
async function updateBuilding(req: express.Request, res: express.Response, userId: string) {
|
||||
const { building_id } = req.params;
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
|
||||
const buildingUpdate = req.body;
|
||||
|
||||
try {
|
||||
const building = await buildingService.saveBuilding(building_id, buildingUpdate, userId);
|
||||
const building = await buildingService.saveBuilding(buildingId, buildingUpdate, userId);
|
||||
|
||||
if (typeof (building) === 'undefined') {
|
||||
return res.send({ error: 'Database error' });
|
||||
@ -81,9 +84,10 @@ async function updateBuilding(req: express.Request, res: express.Response, userI
|
||||
|
||||
// GET building UPRNs
|
||||
const getBuildingUPRNsById = asyncController(async (req: express.Request, res: express.Response) => {
|
||||
const { building_id } = req.params;
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
|
||||
try {
|
||||
const result = await buildingService.getBuildingUPRNsById(building_id);
|
||||
const result = await buildingService.getBuildingUPRNsById(buildingId);
|
||||
|
||||
if (typeof (result) === 'undefined') {
|
||||
return res.send({ error: 'Database error' });
|
||||
@ -100,9 +104,11 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
|
||||
if (!req.session.user_id) {
|
||||
return res.send({ like: false }); // not logged in, so cannot have liked
|
||||
}
|
||||
const { building_id } = req.params;
|
||||
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
|
||||
try {
|
||||
const like = await buildingService.getBuildingLikeById(building_id, req.session.user_id);
|
||||
const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id);
|
||||
|
||||
// any value returned means like
|
||||
res.send({ like: like });
|
||||
@ -112,9 +118,10 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
|
||||
});
|
||||
|
||||
const getBuildingEditHistoryById = asyncController(async (req: express.Request, res: express.Response) => {
|
||||
const { building_id } = req.params;
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
|
||||
try {
|
||||
const editHistory = await buildingService.getBuildingEditHistory(building_id);
|
||||
const editHistory = await buildingService.getBuildingEditHistory(buildingId);
|
||||
|
||||
res.send({ history: editHistory });
|
||||
} catch(error) {
|
||||
@ -127,13 +134,13 @@ const updateBuildingLikeById = asyncController(async (req: express.Request, res:
|
||||
return res.send({ error: 'Must be logged in' });
|
||||
}
|
||||
|
||||
const { building_id } = req.params;
|
||||
const buildingId = parseIntParam(req.params.building_id);
|
||||
const { like } = req.body;
|
||||
|
||||
try {
|
||||
const building = like ?
|
||||
await buildingService.likeBuilding(building_id, req.session.user_id) :
|
||||
await buildingService.unlikeBuilding(building_id, req.session.user_id);
|
||||
await buildingService.likeBuilding(buildingId, req.session.user_id) :
|
||||
await buildingService.unlikeBuilding(buildingId, req.session.user_id);
|
||||
|
||||
if (building.error) {
|
||||
return res.send(building);
|
||||
|
@ -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
9
app/src/api/helpers.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user