Extract ui api requests to common functions

This commit is contained in:
2023-09-17 09:35:03 -04:00
parent 4a3225c3f9
commit 5443ed6d77
6 changed files with 43 additions and 21 deletions

1
.gitignore vendored
View File

@@ -37,3 +37,4 @@ yarn-error.log*
target/ target/
dist/ dist/
*.env *.env
.env.local

View File

@@ -43,8 +43,16 @@ pub struct QueryAirport {
} }
impl QueryAirport { impl QueryAirport {
pub fn get_all(bounds: Option<Polygon<Point>>, category: Option<String>, filter: Option<String>, limit: i32, page: i32) -> Result<Vec<Self>, ServiceError> { pub fn get_all(bounds: Option<Polygon<Point>>, category: Option<String>, filter: Option<String>, limit: Option<i32>, page: Option<i32>) -> Result<Vec<Self>, ServiceError> {
let mut conn = db::connection()?; 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 let mut query = airports::table
.limit(limit as i64) .limit(limit as i64)
.into_boxed(); .into_boxed();

View File

@@ -9,8 +9,8 @@ struct GetAllParameters {
filter: Option<String>, filter: Option<String>,
bounds: Option<String>, bounds: Option<String>,
category: Option<String>, category: Option<String>,
limit: i32, limit: Option<i32>,
page: i32 page: Option<i32>
} }
#[get("/import")] #[get("/import")]

View File

@@ -1,12 +1,12 @@
import axios from 'axios';
import { Bounds, GetAirportResponse, GetAirportsResponse } from './airport.types'; import { Bounds, GetAirportResponse, GetAirportsResponse } from './airport.types';
import { getRequest } from '.';
interface GetAirportProps { interface GetAirportProps {
icao: string; icao: string;
} }
export async function getAirport({ icao }: GetAirportProps): Promise<GetAirportResponse> { export async function getAirport({ icao }: GetAirportProps): Promise<GetAirportResponse> {
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 }; return response?.data || { data: undefined };
} }
@@ -25,9 +25,7 @@ export async function getAirports({
limit = 10, limit = 10,
page = 1 page = 1
}: GetAirportsProps): Promise<GetAirportsResponse> { }: GetAirportsProps): Promise<GetAirportsResponse> {
const response = await axios const response = await getRequest('airports', {
.get(`http://localhost:5000/airports`, {
params: {
bounds: bounds bounds: bounds
? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}` ? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}`
: undefined, : undefined,
@@ -35,8 +33,6 @@ export async function getAirports({
filter: filter ?? undefined, filter: filter ?? undefined,
limit, limit,
page page
} });
})
.catch((error) => console.error(error));
return response?.data || { data: [] }; return response?.data || { data: [] };
} }

View File

@@ -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<AxiosResponse<any, any> | 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<AxiosResponse<any, any> | undefined> {
const response = await axios
.post(`${serviceHost}:${servicePort}/${endpoint}`, { body })
.catch((error) => console.error(error));
return response || undefined;
}

View File

@@ -1,6 +1,6 @@
import axios from 'axios';
import { Airport } from './airport.types'; import { Airport } from './airport.types';
import { Metar } from './metar.types'; import { Metar } from './metar.types';
import { getRequest } from '.';
interface GetMetarsResponse { interface GetMetarsResponse {
data: Metar[]; data: Metar[];
@@ -11,7 +11,6 @@ export async function getMetars(airports: Airport[]): Promise<GetMetarsResponse>
return { data: [] }; return { data: [] };
} }
const stationICAOs: string = airports.map((airport) => airport.icao).join(','); const stationICAOs: string = airports.map((airport) => airport.icao).join(',');
const url = `http://localhost:5000/metars/${stationICAOs}`; const response = await getRequest(`metars/${stationICAOs}`, {});
const response = await axios.get(url).catch((error) => console.error(error));
return response?.data || { data: [] }; return response?.data || { data: [] };
} }