Added some documentation....

main
BurnyLlama 2023-04-16 14:36:42 +02:00
parent 56d450bda6
commit 7411bd4430
5 changed files with 27 additions and 1 deletions

View File

@ -4,6 +4,8 @@ use std::{env, error::Error};
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 pool: PgPool,
}

View File

@ -17,18 +17,24 @@ pub struct Map {
}
impl Map {
/// This gets all maps in the map database.
pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> {
sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql")
.fetch_all(&db.pool)
.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> {
sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map)
.fetch_one(&db.pool)
.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(
db: &DatabaseHandler,
category: &str,
@ -38,6 +44,9 @@ impl Map {
.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(
db: &DatabaseHandler,
mapper: &str,

View File

@ -18,6 +18,9 @@ pub struct 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(
db: &DatabaseHandler,
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(
db: &DatabaseHandler,
map: &str,

View File

@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
/// This is a generic struct that represents a page of content that has been paginated.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SearchResult<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.
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 {
str::parse(page_as_str.unwrap_or("1")).unwrap_or(1)
}

View File

@ -14,6 +14,9 @@ pub struct 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(
db: &DatabaseHandler,
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(
db: &DatabaseHandler,
map: &str,