From 4d3a4fb2f5fd7b0cda152ce1f9a688d85b3a5deb Mon Sep 17 00:00:00 2001 From: Clement Date: Wed, 15 May 2024 00:12:27 +0200 Subject: [PATCH] rework OTM --- Express/barAndCafe/src/openTripMaps.ts | 112 +++++++++++++++---------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/Express/barAndCafe/src/openTripMaps.ts b/Express/barAndCafe/src/openTripMaps.ts index e72fabe..7655937 100644 --- a/Express/barAndCafe/src/openTripMaps.ts +++ b/Express/barAndCafe/src/openTripMaps.ts @@ -6,29 +6,48 @@ dotenv.config({path: '../../.env'}) const key = process.env.OPEN_TRIP_MAPS_KEY -const optionsDrink = { - method: 'GET', - url: 'https://api.opentripmap.com/0.1/en/places/radius', - params: { - radius: '1000', - lon: '-1.43333', - lat: '46.66667', - apikey: key, - kinds: 'bars,cafes,pubs,biergartens' - }, - headers: {'Content-Type': 'application/json'} -}; +async function callRadius(lon: string, lat: string, radius = '1000') { + const optionsDrink = { + method: 'GET', + url: 'https://api.opentripmap.com/0.1/en/places/radius', + params: { + radius: radius, + lon: lon, + lat: lat, + apikey: key, + kinds: 'bars,cafes,pubs,biergartens' + }, + headers: {'Content-Type': 'application/json'} + } + + try { + const { data } = await axios.request(optionsDrink); + return data + } catch (error) { + console.error(error); + } +} -const optionsCity = { - method: 'GET', - url: 'https://api.opentripmap.com/0.1/en/places/geoname', - params: { - name: 'Paris', - apikey: key - }, - headers: {'Content-Type': 'application/json'} -}; +async function callCity(name:string) { + const optionsCity = { + method: 'GET', + url: 'https://api.opentripmap.com/0.1/en/places/geoname', + params: { + name: name, + apikey: key + }, + headers: {'Content-Type': 'application/json'} + }; + + try { + const { data } = await axios.request(optionsCity); + return data + } catch (error) { + console.error(error); + } +} + /** * Handle GET request for city search route ('/otm/city'). @@ -37,35 +56,38 @@ const optionsCity = { */ export async function getCity(req: express.Request, res: express.Response) { const cityName = req.query["name"] - const radius = req.query["radius"] + let radius = req.query["radius"] - if(!key){ - res.status(401).send("Missing OTM key") - return - } if(!cityName){ res.status(400).send("Missing Argument name") return } - if(radius){ - optionsDrink.params.radius = radius as string - } - - optionsCity.params.name = cityName as string - - try { - const { data } = await axios.request(optionsCity); - optionsDrink.params.lat = data.lat - optionsDrink.params.lon = data.lon - } catch (error) { - console.error(error); - } - - try { - const { data } = await axios.request(optionsDrink); - res.send(data); - } catch (error) { - console.error(error); + if(!radius){ + radius = "1000" } + const cityPose = await callCity(cityName as string) + res.send( await callRadius(cityPose.lon,cityPose.lat, radius as string)) } +/** + * Handle GET request for city search route ('/otm/radius'). + * @param {express.Request} req - HTTP Request object. + * @param {express.Response} res - HTTP Response object. + */ +export async function getRadius(req:express.Request, res: express.Response) { + const lon = req.query["lon"] as string + const lat = req.query["lat"] as string + let radius = req.query["radius"] + if(!lon || !lat){ + res.status(400).send("Missing Argument name") + return + } + if(!radius){ + radius = "1000" + } + + res.send(callRadius(lon,lat,radius as string)) +} + +//TODO: fair une route ou l'on donne l'id un établicement pour avoir des détailles +//TODO: fair une route ou l'on donne 2 coordonée