Updated api endpoint structure, fixed issues, worked on admin page
This commit is contained in:
@@ -27,8 +27,7 @@ pub struct InsertAirport {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct QueryFilters {
|
||||
pub name: Option<String>,
|
||||
pub icao: Option<String>,
|
||||
pub search: Option<String>,
|
||||
pub bounds: Option<Polygon<Point>>,
|
||||
pub category: Option<String>,
|
||||
pub order_field: Option<QueryOrderField>,
|
||||
@@ -38,8 +37,7 @@ pub struct QueryFilters {
|
||||
impl Default for QueryFilters {
|
||||
fn default() -> Self {
|
||||
QueryFilters {
|
||||
name: None,
|
||||
icao: None,
|
||||
search: None,
|
||||
bounds: None,
|
||||
category: None,
|
||||
order_field: None,
|
||||
@@ -131,16 +129,17 @@ impl QueryAirport {
|
||||
if let Some(category) = &filters.category {
|
||||
query = query.filter(airports::category.eq(category));
|
||||
}
|
||||
if let Some(icao) = &filters.icao {
|
||||
if let Some(name) = &filters.name {
|
||||
query = query.filter(
|
||||
airports::icao.ilike(format!("%{}%", icao)).or(
|
||||
airports::full_name.ilike(format!("%{}%", name))
|
||||
)
|
||||
)
|
||||
} else {
|
||||
query = query.filter(airports::icao.ilike(format!("%{}%", icao)))
|
||||
}
|
||||
if let Some(search) = &filters.search {
|
||||
query = query.filter(
|
||||
airports::icao.ilike(format!("%{}%", search))
|
||||
.or(airports::full_name.ilike(format!("%{}%", search)))
|
||||
.or(airports::iso_country.ilike(format!("%{}%", search)))
|
||||
.or(airports::iso_region.ilike(format!("%{}%", search)))
|
||||
.or(airports::municipality.ilike(format!("%{}%", search)))
|
||||
.or(airports::gps_code.ilike(format!("%{}%", search)))
|
||||
.or(airports::iata_code.ilike(format!("%{}%", search)))
|
||||
.or(airports::local_code.ilike(format!("%{}%", search)))
|
||||
)
|
||||
}
|
||||
|
||||
if let Some(order_by) = &filters.order_by {
|
||||
@@ -193,16 +192,17 @@ impl QueryAirport {
|
||||
if let Some(category) = &filters.category {
|
||||
query = query.filter(airports::category.eq(category));
|
||||
}
|
||||
if let Some(icao) = &filters.icao {
|
||||
if let Some(name) = &filters.name {
|
||||
query = query.filter(
|
||||
airports::icao.ilike(format!("%{}%", icao)).or(
|
||||
airports::full_name.ilike(format!("%{}%", name))
|
||||
)
|
||||
)
|
||||
} else {
|
||||
query = query.filter(airports::icao.ilike(format!("%{}%", icao)))
|
||||
}
|
||||
if let Some(search) = &filters.search {
|
||||
query = query.filter(
|
||||
airports::icao.ilike(format!("%{}%", search))
|
||||
.or(airports::full_name.ilike(format!("%{}%", search)))
|
||||
.or(airports::iso_country.ilike(format!("%{}%", search)))
|
||||
.or(airports::iso_region.ilike(format!("%{}%", search)))
|
||||
.or(airports::municipality.ilike(format!("%{}%", search)))
|
||||
.or(airports::gps_code.ilike(format!("%{}%", search)))
|
||||
.or(airports::iata_code.ilike(format!("%{}%", search)))
|
||||
.or(airports::local_code.ilike(format!("%{}%", search)))
|
||||
)
|
||||
}
|
||||
|
||||
let count: i64 = query.get_result(&mut conn)?;
|
||||
@@ -224,19 +224,19 @@ impl QueryAirport {
|
||||
Ok(airport)
|
||||
}
|
||||
|
||||
pub fn update(id: i32, airport: InsertAirport) -> Result<Self, ServiceError> {
|
||||
pub fn update(icao: String, airport: InsertAirport) -> Result<Self, ServiceError> {
|
||||
let mut conn = db::connection()?;
|
||||
let airport = diesel::update(airports::table)
|
||||
.filter(airports::id.eq(id))
|
||||
.filter(airports::icao.eq(icao))
|
||||
.set(airport)
|
||||
.get_result(&mut conn)?;
|
||||
Ok(airport)
|
||||
}
|
||||
|
||||
pub fn delete(id: Option<i32>) -> Result<usize, ServiceError> {
|
||||
pub fn delete(icao: Option<String>) -> Result<usize, ServiceError> {
|
||||
let mut conn = db::connection()?;
|
||||
let res = match id {
|
||||
Some(id) => diesel::delete(airports::table.filter(airports::id.eq(id))).execute(&mut conn)?,
|
||||
let res = match icao {
|
||||
Some(icao) => diesel::delete(airports::table.filter(airports::icao.eq(icao))).execute(&mut conn)?,
|
||||
None => diesel::delete(airports::table).execute(&mut conn)?
|
||||
};
|
||||
Ok(res)
|
||||
|
||||
@@ -8,8 +8,7 @@ use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct GetAllParameters {
|
||||
name: Option<String>,
|
||||
icao: Option<String>,
|
||||
search: Option<String>,
|
||||
bounds: Option<String>,
|
||||
category: Option<String>,
|
||||
order_field: Option<String>,
|
||||
@@ -18,7 +17,7 @@ struct GetAllParameters {
|
||||
page: Option<i32>
|
||||
}
|
||||
|
||||
#[get("/import")]
|
||||
#[post("/import")]
|
||||
async fn import(auth: JwtAuth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
Ok(_) => {},
|
||||
@@ -31,12 +30,11 @@ async fn import(auth: JwtAuth) -> HttpResponse {
|
||||
})
|
||||
}
|
||||
|
||||
#[get("/search")]
|
||||
#[get("")]
|
||||
async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
let params = web::Query::<GetAllParameters>::from_query(req.query_string()).unwrap();
|
||||
let mut filters = QueryFilters::default();
|
||||
filters.name = params.name.clone();
|
||||
filters.icao = params.icao.clone();
|
||||
filters.search = params.search.clone();
|
||||
filters.category = params.category.clone();
|
||||
filters.bounds = match ¶ms.bounds {
|
||||
Some(b) => {
|
||||
@@ -119,7 +117,7 @@ async fn get_all(req: HttpRequest) -> HttpResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/search/{icao}")]
|
||||
#[get("/{icao}")]
|
||||
async fn get(icao: web::Path<String>) -> HttpResponse {
|
||||
match QueryAirport::find(icao.into_inner()) {
|
||||
Ok(a) => HttpResponse::Ok().json(Response {
|
||||
@@ -133,7 +131,7 @@ async fn get(icao: web::Path<String>) -> HttpResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/create")]
|
||||
#[post("")]
|
||||
async fn create(airport: web::Json<InsertAirport>, auth: JwtAuth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
Ok(_) => {},
|
||||
@@ -148,8 +146,8 @@ async fn create(airport: web::Json<InsertAirport>, auth: JwtAuth) -> HttpRespons
|
||||
}
|
||||
}
|
||||
|
||||
#[put("/update/{icao}")]
|
||||
async fn update(icao: web::Path<i32>, airport: web::Json<InsertAirport>, auth: JwtAuth) -> HttpResponse {
|
||||
#[put("/{icao}")]
|
||||
async fn update(icao: web::Path<String>, airport: web::Json<InsertAirport>, auth: JwtAuth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
Ok(_) => {},
|
||||
Err(err) => return ResponseError::error_response(&err)
|
||||
@@ -163,8 +161,8 @@ async fn update(icao: web::Path<i32>, airport: web::Json<InsertAirport>, auth: J
|
||||
}
|
||||
}
|
||||
|
||||
#[delete("/remove")]
|
||||
async fn remove_all(auth: JwtAuth) -> HttpResponse {
|
||||
#[delete("")]
|
||||
async fn delete_all(auth: JwtAuth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
Ok(_) => {},
|
||||
Err(err) => return ResponseError::error_response(&err)
|
||||
@@ -178,8 +176,8 @@ async fn remove_all(auth: JwtAuth) -> HttpResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[delete("/remove/{icao}")]
|
||||
async fn remove(icao: web::Path<i32>, auth: JwtAuth) -> HttpResponse {
|
||||
#[delete("/{icao}")]
|
||||
async fn delete(icao: web::Path<String>, auth: JwtAuth) -> HttpResponse {
|
||||
let _ = match verify_role(&auth, "admin") {
|
||||
Ok(_) => {},
|
||||
Err(err) => return ResponseError::error_response(&err)
|
||||
@@ -199,8 +197,8 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
.service(get)
|
||||
.service(create)
|
||||
.service(update)
|
||||
.service(remove)
|
||||
.service(remove_all)
|
||||
.service(delete)
|
||||
.service(delete_all)
|
||||
.service(import)
|
||||
);
|
||||
}
|
||||
@@ -275,18 +275,19 @@ impl Metar {
|
||||
return insert_metars;
|
||||
}
|
||||
|
||||
pub async fn get_all(icaos: String) -> Result<Vec<Self>, ServiceError> {
|
||||
if icaos.is_empty() {
|
||||
pub async fn get_all(icao_string: String) -> Result<Vec<Self>, ServiceError> {
|
||||
if icao_string.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let station_icaos: Vec<&str> = icaos.split(',').collect();
|
||||
let mut db_metars = match QueryMetar::get_all(&station_icaos) {
|
||||
let icaos: Vec<&str> = icao_string.split(",").collect();
|
||||
|
||||
let mut db_metars = match QueryMetar::get_all(&icaos) {
|
||||
Ok(m) => Self::from_query(m),
|
||||
Err(err) => return Err(err)
|
||||
};
|
||||
|
||||
let missing_icaos = Self::get_missing_metar_icaos(&db_metars, &station_icaos);
|
||||
let missing_icaos = Self::get_missing_metar_icaos(&db_metars, &icaos);
|
||||
if missing_icaos.is_empty() {
|
||||
return Ok(db_metars);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{error_handler::ServiceError, db::Metadata};
|
||||
use crate::metars::Metar;
|
||||
use actix_web::{get, web, HttpResponse};
|
||||
use actix_web::{get, web, HttpResponse, HttpRequest};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -10,23 +10,35 @@ pub struct MetarsResponse {
|
||||
pub meta: Metadata
|
||||
}
|
||||
|
||||
#[get("metars/{ids}")]
|
||||
async fn get_all(ids: web::Path<String>) -> HttpResponse {
|
||||
let airports = match web::block(|| Ok::<_, ServiceError>(async {Metar::get_all(ids.into_inner()).await}))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
return err.to_http_response();
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(MetarsResponse {
|
||||
data: airports,
|
||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
||||
})
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct GetAllParameters {
|
||||
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();
|
||||
let icao_string = match icao_option {
|
||||
Some(i) => i,
|
||||
None => return HttpResponse::UnprocessableEntity().body("Missing icaos parameter")
|
||||
};
|
||||
|
||||
let airports = match web::block(|| Ok::<_, ServiceError>(async {Metar::get_all(icao_string).await}))
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.await {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
return err.to_http_response();
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(MetarsResponse {
|
||||
data: airports,
|
||||
meta: Metadata { page: 0, limit: 0, pages: 0, total: 0 }
|
||||
})
|
||||
}
|
||||
|
||||
pub fn init_routes(config: &mut web::ServiceConfig) {
|
||||
|
||||
Reference in New Issue
Block a user