Refactor.

This commit is contained in:
BurnyLlama 2022-11-06 15:31:31 +01:00
parent 5943af70e9
commit 7e92d8c5be
3 changed files with 54 additions and 19 deletions

View File

@ -9,7 +9,7 @@ pub struct Map {
pub points: i8, pub points: i8,
pub stars: i8, pub stars: i8,
pub mapper: String, pub mapper: String,
pub timestamp: NaiveDateTime, pub timestamp: NaiveDateTime
} }
/* /*

View File

@ -4,23 +4,37 @@ use std::error::Error;
use mysql::*; use mysql::*;
use mysql::prelude::*; use mysql::prelude::*;
use crate::types; pub mod map;
pub fn get_maps() -> Result<Vec<types::Map>, Box<dyn Error>> { pub struct DatabasePoolStore(pub Pool);
let mut conn = create_pool()?.get_conn()?;
pub fn get_maps(pool: &Pool) -> Result<Vec<map::Map>, Box<dyn Error>> {
let mut conn = pool.get_conn()?;
let maps = conn.query_map( let maps = conn.query_map(
"SELECT * FROM record_maps", "SELECT * FROM record_maps",
|(name, server, points, stars, mapper, timestamp)| types::Map { |(
name, server, points, stars, mapper, timestamp name,
server,
points,
stars,
mapper,
timestamp,
)| map::Map {
name,
server,
points,
stars,
mapper,
timestamp
} }
)?; )?;
Ok(maps) Ok(maps)
} }
pub fn get_map_by_name(name: &str) -> Result<Option<types::Map>, Box<dyn Error>> { pub fn get_map_by_name(pool: &Pool, name: &str) -> Result<Option<map::Map>, Box<dyn Error>> {
let mut conn = create_pool()?.get_conn()?; let mut conn = pool.get_conn()?;
let stmt = conn.prep("SELECT * FROM record_maps WHERE Map = ?")?; let stmt = conn.prep("SELECT * FROM record_maps WHERE Map = ?")?;
let map = conn.exec_first( let map = conn.exec_first(
@ -29,7 +43,7 @@ pub fn get_map_by_name(name: &str) -> Result<Option<types::Map>, Box<dyn Error>>
)?; )?;
Ok(match map { Ok(match map {
Some((name, server, points, stars, mapper, timestamp)) => Some(types::Map { Some((name, server, points, stars, mapper, timestamp)) => Some(map::Map {
name, server, points, stars, mapper, timestamp name, server, points, stars, mapper, timestamp
}), }),
None => None None => None
@ -38,8 +52,22 @@ pub fn get_map_by_name(name: &str) -> Result<Option<types::Map>, Box<dyn Error>>
fn create_pool() -> Result<Pool, Box<dyn Error>> { pub fn create_pool() -> Pool {
let url = env::var("DB_URI")?; let url = match env::var("DB_URI") {
let pool = Pool::new(url.as_str())?; Ok(uri) => uri,
Ok(pool) Err(err) => {
println!("You must provide an env var: 'DB_URI'!");
panic!("{}", err);
}
};
let pool = match Pool::new(url.as_str()) {
Ok(pool) => pool,
Err(err) => {
println!("Couldn't connect to the database!");
panic!("{}", err);
}
};
pool
} }

View File

@ -1,15 +1,19 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use database::create_pool;
use rocket::serde::json::Json; use rocket::serde::json::Json;
use rocket::State;
extern crate dotenv; extern crate dotenv;
use dotenv::dotenv; use dotenv::dotenv;
mod types;
mod database; mod database;
use database::DatabasePoolStore;
use database::map::Map;
#[get("/maps")] #[get("/maps")]
fn get_all_maps() -> Option<Json<Vec<types::Map>>> { fn get_all_maps(db_pool: &State<DatabasePoolStore>) -> Option<Json<Vec<Map>>> {
match database::get_maps() { match database::get_maps(&db_pool.0) {
Ok(maps) => Some(Json(maps)), Ok(maps) => Some(Json(maps)),
Err(err) => { Err(err) => {
println!("{err}"); println!("{err}");
@ -19,8 +23,8 @@ fn get_all_maps() -> Option<Json<Vec<types::Map>>> {
} }
#[get("/maps/<map>")] #[get("/maps/<map>")]
fn get_map_by_name(map: &str) -> Option<Json<types::Map>> { fn get_map_by_name(db_pool: &State<DatabasePoolStore>, map: &str) -> Option<Json<Map>> {
match database::get_map_by_name(map) { match database::get_map_by_name(&db_pool.0, map) {
Ok(map) => match map { Ok(map) => match map {
Some(map) => Some(Json(map)), Some(map) => Some(Json(map)),
None => None None => None
@ -32,10 +36,13 @@ fn get_map_by_name(map: &str) -> Option<Json<types::Map>> {
} }
} }
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
dotenv().ok(); dotenv().ok();
let db_pool = create_pool();
rocket::build() rocket::build()
.manage(DatabasePoolStore(db_pool))
.mount("/", routes![get_all_maps, get_map_by_name]) .mount("/", routes![get_all_maps, get_map_by_name])
} }