From 86cdbedd4143a5e18e14b72318497a49edb25bc0 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 28 May 2022 19:28:58 +0200 Subject: [PATCH] Adds the participants part of the API. --- backend/src/api.rs | 48 ++++++++++++++++++++++++++++++++- backend/src/main.rs | 6 +++++ backend/src/schema/gamenight.rs | 34 +++++++++++++++++------ 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/backend/src/api.rs b/backend/src/api.rs index 3f2a1a5..6bb69cc 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -180,7 +180,13 @@ pub async fn gamenights_post_json( Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), }; - 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}; + match insert_participant(&conn, participant).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), } @@ -307,3 +313,43 @@ pub async fn games(conn: DbConn, _user: User) -> ApiResponseVariant { 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 { + match load_participants(&conn, gamenight_id_json.into_inner()).await { + Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), + Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), + } +} + +#[get("/participants", rank = 2)] +pub async fn get_participants_unauthorized() -> ApiResponseVariant { + ApiResponseVariant::Status(Status::Unauthorized) +} + +#[post("/participants", format = "application/json", data = "")] +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()))), + } +} + +#[post("/participants", rank = 2)] +pub async fn post_participants_unauthorized() -> ApiResponseVariant { + ApiResponseVariant::Status(Status::Unauthorized) +} + +#[delete("/participants", format = "application/json", data = "")] +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()))), + } +} + +#[delete("/participants", rank = 2)] +pub async fn delete_participants_unauthorized() -> ApiResponseVariant { + ApiResponseVariant::Status(Status::Unauthorized) +} + diff --git a/backend/src/main.rs b/backend/src/main.rs index cc9989e..2e1c2b0 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -60,6 +60,12 @@ async fn rocket() -> _ { api::gamenights_delete_json_unauthorized, api::games, api::games_unauthorized, + api::get_participants, + api::get_participants_unauthorized, + api::post_participants, + api::post_participants_unauthorized, + api::delete_participants, + api::delete_participants_unauthorized, ], ); diff --git a/backend/src/schema/gamenight.rs b/backend/src/schema/gamenight.rs index ba8b5b3..1a3d417 100644 --- a/backend/src/schema/gamenight.rs +++ b/backend/src/schema/gamenight.rs @@ -69,21 +69,27 @@ pub struct DeleteGameNight { pub game_id: Uuid, } +#[derive(Serialize, Deserialize, Debug, Queryable)] +pub struct GamenightId { + pub gamenight_id: Uuid, +} + pub async fn get_all_gamenights(conn: DbConn) -> Result, DatabaseError> { Ok(conn.run(|c| gamenight::table.load::(c)).await?) } pub async fn insert_gamenight( - conn: DbConn, + conn: &DbConn, new_gamenight: GameNight, game_list: Vec, -) -> Result { +) -> Result { Ok(conn .run(move |c| { - c.transaction(|| { - diesel::insert_into(gamenight::table) + c.transaction::<_, DatabaseError, _>(|| { + let id : Uuid = diesel::insert_into(gamenight::table) .values(&new_gamenight) - .execute(c)?; + .returning(gamenight::id) + .get_result(c)?; let entries: Vec = game_list .iter() @@ -95,10 +101,12 @@ pub async fn insert_gamenight( diesel::insert_into(gamenight_gamelist::table) .values(entries) - .execute(c) + .execute(c)?; + + Ok(id) }) - }) - .await?) + }).await? + ) } pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result { @@ -138,6 +146,16 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec) -> Result<( Ok(()) } +pub async fn load_participants(conn: &DbConn, gamenight_id: GamenightId) -> Result, DatabaseError> { + Ok(conn + .run(move |c| { + gamenight_participants::table + .filter(gamenight_participants::gamenight_id.eq(gamenight_id.gamenight_id)) + .load::(c) + }) + .await?) +} + pub async fn insert_participant( conn: &DbConn, participant: GamenightParticipantsEntry,