Added some documentation....
This commit is contained in:
parent
56d450bda6
commit
7411bd4430
|
@ -4,6 +4,8 @@ use std::{env, error::Error};
|
||||||
|
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
|
/// DatabaseHandler is a struct that can hold a database connection pool.
|
||||||
|
/// TODO: This might actually be an unnecessary abstraction, however it is 0-cost I believe...
|
||||||
pub struct DatabaseHandler {
|
pub struct DatabaseHandler {
|
||||||
pub pool: PgPool,
|
pub pool: PgPool,
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,18 +17,24 @@ pub struct Map {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
|
/// This gets all maps in the map database.
|
||||||
pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> {
|
pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> {
|
||||||
sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql")
|
sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql")
|
||||||
.fetch_all(&db.pool)
|
.fetch_all(&db.pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This gets a map from the database via its name.
|
||||||
|
/// Returns an Err<sqlx::Error> if no map exists with the given name.
|
||||||
pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result<Map, sqlx::Error> {
|
pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result<Map, sqlx::Error> {
|
||||||
sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map)
|
sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map)
|
||||||
.fetch_one(&db.pool)
|
.fetch_one(&db.pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This gets all maps from the database via their category.
|
||||||
|
/// Valid values are: Novice, Moderate, Brutal, Insane, Dummy, DDmaX.Easy, DDmaX.Next, DDmaX.Pro, DDmaX.Nut, Oldschool, Solo, Race, Fun
|
||||||
|
/// FIXME: Returns an empty array on invalid category!
|
||||||
pub async fn get_maps_by_category(
|
pub async fn get_maps_by_category(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
category: &str,
|
category: &str,
|
||||||
|
@ -38,6 +44,9 @@ impl Map {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This gets all the maps in the database made by a specific mapper.
|
||||||
|
/// Mappers are stored as an array, so if that array contains the given mapper, the entry is returned.
|
||||||
|
/// FIXME: Returns an empty array on non-existant mapper!
|
||||||
pub async fn get_maps_by_mapper(
|
pub async fn get_maps_by_mapper(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
mapper: &str,
|
mapper: &str,
|
||||||
|
|
|
@ -18,6 +18,9 @@ pub struct Race {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Race {
|
impl Race {
|
||||||
|
/// This gets all the races from the database by a given player.
|
||||||
|
/// It is paginated into a SearchResult<Race>!
|
||||||
|
/// FIXME: Returns empty array for non-existent player.
|
||||||
pub async fn get_races_by_player(
|
pub async fn get_races_by_player(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
player: &str,
|
player: &str,
|
||||||
|
@ -50,6 +53,9 @@ impl Race {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This gets all the races from the database on a specific map.
|
||||||
|
/// It is paginated into a SearchResult<Race>!
|
||||||
|
/// FIXME: Returns empty array for non-existent map.
|
||||||
pub async fn get_races_by_map(
|
pub async fn get_races_by_map(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
map: &str,
|
map: &str,
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// This is a generic struct that represents a page of content that has been paginated.
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct SearchResult<T> {
|
pub struct SearchResult<T> {
|
||||||
pub results: Vec<T>,
|
pub results: Vec<T>,
|
||||||
|
@ -10,7 +11,9 @@ pub struct SearchResult<T> {
|
||||||
/// This constant should be used everywhere so that it can easily changed.
|
/// This constant should be used everywhere so that it can easily changed.
|
||||||
pub const ROWS_PER_PAGE: i32 = 200;
|
pub const ROWS_PER_PAGE: i32 = 200;
|
||||||
|
|
||||||
/// This gets an i32 from an Option<&str>. It is very fail safe of any bad value, where it will return 1 instead of an error.
|
/// This gets an i32 from an Option<&str>.
|
||||||
|
/// It is fail safe: for any "bad" value, it will return 1 instead of an error.
|
||||||
|
/// TODO: Maybe this should be an error instead...
|
||||||
pub fn get_page_number_as_i32_from_str(page_as_str: Option<&str>) -> i32 {
|
pub fn get_page_number_as_i32_from_str(page_as_str: Option<&str>) -> i32 {
|
||||||
str::parse(page_as_str.unwrap_or("1")).unwrap_or(1)
|
str::parse(page_as_str.unwrap_or("1")).unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,9 @@ pub struct Teamrace {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Teamrace {
|
impl Teamrace {
|
||||||
|
/// This gets all the teamraces from the database a given player has partaken in.
|
||||||
|
/// It is paginated into a SearchResult<Teamrace>!
|
||||||
|
/// FIXME: Returns empty array for non-existent player.
|
||||||
pub async fn get_teamraces_by_player(
|
pub async fn get_teamraces_by_player(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
player: &str,
|
player: &str,
|
||||||
|
@ -46,6 +49,9 @@ impl Teamrace {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This gets all the teamraces from the database on a specific map.
|
||||||
|
/// It is paginated into a SearchResult<Teamrace>!
|
||||||
|
/// FIXME: Returns empty array for non-existent map.
|
||||||
pub async fn get_teamraces_by_map(
|
pub async fn get_teamraces_by_map(
|
||||||
db: &DatabaseHandler,
|
db: &DatabaseHandler,
|
||||||
map: &str,
|
map: &str,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user