Now can access races and teamraces.

This commit is contained in:
BurnyLlama 2023-04-15 22:03:58 +02:00
parent 5f6c3b6c52
commit 9ed1d728da
4 changed files with 51 additions and 0 deletions

View File

@ -1 +1,3 @@
pub mod maps; pub mod maps;
pub mod races;
pub mod teamraces;

18
src/api/races.rs Normal file
View File

@ -0,0 +1,18 @@
use rocket::{serde::json::Json, Route, State};
use crate::database::{models::race::Race, DatabaseHandler};
#[get("/player/<player>")]
async fn get_races_by_player(
db: &State<DatabaseHandler>,
player: &str,
) -> Result<Json<Vec<Race>>, String> {
match Race::get_races_by_player(db, player).await {
Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)),
}
}
pub fn get_all_routes() -> Vec<Route> {
routes![get_races_by_player]
}

29
src/api/teamraces.rs Normal file
View File

@ -0,0 +1,29 @@
use rocket::{serde::json::Json, Route, State};
use crate::database::{models::teamrace::Teamrace, DatabaseHandler};
#[get("/player/<player>")]
async fn get_teamrace_by_player(
db: &State<DatabaseHandler>,
player: &str,
) -> Result<Json<Vec<Teamrace>>, String> {
match Teamrace::get_teamrace_by_player(db, player).await {
Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)),
}
}
#[get("/map/<map>")]
async fn get_teamrace_by_map(
db: &State<DatabaseHandler>,
map: &str,
) -> Result<Json<Vec<Teamrace>>, String> {
match Teamrace::get_teamrace_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_teamrace_by_player, get_teamrace_by_map]
}

View File

@ -19,6 +19,8 @@ async fn main() {
match rocket::build() match rocket::build()
.manage(db) .manage(db)
.mount("/api/v1/maps", api::maps::get_all_routes()) .mount("/api/v1/maps", api::maps::get_all_routes())
.mount("/api/v1/races", api::races::get_all_routes())
.mount("/api/v1/teamraces", api::teamraces::get_all_routes())
.launch() .launch()
.await .await
{ {