Gamenights also return their game list.

This commit is contained in:
2022-05-29 00:22:30 +02:00
parent 86cdbedd41
commit 2ba2026e21
7 changed files with 139 additions and 27 deletions

View File

@@ -1,3 +1,4 @@
use crate::schema::DatabaseError;
use crate::schema::gamenight::*;
use crate::schema::users::*;
use crate::schema::DbConn;
@@ -12,6 +13,7 @@ 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 {
@@ -27,7 +29,7 @@ struct ApiResponse {
#[serde(skip_serializing_if = "Option::is_none")]
user: Option<UserWithToken>,
#[serde(skip_serializing_if = "Option::is_none")]
gamenights: Option<Vec<GameNight>>,
gamenights: Option<Vec<GamenightOutput>>,
#[serde(skip_serializing_if = "Option::is_none")]
games: Option<Vec<Game>>,
}
@@ -67,7 +69,7 @@ impl ApiResponse {
}
}
fn gamenight_response(gamenights: Vec<GameNight>) -> Self {
fn gamenight_response(gamenights: Vec<GamenightOutput>) -> Self {
Self {
result: Self::SUCCES_RESULT,
message: None,
@@ -130,14 +132,34 @@ impl<'r> FromRequest<'r> for User {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GamenightOutput {
#[serde(flatten)]
gamenight: Gamenight,
game_list: Vec<Game>,
}
#[get("/gamenights")]
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
match get_all_gamenights(conn).await {
Ok(gamenights) => {
ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(gamenights)))
}
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
}
let gamenights = match get_all_gamenights(&conn).await {
Ok(result) => result,
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?;
Ok(GamenightOutput{
gamenight: gn.clone(),
game_list: games
})
})).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())))
}
}
#[get("/gamenights", rank = 2)]
@@ -146,16 +168,16 @@ pub async fn gamenights_unauthorized() -> ApiResponseVariant {
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GameNightInput {
pub struct GamenightInput {
pub name: String,
pub datetime: String,
pub owner_id: Option<Uuid>,
pub game_list: Vec<Game>,
}
impl Into<GameNight> for GameNightInput {
fn into(self) -> GameNight {
GameNight {
impl Into<Gamenight> for GamenightInput {
fn into(self) -> Gamenight {
Gamenight {
id: Uuid::new_v4(),
name: self.name,
datetime: self.datetime,
@@ -168,7 +190,7 @@ impl Into<GameNight> for GameNightInput {
pub async fn gamenights_post_json(
conn: DbConn,
user: User,
gamenight_json: Json<GameNightInput>,
gamenight_json: Json<GamenightInput>,
) -> ApiResponseVariant {
let mut gamenight = gamenight_json.into_inner();
gamenight.owner_id = Some(user.id);
@@ -205,7 +227,7 @@ pub async fn gamenights_post_json_unauthorized() -> ApiResponseVariant {
pub async fn gamenights_delete_json(
conn: DbConn,
user: User,
delete_gamenight_json: Json<DeleteGameNight>,
delete_gamenight_json: Json<DeleteGamenight>,
) -> ApiResponseVariant {
if user.role == Role::Admin {
if let Err(error) = delete_gamenight(&conn, delete_gamenight_json.game_id).await {

View File

@@ -40,9 +40,9 @@ pub struct Game {
pub name: String,
}
#[derive(Serialize, Deserialize, Debug, Queryable, Insertable)]
#[derive(Serialize, Deserialize, Debug, Queryable, Insertable, Clone)]
#[table_name = "gamenight"]
pub struct GameNight {
pub struct Gamenight {
pub id: Uuid,
pub name: String,
pub datetime: String,
@@ -65,7 +65,7 @@ pub struct GamenightParticipantsEntry {
}
#[derive(Serialize, Deserialize, Debug, Queryable)]
pub struct DeleteGameNight {
pub struct DeleteGamenight {
pub game_id: Uuid,
}
@@ -74,13 +74,13 @@ pub struct GamenightId {
pub gamenight_id: Uuid,
}
pub async fn get_all_gamenights(conn: DbConn) -> Result<Vec<GameNight>, DatabaseError> {
Ok(conn.run(|c| gamenight::table.load::<GameNight>(c)).await?)
pub async fn get_all_gamenights(conn: &DbConn) -> Result<Vec<Gamenight>, DatabaseError> {
Ok(conn.run(|c| gamenight::table.load::<Gamenight>(c)).await?)
}
pub async fn insert_gamenight(
conn: &DbConn,
new_gamenight: GameNight,
new_gamenight: Gamenight,
game_list: Vec<Game>,
) -> Result<Uuid, DatabaseError> {
Ok(conn
@@ -109,7 +109,7 @@ pub async fn insert_gamenight(
)
}
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> {
Ok(conn
.run(move |c| gamenight::table.find(game_id).first(c))
.await?)
@@ -125,6 +125,20 @@ 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)?;
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> {
Ok(conn
.run(|c| {