Overhaul refactor. Still things in progress
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,45 +1,34 @@
|
||||
use crate::{error::ApiError, db::Metadata};
|
||||
use crate::error::Error;
|
||||
use crate::metars::Metar;
|
||||
use actix_web::{get, web, HttpResponse, HttpRequest};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MetarsResponse {
|
||||
pub data: Vec<Metar>,
|
||||
pub meta: Metadata,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct GetAllParameters {
|
||||
struct FindAllParameters {
|
||||
icaos: Option<String>,
|
||||
}
|
||||
|
||||
#[get("metars")]
|
||||
async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let icao_option = params.icaos.clone();
|
||||
async fn find_all(req: HttpRequest) -> HttpResponse {
|
||||
let parameters = web::Query::<FindAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let icao_option = ¶meters.icaos;
|
||||
let icao_string = match icao_option {
|
||||
Some(i) => i,
|
||||
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter"),
|
||||
};
|
||||
let icaos: Vec<&str> = icao_string.split(',').collect();
|
||||
|
||||
let metars =
|
||||
match web::block(|| Ok::<_, ApiError>(async { Metar::get_all(icao_string).await }))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.await
|
||||
{
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
return err.to_http_response();
|
||||
}
|
||||
};
|
||||
let metars = match Metar::find_all(&icaos).await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
return err.to_http_response();
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(metars)
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
config.service(get_all);
|
||||
config.service(find_all);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user