Compare commits

...

3 Commits

Author SHA1 Message Date
02dc74bb79 add box route
All checks were successful
Build Docker Image Front / run (pull_request) Successful in 23s
Build Docker Image Back / run (pull_request) Successful in 41s
JsDocs / coverage (pull_request) Successful in 37s
Test and coverage / coverage (pull_request) Successful in 1m22s
2024-05-18 14:35:28 +02:00
4ffef9be27 add box function 2024-05-18 14:35:18 +02:00
70fa338f2f add otm tes box 2024-05-18 14:35:07 +02:00
3 changed files with 245 additions and 57 deletions

View File

@ -1,4 +1,4 @@
import { getCity, getRadius, getPoiId } from "./openTripMaps" import { getCity, getRadius, getPoiId, getBox } from "./openTripMaps"
import express from "express" import express from "express"
/** /**
* Initialize Express application instance. * Initialize Express application instance.
@ -107,7 +107,7 @@ app.get("/otm/radius", getRadius)
* type: string * type: string
* responses: * responses:
* 200: * 200:
* description: Return a list of bars and coffee in city in geoJSON format * description: Return the detaill of an POI in otm
* 400: * 400:
* description: Missing Argument Error * description: Missing Argument Error
* 401: * 401:
@ -115,4 +115,53 @@ app.get("/otm/radius", getRadius)
*/ */
app.get("/otm/poidetaill", getPoiId) app.get("/otm/poidetaill", getPoiId)
/**
* @openapi
* /otm/box:
* get:
* summary: return the drinks in a box
* description: return the drinks in a defined box
* parameters:
* - name: lon1
* in: query
* required: true
* description: longitude 1 of the box
* schema:
* type: number
* minimum: -180
* maximum: 180
* - name: lat1
* in: query
* required: true
* description: latitude 1 of the box
* schema:
* type: number
* minimum: -90
* maximum: 90
* - name: lon2
* in: query
* required: true
* description: longitude 2 of the box
* schema:
* type: number
* minimum: -180
* maximum: 180
* - name: lat2
* in: query
* required: true
* description: latitude 2 of the box
* schema:
* type: number
* minimum: -90
* maximum: 90
* responses:
* 200:
* description: Return a list of bars and coffee in city in geoJSON format
* 400:
* description: Missing Argument Error
* 401:
* description: Missing OTM tocken
*/
app.get("/otm/box", getBox)
export default app export default app

View File

