gamenight-participants #6
@ -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,25 +144,28 @@ 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<Vec<GamenightOutput>, 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<Vec<GamenightOutput>, 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()))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 = "<gamenight_id_json>")]
|
||||
pub async fn get_participants(conn: DbConn, _user: User, gamenight_id_json: Json<GamenightId>) -> ApiResponseVariant {
|
||||
#[get(
|
||||
"/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 {
|
||||
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 = "<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 {
|
||||
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 = "<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 {
|
||||
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<Gam
|
||||
pub async fn delete_participants_unauthorized() -> ApiResponseVariant {
|
||||
ApiResponseVariant::Status(Status::Unauthorized)
|
||||
}
|
||||
|
||||
|
@ -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<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?)
|
||||
}
|
||||
|
||||
pub async fn get_games_of_gamenight(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
|
||||
.filter(gamenight_gamelist::gamenight_id.eq(gamenight_id))
|
||||
.load::<GamenightGameListEntry>(c)?;
|
||||
pub async fn get_games_of_gamenight(
|
||||
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
|
||||
.filter(gamenight_gamelist::gamenight_id.eq(gamenight_id))
|
||||
.load::<GamenightGameListEntry>(c)?;
|
||||
|
||||
linked_game_ids.iter().map(|l| {
|
||||
known_games::table
|
||||
.filter(known_games::id.eq(l.game_id))
|
||||
.first::<Game>(c)
|
||||
}).collect()
|
||||
}).await?)
|
||||
linked_game_ids
|
||||
.iter()
|
||||
.map(|l| {
|
||||
known_games::table
|
||||
.filter(known_games::id.eq(l.game_id))
|
||||
.first::<Game>(c)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.await?)
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
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
|
||||
.run::<_, Result<Vec<User>, _>>(move |c| {
|
||||
let linked_participants = gamenight_participants::table
|
||||
.filter(gamenight_participants::gamenight_id.eq(gamenight_id))
|
||||
.load::<GamenightParticipantsEntry>(c)?;
|
||||
linked_participants.iter().map(|l| {
|
||||
users::table
|
||||
.filter(users::id.eq(l.user_id))
|
||||
.first::<User>(c)
|
||||
}).collect()
|
||||
linked_participants
|
||||
.iter()
|
||||
.map(|l| {
|
||||
users::table
|
||||
.filter(users::id.eq(l.user_id))
|
||||
.first::<User>(c)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.await?)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user