Fix compile errors by casting variables pulled from query strings

This commit is contained in:
Tom Russell 2020-04-09 15:44:11 +01:00
parent 3c2ecbd76e
commit 3649f92d4c
4 changed files with 40 additions and 36 deletions

View File

@ -15,7 +15,7 @@ import * as userService from '../services/user';
const getBuildingsByLocation = asyncController(async (req: express.Request, res: express.Response) => {
const { lng, lat } = req.query;
try {
const result = await buildingService.queryBuildingsAtPoint(lng, lat);
const result = await buildingService.queryBuildingsAtPoint(Number(lng), Number(lat));
res.send(result);
} catch (error) {
console.error(error);
@ -27,7 +27,7 @@ const getBuildingsByLocation = asyncController(async (req: express.Request, res:
const getBuildingsByReference = asyncController(async (req: express.Request, res: express.Response) => {
const { key, id } = req.query;
try {
const result = await buildingService.queryBuildingsByReference(key, id);
const result = await buildingService.queryBuildingsByReference(String(key), String(id));
res.send(result);
} catch (error) {
console.error(error);
@ -53,7 +53,7 @@ const updateBuildingById = asyncController(async (req: express.Request, res: exp
await updateBuilding(req, res, req.session.user_id);
} else if (req.query.api_key) {
try {
const user = await userService.authAPIUser(req.query.api_key);
const user = await userService.authAPIUser(String(req.query.api_key));
await updateBuilding(req, res, user.user_id);
} catch(err) {
console.error(err);
@ -106,7 +106,7 @@ const getBuildingLikeById = asyncController(async (req: express.Request, res: ex
}
const buildingId = processParam(req.params, 'building_id', parsePositiveIntParam, true);
try {
const like = await buildingService.getBuildingLikeById(buildingId, req.session.user_id);
@ -146,7 +146,7 @@ const updateBuildingLikeById = asyncController(async (req: express.Request, res:
if(error instanceof UserError) {
throw new ApiUserError(error.message, error);
}
throw error;
}

View File

@ -5,8 +5,8 @@ import * as leadersService from '../services/leaderboard';
const getLeaders = asyncController(async (req: express.Request, res: express.Response) => {
try {
const number_limit = req.query.number_limit;
const time_limit = req.query.time_limit;
const number_limit = Number(req.query.number_limit);
const time_limit = Number(req.query.time_limit);
const result = await leadersService.getLeaders(number_limit, time_limit);
res.send({
leaders: result

View File

@ -1,35 +1,38 @@
import db from '../../db';
async function getLeaders(number_limit: number, time_limit: number) {
// Hard constraint on number of users returned
const max_limit = 100;
number_limit = Math.min(number_limit, max_limit);
try {
if(time_limit > 0){
return await db.manyOrNone(
`SELECT count(log_id) as number_edits, username
FROM logs, users
WHERE logs.user_id=users.user_id
AND CURRENT_TIMESTAMP::DATE - log_timestamp::DATE <= $1
AND NOT (users.username = 'casa_friendly_robot')
AND NOT (users.username = 'colouringlondon')
GROUP by users.username
ORDER BY number_edits DESC
LIMIT $2`, [time_limit, number_limit]
);
}else{
return await db.manyOrNone(
`SELECT count(log_id) as number_edits, username
FROM logs, users
WHERE logs.user_id=users.user_id
AND NOT (users.username = 'casa_friendly_robot')
AND NOT (users.username = 'colouringlondon')
GROUP by users.username
ORDER BY number_edits DESC
LIMIT $1`, [number_limit]
return await db.manyOrNone(
`SELECT count(log_id) as number_edits, username
FROM logs, users
WHERE logs.user_id = users.user_id
AND CURRENT_TIMESTAMP::DATE - log_timestamp::DATE <= $1
AND NOT (users.username = 'casa_friendly_robot')
AND NOT (users.username = 'colouringlondon')
GROUP by users.username
ORDER BY number_edits DESC
LIMIT $2`, [time_limit, number_limit]
);
}
} else {
return await db.manyOrNone(
`SELECT count(log_id) as number_edits, username
FROM logs, users
WHERE logs.user_id = users.user_id
AND NOT (users.username = 'casa_friendly_robot')
AND NOT (users.username = 'colouringlondon')
GROUP by users.username
ORDER BY number_edits DESC
LIMIT $1`, [number_limit]
);
}
} catch(error) {
console.error(error);
return [];
console.error(error);
return [];
}
}

View File

@ -33,7 +33,7 @@ const ChangesPage = (props: RouteComponentProps) => {
if(after_id) {
url = `${url}&after_id=${after_id}`;
}
if (before_id) {
url = `${url}&before_id=${before_id}`;
}
@ -47,14 +47,15 @@ const ChangesPage = (props: RouteComponentProps) => {
setPaging(paging);
}
} catch (err) {
setError('Connection problem. Please try again later...');
console.error('Connection problem. Please try again later...');
setError(err);
}
};
fetchData();
}, [props.location.search]);
return (
<article>
<section className="main-col">
@ -81,7 +82,7 @@ const ChangesPage = (props: RouteComponentProps) => {
(history?.length === 0) &&
<InfoBox msg="No changes so far"></InfoBox>
}
{
{
(history != undefined && history.length > 0) &&
history.map(entry => (
<li key={`${entry.revision_id}`} className="edit-history-list-element">