Adds a details page for a single gamenight.

This commit is contained in:
2022-05-29 10:28:53 +02:00
parent 2ba2026e21
commit 639405bf9f
6 changed files with 54 additions and 38 deletions

View File

@@ -137,6 +137,7 @@ pub struct GamenightOutput {
#[serde(flatten)]
gamenight: Gamenight,
game_list: Vec<Game>,
participants: Vec<User>,
}
#[get("/gamenights")]
@@ -150,12 +151,15 @@ pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
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
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())))
@@ -338,7 +342,7 @@ pub async fn games_unauthorized() -> 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()).await {
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()))),
}

View File

@@ -1,3 +1,4 @@
use crate::schema::users::{users, User};
use crate::schema::{DatabaseError, DbConn};
use diesel::{Connection, ExpressionMethods, QueryDsl, RunQueryDsl};
use serde::{Deserialize, Serialize};
@@ -160,12 +161,17 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec<Game>) -> Result<(
Ok(())
}
pub async fn load_participants(conn: &DbConn, gamenight_id: GamenightId) -> Result<Vec<GamenightParticipantsEntry>, DatabaseError> {
pub async fn load_participants(conn: &DbConn, gamenight_id: Uuid) -> Result<Vec<User>, DatabaseError> {
Ok(conn
.run(move |c| {
gamenight_participants::table
.filter(gamenight_participants::gamenight_id.eq(gamenight_id.gamenight_id))
.load::<GamenightParticipantsEntry>(c)
.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()
})
.await?)
}