Formatting commit

This commit is contained in:
Dennis Brentjes 2022-05-29 10:33:55 +02:00
parent 639405bf9f
commit 102a3e6082
2 changed files with 80 additions and 43 deletions

View File

@ -1,9 +1,10 @@
use crate::schema::DatabaseError;
use crate::schema::gamenight::*; use crate::schema::gamenight::*;
use crate::schema::users::*; use crate::schema::users::*;
use crate::schema::DatabaseError;
use crate::schema::DbConn; use crate::schema::DbConn;
use crate::AppConfig; use crate::AppConfig;
use chrono::Utc; use chrono::Utc;
use futures::future::join_all;
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
use rocket::http::Status; use rocket::http::Status;
use rocket::request::{FromRequest, Outcome, Request}; use rocket::request::{FromRequest, Outcome, Request};
@ -13,7 +14,6 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow; use std::borrow::Cow;
use uuid::Uuid; use uuid::Uuid;
use validator::ValidateArgs; use validator::ValidateArgs;
use futures::future::join_all;
#[derive(Debug, Responder)] #[derive(Debug, Responder)]
pub enum ApiResponseVariant { pub enum ApiResponseVariant {
@ -144,25 +144,28 @@ pub struct GamenightOutput {
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant { pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
let gamenights = match get_all_gamenights(&conn).await { let gamenights = match get_all_gamenights(&conn).await {
Ok(result) => result, 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 conn_ref = &conn;
let game_results : Result<Vec<GamenightOutput>, DatabaseError> = join_all(gamenights.iter().map(|gn| async move { let game_results: Result<Vec<GamenightOutput>, DatabaseError> =
join_all(gamenights.iter().map(|gn| async move {
let games = get_games_of_gamenight(conn_ref, gn.id).await?; let games = get_games_of_gamenight(conn_ref, gn.id).await?;
let participants = load_participants(conn_ref, gn.id).await?; let participants = load_participants(conn_ref, gn.id).await?;
Ok(GamenightOutput{ Ok(GamenightOutput {
gamenight: gn.clone(), gamenight: gn.clone(),
game_list: games, game_list: games,
participants: participants, participants: participants,
}) })
})).await.into_iter().collect(); }))
.await
.into_iter()
.collect();
match game_results { match game_results {
Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))), 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()))),
} }
} }
@ -206,12 +209,17 @@ pub async fn gamenights_post_json(
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), 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, Ok(id) => id,
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), 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 { match insert_participant(&conn, participant).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
@ -340,8 +348,16 @@ pub async fn games_unauthorized() -> ApiResponseVariant {
ApiResponseVariant::Status(Status::Unauthorized) ApiResponseVariant::Status(Status::Unauthorized)
} }
#[get("/participants", format = "application/json", data = "<gamenight_id_json>")] #[get(
pub async fn get_participants(conn: DbConn, _user: User, gamenight_id_json: Json<GamenightId>) -> ApiResponseVariant { "/participants",
format = "application/json",
data = "<gamenight_id_json>"
)]
pub async fn get_participants(
conn: DbConn,
_user: User,
gamenight_id_json: Json<GamenightId>,
) -> ApiResponseVariant {
match load_participants(&conn, gamenight_id_json.into_inner().gamenight_id).await { match load_participants(&conn, gamenight_id_json.into_inner().gamenight_id).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), 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 = "<entry_json>")] #[post("/participants", format = "application/json", data = "<entry_json>")]
pub async fn post_participants(conn: DbConn, _user: User, entry_json: Json<GamenightParticipantsEntry>) -> ApiResponseVariant { pub async fn post_participants(
conn: DbConn,
_user: User,
entry_json: Json<GamenightParticipantsEntry>,
) -> ApiResponseVariant {
match insert_participant(&conn, entry_json.into_inner()).await { match insert_participant(&conn, entry_json.into_inner()).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), 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 = "<entry_json>")] #[delete("/participants", format = "application/json", data = "<entry_json>")]
pub async fn delete_participants(conn: DbConn, _user: User, entry_json: Json<GamenightParticipantsEntry>) -> ApiResponseVariant { pub async fn delete_participants(
conn: DbConn,
_user: User,
entry_json: Json<GamenightParticipantsEntry>,
) -> ApiResponseVariant {
match remove_participant(&conn, entry_json.into_inner()).await { match remove_participant(&conn, entry_json.into_inner()).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), 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<Gam
pub async fn delete_participants_unauthorized() -> ApiResponseVariant { pub async fn delete_participants_unauthorized() -> ApiResponseVariant {
ApiResponseVariant::Status(Status::Unauthorized) ApiResponseVariant::Status(Status::Unauthorized)
} }

View File

@ -87,7 +87,7 @@ pub async fn insert_gamenight(
Ok(conn Ok(conn
.run(move |c| { .run(move |c| {
c.transaction::<_, DatabaseError, _>(|| { c.transaction::<_, DatabaseError, _>(|| {
let id : Uuid = diesel::insert_into(gamenight::table) let id: Uuid = diesel::insert_into(gamenight::table)
.values(&new_gamenight) .values(&new_gamenight)
.returning(gamenight::id) .returning(gamenight::id)
.get_result(c)?; .get_result(c)?;
@ -106,8 +106,8 @@ pub async fn insert_gamenight(
Ok(id) Ok(id)
}) })
}).await? })
) .await?)
} }
pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<Gamenight, DatabaseError> { pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<Gamenight, DatabaseError> {
@ -126,18 +126,26 @@ pub async fn get_all_known_games(conn: &DbConn) -> Result<Vec<Game>, DatabaseErr
Ok(conn.run(|c| known_games::table.load::<Game>(c)).await?) Ok(conn.run(|c| known_games::table.load::<Game>(c)).await?)
} }
pub async fn get_games_of_gamenight(conn: &DbConn, gamenight_id: Uuid) -> Result<Vec<Game>, DatabaseError> { pub async fn get_games_of_gamenight(
Ok(conn.run::<_, Result<Vec<Game>, _>>(move |c| { conn: &DbConn,
gamenight_id: Uuid,
) -> Result<Vec<Game>, DatabaseError> {
Ok(conn
.run::<_, Result<Vec<Game>, _>>(move |c| {
let linked_game_ids: Vec<GamenightGameListEntry> = gamenight_gamelist::table let linked_game_ids: Vec<GamenightGameListEntry> = gamenight_gamelist::table
.filter(gamenight_gamelist::gamenight_id.eq(gamenight_id)) .filter(gamenight_gamelist::gamenight_id.eq(gamenight_id))
.load::<GamenightGameListEntry>(c)?; .load::<GamenightGameListEntry>(c)?;
linked_game_ids.iter().map(|l| { linked_game_ids
.iter()
.map(|l| {
known_games::table known_games::table
.filter(known_games::id.eq(l.game_id)) .filter(known_games::id.eq(l.game_id))
.first::<Game>(c) .first::<Game>(c)
}).collect() })
}).await?) .collect()
})
.await?)
} }
pub async fn add_game(conn: &DbConn, game: Game) -> Result<usize, DatabaseError> { pub async fn add_game(conn: &DbConn, game: Game) -> Result<usize, DatabaseError> {
@ -161,17 +169,23 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec<Game>) -> Result<(
Ok(()) Ok(())
} }
pub async fn load_participants(conn: &DbConn, gamenight_id: Uuid) -> Result<Vec<User>, DatabaseError> { pub async fn load_participants(
conn: &DbConn,
gamenight_id: Uuid,
) -> Result<Vec<User>, DatabaseError> {
Ok(conn Ok(conn
.run::<_, Result<Vec<User>, _>>(move |c| { .run::<_, Result<Vec<User>, _>>(move |c| {
let linked_participants = gamenight_participants::table let linked_participants = gamenight_participants::table
.filter(gamenight_participants::gamenight_id.eq(gamenight_id)) .filter(gamenight_participants::gamenight_id.eq(gamenight_id))
.load::<GamenightParticipantsEntry>(c)?; .load::<GamenightParticipantsEntry>(c)?;
linked_participants.iter().map(|l| { linked_participants
.iter()
.map(|l| {
users::table users::table
.filter(users::id.eq(l.user_id)) .filter(users::id.eq(l.user_id))
.first::<User>(c) .first::<User>(c)
}).collect() })
.collect()
}) })
.await?) .await?)
} }