diff --git a/.gitignore b/.gitignore index b4cca90..45eb45a 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ yarn-error.log* target/ dist/ *.env +.env.local diff --git a/weather-service/src/airports/model.rs b/weather-service/src/airports/model.rs index af3fd54..0ca1176 100644 --- a/weather-service/src/airports/model.rs +++ b/weather-service/src/airports/model.rs @@ -43,8 +43,16 @@ pub struct QueryAirport { } impl QueryAirport { - pub fn get_all(bounds: Option>, category: Option, filter: Option, limit: i32, page: i32) -> Result, ServiceError> { + pub fn get_all(bounds: Option>, category: Option, filter: Option, limit: Option, page: Option) -> Result, ServiceError> { let mut conn = db::connection()?; + let limit = match limit { + Some(l) => l, + None => 100 + }; + let page = match page { + Some(p) => p, + None => 1 + }; let mut query = airports::table .limit(limit as i64) .into_boxed(); diff --git a/weather-service/src/airports/routes.rs b/weather-service/src/airports/routes.rs index 067636b..d0ec87b 100644 --- a/weather-service/src/airports/routes.rs +++ b/weather-service/src/airports/routes.rs @@ -9,8 +9,8 @@ struct GetAllParameters { filter: Option, bounds: Option, category: Option, - limit: i32, - page: i32 + limit: Option, + page: Option } #[get("/import")] diff --git a/weather-ui/src/app/_api/airport.ts b/weather-ui/src/app/_api/airport.ts index bb5e79e..6116ebb 100644 --- a/weather-ui/src/app/_api/airport.ts +++ b/weather-ui/src/app/_api/airport.ts @@ -1,12 +1,12 @@ -import axios from 'axios'; import { Bounds, GetAirportResponse, GetAirportsResponse } from './airport.types'; +import { getRequest } from '.'; interface GetAirportProps { icao: string; } export async function getAirport({ icao }: GetAirportProps): Promise { - const response = await axios.get(`http://localhost:5000/airports/${icao}`).catch((error) => console.error(error)); + const response = await getRequest(`airports/${icao}`, {}); return response?.data || { data: undefined }; } @@ -25,18 +25,14 @@ export async function getAirports({ limit = 10, page = 1 }: GetAirportsProps): Promise { - const response = await axios - .get(`http://localhost:5000/airports`, { - params: { - bounds: bounds - ? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}` - : undefined, - category: category ?? undefined, - filter: filter ?? undefined, - limit, - page - } - }) - .catch((error) => console.error(error)); + const response = await getRequest('airports', { + bounds: bounds + ? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}` + : undefined, + category: category ?? undefined, + filter: filter ?? undefined, + limit, + page + }); return response?.data || { data: [] }; } diff --git a/weather-ui/src/app/_api/index.ts b/weather-ui/src/app/_api/index.ts new file mode 100644 index 0000000..0b1897a --- /dev/null +++ b/weather-ui/src/app/_api/index.ts @@ -0,0 +1,18 @@ +import axios, { AxiosResponse } from 'axios'; + +const serviceHost = process.env.SERVICE_HOST || 'http://localhost'; +const servicePort = process.env.SERVICE_PORT || 5000; + +export async function getRequest(endpoint: string, params: any): Promise | undefined> { + const response = await axios + .get(`${serviceHost}:${servicePort}/${endpoint}`, { params }) + .catch((error) => console.error(error)); + return response || undefined; +} + +export async function postRequest(endpoint: string, body: any): Promise | undefined> { + const response = await axios + .post(`${serviceHost}:${servicePort}/${endpoint}`, { body }) + .catch((error) => console.error(error)); + return response || undefined; +} diff --git a/weather-ui/src/app/_api/metar.ts b/weather-ui/src/app/_api/metar.ts index d5ea76d..d416e6c 100644 --- a/weather-ui/src/app/_api/metar.ts +++ b/weather-ui/src/app/_api/metar.ts @@ -1,6 +1,6 @@ -import axios from 'axios'; import { Airport } from './airport.types'; import { Metar } from './metar.types'; +import { getRequest } from '.'; interface GetMetarsResponse { data: Metar[]; @@ -11,7 +11,6 @@ export async function getMetars(airports: Airport[]): Promise return { data: [] }; } const stationICAOs: string = airports.map((airport) => airport.icao).join(','); - const url = `http://localhost:5000/metars/${stationICAOs}`; - const response = await axios.get(url).catch((error) => console.error(error)); + const response = await getRequest(`metars/${stationICAOs}`, {}); return response?.data || { data: [] }; }