Extract ui api requests to common functions
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -37,3 +37,4 @@ yarn-error.log*
|
|||||||
target/
|
target/
|
||||||
dist/
|
dist/
|
||||||
*.env
|
*.env
|
||||||
|
.env.local
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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")]
|
||||||
|
|||||||
@@ -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,18 +25,14 @@ 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`, {
|
bounds: bounds
|
||||||
params: {
|
? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}`
|
||||||
bounds: bounds
|
: undefined,
|
||||||
? `${bounds?.northEast.lat},${bounds?.northEast.lon},${bounds?.southWest.lat},${bounds?.southWest.lon}`
|
category: category ?? undefined,
|
||||||
: undefined,
|
filter: filter ?? undefined,
|
||||||
category: category ?? undefined,
|
limit,
|
||||||
filter: filter ?? undefined,
|
page
|
||||||
limit,
|
});
|
||||||
page
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => console.error(error));
|
|
||||||
return response?.data || { data: [] };
|
return response?.data || { data: [] };
|
||||||
}
|
}
|
||||||
|
|||||||
18
weather-ui/src/app/_api/index.ts
Normal file
18
weather-ui/src/app/_api/index.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -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: [] };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user