@ -1,10 +1,47 @@
import axios from 'axios'; import axios from 'axios'
import express from "express" import express from "express"
import * as dotenv from "dotenv"; import * as dotenv from "dotenv"
dotenv.config({path: '../../.env'}) dotenv.config({path: '../../.env'})
const key = process.env.OPEN_TRIP_MAPS_KEY const key = process.env.OPEN_TRIP_MAPS_KEY
/**
* make a GET request to the OTM for a rectangle search.
* @param {string} lon1 Longitude of the 1st point of the box
* @param {string} lat1 Latitude of the 1st point of the box
* @param {string} lon2 Longitude of the 2nd point of the box
* @param {string} lat2 Latitude of the 2nd point of the box
* @returns {FeatureCollection} a list of POIs with their type, id, etc. (cf: [opentripmap](https://dev.opentripmap.org/docs#))
*/
async function callBox(lon1:string, lat1:string, lon2: string, lat2: string) {
const lonMin = Math.min(parseFloat(lon1), parseFloat(lon2))
const lonMax = Math.max(parseFloat(lon1), parseFloat(lon2))
const latMin = Math.min(parseFloat(lat1), parseFloat(lat2))
const latMax = Math.max(parseFloat(lat1), parseFloat(lat2))
const options = {
method: 'GET',
url: 'https://api.opentripmap.com/0.1/en/places/bbox',
params: {
lon_min: lonMin,
lon_max: lonMax,
lat_min: latMin,
lat_max: latMax,
apikey: key,
kinds: 'bars,cafes,pubs,biergartens'
},
headers: {'Content-Type': 'application/json'}
}
try {
const { data } = await axios.request(options)
return data
} catch (error) {
console.error(error)
}
}
/** /**
* make GET request to the OTM for radius search. * make GET request to the OTM for radius search.
* @param {string} lon Longitude of radius center point * @param {string} lon Longitude of radius center point
@ -27,10 +64,10 @@ async function callRadius(lon: string, lat: string, radius = '1000') {
} }
try { try {
const { data } = await axios.request(optionsDrink); const { data } = await axios.request(optionsDrink)
return data return data
} catch (error) { } catch (error) {
console.error(error); console.error(error)
} }
} }
@ -48,13 +85,13 @@ async function callCity(name:string) {
apikey: key apikey: key
}, },
headers: {'Content-Type': 'application/json'} headers: {'Content-Type': 'application/json'}
}; }
try { try {
const { data } = await axios.request(optionsCity); const { data } = await axios.request(optionsCity)
return data return data
} catch (error) { } catch (error) {
console.error(error); console.error(error)
} }
} }
@ -71,13 +108,13 @@ async function callId(id:string) {
apikey: key apikey: key
}, },
headers: {'Content-Type': 'application/json'} headers: {'Content-Type': 'application/json'}
}; }
try { try {
const { data } = await axios.request(optionsId); const { data } = await axios.request(optionsId)
return data return data
} catch (error) { } catch (error) {
console.error(error); console.error(error)
} }
} }
@ -111,7 +148,7 @@ export async function getRadius(req:express.Request, res: express.Response) {
const lat = req.query["lat"] as string const lat = req.query["lat"] as string
let radius = req.query["radius"] let radius = req.query["radius"]
if(!lon || !lat){ if(!lon || !lat){
res.status(400).send("Missing Argument name") res.status(400).send("Missing Argument")
return return
} }
if(!radius){ if(!radius){
@ -135,5 +172,22 @@ export async function getPoiId(req: express.Request, res: express.Response){
res.send( await callId( id as string)) res.send( await callId( id as string))
} }
//TODO: fair une route ou l'on donne l'id un établicement pour avoir des détailles /**
* Handle GET request for radius search route ('/otm/box').
* @param {express.Request} req - HTTP Request object.
* @param {express.Response} res - HTTP Response object.
*/
export async function getBox(req:express.Request, res: express.Response) {
const lon1 = req.query["lon1"] as string
const lat1 = req.query["lat1"] as string
const lon2 = req.query["lon2"] as string
const lat2 = req.query["lat2"] as string
if(!lon1 || !lat1 || !lon2 || !lat2){
res.status(400).send("Missing Argument")
return
}
res.send( await callBox(lon1, lat1, lon2, lat2))
}
//TODO: fair une route ou l'on donne 2 coordonée //TODO: fair une route ou l'on donne 2 coordonée

View File

