Added way to get races by map.

main
BurnyLlama 2023-04-15 22:08:04 +02:00
parent 9ed1d728da
commit 71ed4d8b70
2 changed files with 42 additions and 2 deletions

View File

@ -13,6 +13,17 @@ async fn get_races_by_player(
}
}
pub fn get_all_routes() -> Vec<Route> {
routes![get_races_by_player]
#[get("/map/<map>")]
async fn get_races_by_map(
db: &State<DatabaseHandler>,
map: &str,
) -> Result<Json<Vec<Race>>, String> {
match Race::get_races_by_map(db, map).await {
Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)),
}
}
pub fn get_all_routes() -> Vec<Route> {
routes![get_races_by_player, get_races_by_map]
}

View File

@ -44,4 +44,33 @@ impl Race {
.fetch_all(&db.pool)
.await
}
pub async fn get_races_by_map(
db: &DatabaseHandler,
map: &str,
) -> Result<Vec<Race>, sqlx::Error> {
sqlx::query_as!(
Race,
"
SELECT name,
map,
time,
timestamp,
server,
ARRAY[cp1, cp2, cp3, cp4, cp5,
cp6, cp7, cp8, cp9, cp10,
cp11, cp12, cp13, cp14, cp15,
cp16, cp17, cp18, cp19, cp20,
cp21, cp22, cp23, cp24, cp25
] AS checkpoints,
gameid,
ddnet7
FROM record_race WHERE map = $1
ORDER BY map, time
",
map
)
.fetch_all(&db.pool)
.await
}
}