From d79744008f587e6b62165644b92a4e72b907a99f Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Fri, 5 May 2023 16:13:56 +0200 Subject: [PATCH] Added get from id --- index.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d42f8b6..2cfe18a 100644 --- a/index.js +++ b/index.js @@ -30,9 +30,36 @@ app.use(express.static(join(__dirname, 'public'))) * Query params : * latitude * longitude + * id?: faster searches */ app.use('/rep', async (req, res) => { - let { lat, lon } = req.query + let { lat, lon, id } = req.query + + // First tries to get the ID if in request + if (id) { + const repIdRegex = new RegExp(/(PA)([0-9]+)/gi) + const parsedId = String(id).match(repIdRegex) + + // Fail first if regex fails + if (!parsedId) { + res.status(401).json({ + "message": "Le format de l'ID ne correspond pas au format attendu (une suite de chiffres préfixée de 'PA')" + }); + return + } + + // If regex okay, fetch file from server + try { + const repFile = readFileSync(join(__dirname, `public/reps/${parsedId}.json`), 'utf-8') + res.end(repFile) + return + } catch (err) { + res.status(404).json({ + "message": "Aucun député n'a été trouvé avec cet identifiant" + }); + return + } + } // If the query doesn't contain the required params... if (!lat || !lon) { @@ -69,12 +96,6 @@ app.use('/rep', async (req, res) => { let circoForeign - // foreignLookupTable.forEach((value, index) => { - // if (value.includes(countryName)) { - // circoForeign = index - // } - // }); - for (const [key, value] of Object.entries(foreignLookupTable)) { if (value.includes(countryName)) { circoForeign = key