diff --git a/backend/src/api.rs b/backend/src/api.rs index 92f186e..fc30e6d 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -1,9 +1,10 @@ -use crate::schema::DatabaseError; use crate::schema::gamenight::*; use crate::schema::users::*; +use crate::schema::DatabaseError; use crate::schema::DbConn; use crate::AppConfig; use chrono::Utc; +use futures::future::join_all; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; use rocket::http::Status; use rocket::request::{FromRequest, Outcome, Request}; @@ -13,7 +14,6 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; use uuid::Uuid; use validator::ValidateArgs; -use futures::future::join_all; #[derive(Debug, Responder)] pub enum ApiResponseVariant { @@ -144,26 +144,29 @@ pub struct GamenightOutput { pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant { let gamenights = match get_all_gamenights(&conn).await { Ok(result) => result, - Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))) + Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), }; let conn_ref = &conn; - let game_results : Result, DatabaseError> = join_all(gamenights.iter().map(|gn| async move { - let games = get_games_of_gamenight(conn_ref, gn.id).await?; - let participants = load_participants(conn_ref, gn.id).await?; - Ok(GamenightOutput{ - gamenight: gn.clone(), - game_list: games, - participants: participants, - }) - })).await.into_iter().collect(); - + let game_results: Result, DatabaseError> = + join_all(gamenights.iter().map(|gn| async move { + let games = get_games_of_gamenight(conn_ref, gn.id).await?; + let participants = load_participants(conn_ref, gn.id).await?; + Ok(GamenightOutput { + gamenight: gn.clone(), + game_list: games, + participants: participants, + }) + })) + .await + .into_iter() + .collect(); match game_results { Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))), - Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))) - } + Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), + } } #[get("/gamenights", rank = 2)] @@ -206,12 +209,17 @@ pub async fn gamenights_post_json( Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), }; - let gamenight_id = match insert_gamenight(&conn, gamenight.clone().into(), mutable_game_list).await { + let gamenight_id = match insert_gamenight(&conn, gamenight.clone().into(), mutable_game_list) + .await + { Ok(id) => id, Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), }; - let participant = GamenightParticipantsEntry{gamenight_id: gamenight_id, user_id: user.id}; + let participant = GamenightParticipantsEntry { + gamenight_id: gamenight_id, + user_id: user.id, + }; match insert_participant(&conn, participant).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), @@ -340,8 +348,16 @@ pub async fn games_unauthorized() -> ApiResponseVariant { ApiResponseVariant::Status(Status::Unauthorized) } -#[get("/participants", format = "application/json", data = "")] -pub async fn get_participants(conn: DbConn, _user: User, gamenight_id_json: Json) -> ApiResponseVariant { +#[get( + "/participants", + format = "application/json", + data = "" +)] +pub async fn get_participants( + conn: DbConn, + _user: User, + gamenight_id_json: Json, +) -> ApiResponseVariant { match load_participants(&conn, gamenight_id_json.into_inner().gamenight_id).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), @@ -354,7 +370,11 @@ pub async fn get_participants_unauthorized() -> ApiResponseVariant { } #[post("/participants", format = "application/json", data = "")] -pub async fn post_participants(conn: DbConn, _user: User, entry_json: Json) -> ApiResponseVariant { +pub async fn post_participants( + conn: DbConn, + _user: User, + entry_json: Json, +) -> ApiResponseVariant { match insert_participant(&conn, entry_json.into_inner()).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), @@ -367,7 +387,11 @@ pub async fn post_participants_unauthorized() -> ApiResponseVariant { } #[delete("/participants", format = "application/json", data = "")] -pub async fn delete_participants(conn: DbConn, _user: User, entry_json: Json) -> ApiResponseVariant { +pub async fn delete_participants( + conn: DbConn, + _user: User, + entry_json: Json, +) -> ApiResponseVariant { match remove_participant(&conn, entry_json.into_inner()).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), @@ -378,4 +402,3 @@ pub async fn delete_participants(conn: DbConn, _user: User, entry_json: Json ApiResponseVariant { ApiResponseVariant::Status(Status::Unauthorized) } - diff --git a/backend/src/schema/gamenight.rs b/backend/src/schema/gamenight.rs index 754bd11..3cc1c0a 100644 --- a/backend/src/schema/gamenight.rs +++ b/backend/src/schema/gamenight.rs @@ -87,7 +87,7 @@ pub async fn insert_gamenight( Ok(conn .run(move |c| { c.transaction::<_, DatabaseError, _>(|| { - let id : Uuid = diesel::insert_into(gamenight::table) + let id: Uuid = diesel::insert_into(gamenight::table) .values(&new_gamenight) .returning(gamenight::id) .get_result(c)?; @@ -106,8 +106,8 @@ pub async fn insert_gamenight( Ok(id) }) - }).await? - ) + }) + .await?) } pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result { @@ -126,18 +126,26 @@ pub async fn get_all_known_games(conn: &DbConn) -> Result, DatabaseErr Ok(conn.run(|c| known_games::table.load::(c)).await?) } -pub async fn get_games_of_gamenight(conn: &DbConn, gamenight_id: Uuid) -> Result, DatabaseError> { - Ok(conn.run::<_, Result, _>>(move |c| { - let linked_game_ids: Vec = gamenight_gamelist::table - .filter(gamenight_gamelist::gamenight_id.eq(gamenight_id)) - .load::(c)?; - - linked_game_ids.iter().map(|l| { - known_games::table - .filter(known_games::id.eq(l.game_id)) - .first::(c) - }).collect() - }).await?) +pub async fn get_games_of_gamenight( + conn: &DbConn, + gamenight_id: Uuid, +) -> Result, DatabaseError> { + Ok(conn + .run::<_, Result, _>>(move |c| { + let linked_game_ids: Vec = gamenight_gamelist::table + .filter(gamenight_gamelist::gamenight_id.eq(gamenight_id)) + .load::(c)?; + + linked_game_ids + .iter() + .map(|l| { + known_games::table + .filter(known_games::id.eq(l.game_id)) + .first::(c) + }) + .collect() + }) + .await?) } pub async fn add_game(conn: &DbConn, game: Game) -> Result { @@ -161,17 +169,23 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec) -> Result<( Ok(()) } -pub async fn load_participants(conn: &DbConn, gamenight_id: Uuid) -> Result, DatabaseError> { +pub async fn load_participants( + conn: &DbConn, + gamenight_id: Uuid, +) -> Result, DatabaseError> { Ok(conn .run::<_, Result, _>>(move |c| { let linked_participants = gamenight_participants::table .filter(gamenight_participants::gamenight_id.eq(gamenight_id)) .load::(c)?; - linked_participants.iter().map(|l| { - users::table - .filter(users::id.eq(l.user_id)) - .first::(c) - }).collect() + linked_participants + .iter() + .map(|l| { + users::table + .filter(users::id.eq(l.user_id)) + .first::(c) + }) + .collect() }) .await?) }