@ -1,7 +1,7 @@
import request from "supertest"; import request from "supertest"
import app from "../src/app"; import app from "../src/app"
import { Server, IncomingMessage, ServerResponse } from "http"; import { Server, IncomingMessage, ServerResponse } from "http"
import * as dotenv from "dotenv"; import * as dotenv from "dotenv"
dotenv.config({path: '../../.env'}) dotenv.config({path: '../../.env'})
const port = parseInt(process.env.BAR_PORT || '3000') const port = parseInt(process.env.BAR_PORT || '3000')
@ -15,10 +15,10 @@ describe("Test the otm city path", () => {
.get("/otm/city") .get("/otm/city")
.query({'name':'La roche sur yon'}) .query({'name':'La roche sur yon'})
.then(response => { .then(response => {
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200)
done(); done()
}); })
}); })
test("It should response the GET method with content", done => { test("It should response the GET method with content", done => {
const out = { const out = {
@ -50,48 +50,48 @@ describe("Test the otm city path", () => {
.then(response => { .then(response => {
console.log(response.text) console.log(response.text)
expect(response.text).toEqual(JSON.stringify(out)) expect(response.text).toEqual(JSON.stringify(out))
done(); done()
}); })
}); })
test("It should response the 400 code for GET method", done => { test("It should response the 400 code for GET method", done => {
request(app) request(app)
.get("/otm/city") .get("/otm/city")
.then(response => { .then(response => {
console.log(response.text) console.log(response.text)
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400)
done(); done()
}); })
}); })
test("It should response the 200 code for GET method", done => { test("It should response the 200 code for GET method", done => {
request(app) request(app)
.get("/otm/radius") .get("/otm/radius")
.query({'lon':'-1.4344594', 'lat' : '46.6686478', 'radius': '10'}) .query({'lon':'-1.4344594', 'lat' : '46.6686478', 'radius': '10'})
.then(response => { .then(response => {
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200)
done(); done()
}); })
}); })
test("It should response the 200 code for GET method (default radius)", done => { test("It should response the 200 code for GET method (default radius)", done => {
request(app) request(app)
.get("/otm/radius") .get("/otm/radius")
.query({'lon':'-1.4344594', 'lat' : '46.6686478'}) .query({'lon':'-1.4344594', 'lat' : '46.6686478'})
.then(response => { .then(response => {
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200)
done(); done()
}); })
}); })
test("It should response the 400 code for GET method (missing lon and lat)", done => { test("It should response the 400 code for GET method (missing lon and lat)", done => {
request(app) request(app)
.get("/otm/radius") .get("/otm/radius")
.then(response => { .then(response => {
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(400)
done(); done()
}); })
}); })
test("Get bar in radius from API", done => { test("Get bar in radius from API", done => {
@ -121,9 +121,9 @@ describe("Test the otm city path", () => {
.get("/otm/radius") .get("/otm/radius")
.query({'lon':'-1.4344594', 'lat' : '46.6686478', 'radius': '10'}) .query({'lon':'-1.4344594', 'lat' : '46.6686478', 'radius': '10'})
.then(response => { .then(response => {
expect(response.text).toEqual(JSON.stringify(out)); expect(response.text).toEqual(JSON.stringify(out))
done(); done()
}); })
}) })
test("It should response the 200 code for GET method", done => { test("It should response the 200 code for GET method", done => {
@ -131,10 +131,10 @@ describe("Test the otm city path", () => {
.get("/otm/poidetaill") .get("/otm/poidetaill")
.query({'id':'562635'}) .query({'id':'562635'})
.then(response => { .then(response => {
expect(response.statusCode).toBe(200); expect(response.statusCode).toBe(200)
done(); done()
}); })
}); })
test("Get bar in radius from API", done => { test("Get bar in radius from API", done => {
const out = { const out = {
@ -171,17 +171,102 @@ describe("Test the otm city path", () => {
.get("/otm/poidetaill") .get("/otm/poidetaill")
.query({'id':'562635'}) .query({'id':'562635'})
.then(response => { .then(response => {
expect(response.text).toEqual(JSON.stringify(out)); expect(response.text).toEqual(JSON.stringify(out))
done(); done()
}); })
})
test("It should response the 400 code for GET method (no id)", done => {
request(app)
.get("/otm/poidetaill")
.then(response => {
expect(response.statusCode).toBe(400)
done()
})
}) })
test("It should response the 200 code for GET method", done => { test("It should response the 200 code for GET method", done => {
request(app) request(app)
.get("/otm/poidetaill") .get("/otm/box")
.query({'lon1':'-1.435199','lon2':'-1.43519', 'lat1':'46.668460', 'lat2':'46.668461'})
.then(response => { .then(response => {
expect(response.statusCode).toBe(400); expect(response.statusCode).toBe(200)
done(); done()
}); })
}); })
});
test("Get bar in box from API", done => {
const out = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "562633",
"geometry": {
"type": "Point",
"coordinates": [
-1.435197,
46.6684608
]
},
"properties": {
"xid": "N4032296323",
"name": "Le Cube",
"rate": 1,
"osm": "node/4032296323",
"kinds": "foods,cafes,tourist_facilities"
}
}
]
}
request(app)
.get("/otm/box")
.query({'lon1':'-1.435199','lon2':'-1.43519', 'lat1':'46.668460', 'lat2':'46.668461'})
.then(response => {
expect(response.text).toEqual(JSON.stringify(out))
done()
})
})
test("Get bar in box from API lat reversed", done => {
const out = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "562633",
"geometry": {
"type": "Point",
"coordinates": [
-1.435197,
46.6684608
]
},
"properties": {
"xid": "N4032296323",
"name": "Le Cube",
"rate": 1,
"osm": "node/4032296323",
"kinds": "foods,cafes,tourist_facilities"
}
}
]
}
request(app)
.get("/otm/box")
.query({'lon1':'-1.435199','lon2':'-1.43519', 'lat2':'46.668460', 'lat1':'46.668461'})
.then(response => {
expect(response.text).toEqual(JSON.stringify(out))
done()
})
})
test("It should response the 400 code for GET method (no coordonaite)", done => {
request(app)
.get("/otm/box")
.then(response => {
expect(response.statusCode).toBe(400)
done()
})
})
})