Implemented default airports, fixed loading airport endpoints

This commit is contained in:
2023-09-09 21:54:44 -04:00
parent 17b76ade1f
commit c9699c16c3
19 changed files with 1138496 additions and 84 deletions

View File

@@ -7,8 +7,17 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, AsChangeset, Insertable)]
#[table_name = "airports"]
pub struct Airport {
pub full_name: String,
pub icao: String,
pub category: String,
pub full_name: String,
pub elevation_ft: Option<i32>,
pub continent: String,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub gps_code: String,
pub iata_code: String,
pub local_code: String,
pub latitude: f64,
pub longitude: f64,
}
@@ -16,16 +25,28 @@ pub struct Airport {
#[derive(Serialize, Deserialize, Queryable)]
pub struct Airports {
pub id: i32,
pub full_name: String,
pub icao: String,
pub category: String,
pub full_name: String,
pub elevation_ft: Option<i32>,
pub continent: String,
pub iso_country: String,
pub iso_region: String,
pub municipality: String,
pub gps_code: String,
pub iata_code: String,
pub local_code: String,
pub latitude: f64,
pub longitude: f64,
}
impl Airports {
pub fn find_all() -> Result<Vec<Self>, CustomError> {
pub fn find_all(limit: i32, page: i32) -> Result<Vec<Self>, CustomError> {
let conn = db::connection()?;
let airports = airports::table.load::<Airports>(&conn)?;
let airports = airports::table
.limit(limit as i64)
.filter(airports::id.gt(page * limit))
.load::<Airports>(&conn)?;
Ok(airports)
}

View File

@@ -1,11 +1,19 @@
use crate::airports::{Airport, Airports};
use actix_web::{delete, get, post, put, web, HttpResponse};
use actix_web::{delete, get, post, put, web, HttpResponse, HttpRequest};
use log::error;
use serde::{Serialize, Deserialize};
use serde_json::json;
#[derive(Debug, Serialize, Deserialize)]
struct FindAllParams {
limit: i32,
page: i32
}
#[get("/airports")]
async fn find_all() -> HttpResponse {
match web::block(|| Airports::find_all()).await.unwrap() {
async fn find_all(req: HttpRequest) -> HttpResponse {
let params = web::Query::<FindAllParams>::from_query(req.query_string()).unwrap();
match web::block(move || Airports::find_all(params.limit, params.page)).await.unwrap() {
Ok(a) => HttpResponse::Ok().json(a),
Err(err) => {
error!("{}", err);

View File

@@ -1,8 +1,8 @@
use crate::error_handler::CustomError;
use crate::{error_handler::CustomError, airports::{Airport, Airports}};
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use lazy_static::lazy_static;
use log::{error, info};
use log::{error, info, debug};
use r2d2;
use std::env;
@@ -12,26 +12,38 @@ pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
diesel_migrations::embed_migrations!();
lazy_static! {
static ref POOL: Pool = {
let username = env::var("DATABASE_USER").expect("Database username is not set");
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
let name = env::var("DATABASE_NAME").expect("Database name is not set");
let url = format!("postgres://{}:{}@localhost:5433/{}", username, password, name);
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::new(manager).expect("Failed to create db pool")
};
static ref POOL: Pool = {
let username = env::var("DATABASE_USER").expect("Database username is not set");
let password = env::var("DATABASE_PASSWORD").expect("Database password is not set");
let name = env::var("DATABASE_NAME").expect("Database name is not set");
let url = format!("postgres://{}:{}@localhost:5433/{}", username, password, name);
let manager = ConnectionManager::<PgConnection>::new(url);
Pool::new(manager).expect("Failed to create db pool")
};
}
pub fn init() {
lazy_static::initialize(&POOL);
let conn = connection().expect("Failed to get db connection");
match embedded_migrations::run(&conn) {
Ok(_) => info!("Database initialized"),
Err(err) => error!("Failed to initialize database; {}", err),
};
lazy_static::initialize(&POOL);
let conn = connection().expect("Failed to get db connection");
match embedded_migrations::run(&conn) {
Ok(_) => info!("Database initialized"),
Err(err) => error!("Failed to initialize database; {}", err),
};
}
pub fn connection() -> Result<DbConnection, CustomError> {
POOL.get()
.map_err(|e| CustomError::new(500, format!("Failed getting db connection: {}", e)))
POOL.get()
.map_err(|e| CustomError::new(500, format!("Failed getting db connection: {}", e)))
}
pub fn import_data() {
let contents: String = std::fs::read_to_string("airport-codes.json").expect("Failed to read file");
let airports: Vec<Airport> = serde_json::from_str(&contents).expect("JSON was not well formed.");
for airport in airports {
match Airports::create(airport) {
Ok(_) => {},
Err(err) => error!("Error inserting airport; {}", err)
};
}
debug!("Imported data");
}

View File

@@ -1,8 +1,17 @@
diesel::table! {
airports (id) {
id -> Integer,
full_name -> Text,
icao -> Text,
category -> Text,
full_name -> Text,
elevation_ft -> Nullable<Integer>,
continent -> Text,
iso_country -> Text,
iso_region -> Text,
municipality -> Text,
gps_code -> Text,
iata_code -> Text,
local_code -> Text,
latitude -> Double,
longitude -> Double,
}