Adds the ability to join or leave a gamenight.

This commit is contained in:
2022-05-31 21:27:35 +02:00
parent f7f9f7456b
commit b7f981e3a6
7 changed files with 171 additions and 22 deletions

View File

@@ -31,6 +31,8 @@ struct ApiResponse {
#[serde(skip_serializing_if = "Option::is_none")]
gamenights: Option<Vec<GamenightOutput>>,
#[serde(skip_serializing_if = "Option::is_none")]
gamenight: Option<GamenightOutput>,
#[serde(skip_serializing_if = "Option::is_none")]
games: Option<Vec<Game>>,
}
@@ -43,6 +45,7 @@ impl ApiResponse {
message: None,
user: None,
gamenights: None,
gamenight: None,
games: None,
};
@@ -52,6 +55,7 @@ impl ApiResponse {
message: Some(Cow::Owned(message)),
user: None,
gamenights: None,
gamenight: None,
games: None,
}
}
@@ -65,16 +69,29 @@ impl ApiResponse {
jwt: jwt,
}),
gamenights: None,
gamenight: None,
games: None,
}
}
fn gamenight_response(gamenights: Vec<GamenightOutput>) -> Self {
fn gamenights_response(gamenights: Vec<GamenightOutput>) -> Self {
Self {
result: Self::SUCCES_RESULT,
message: None,
user: None,
gamenights: Some(gamenights),
gamenight: None,
games: None,
}
}
fn gamenight_response(gamenight: GamenightOutput) -> Self {
Self {
result: Self::SUCCES_RESULT,
message: None,
user: None,
gamenights: None,
gamenight: Some(gamenight),
games: None,
}
}
@@ -85,6 +102,7 @@ impl ApiResponse {
message: None,
user: None,
gamenights: None,
gamenight: None,
games: Some(games),
}
}
@@ -140,6 +158,63 @@ pub struct GamenightOutput {
participants: Vec<User>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GamenightUpdate {
action: String
}
#[patch("/gamenights/<gamenight_id>", format = "application/json", data = "<patch_json>")]
pub async fn patch_gamenight(conn: DbConn, user: User, gamenight_id: String, patch_json: Json<GamenightUpdate>) -> ApiResponseVariant {
let uuid = Uuid::parse_str(&gamenight_id).unwrap();
let patch = patch_json.into_inner();
match patch.action.as_str() {
"RemoveParticipant" => {
let entry = GamenightParticipantsEntry {
gamenight_id: uuid,
user_id: user.id
};
match remove_participant(&conn, entry).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string())))
}
}
"AddParticipant" => {
let entry = GamenightParticipantsEntry {
gamenight_id: uuid,
user_id: user.id
};
match add_participant(&conn, entry).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string())))
}
}
_ => ApiResponseVariant::Value(json!(ApiResponse::SUCCES))
}
}
#[get("/gamenights/<gamenight_id>")]
pub async fn gamenight(conn: DbConn, _user: User, gamenight_id: String) -> ApiResponseVariant {
let uuid = Uuid::parse_str(&gamenight_id).unwrap();
let gamenight = match get_gamenight(&conn, uuid).await {
Ok(result) => result,
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
};
let games = match get_games_of_gamenight(&conn, uuid).await {
Ok(result) => result,
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
};
let participants = match load_participants(&conn, uuid).await {
Ok(result) => result,
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
};
let gamenight_output = GamenightOutput {
gamenight: gamenight,
game_list: games,
participants: participants
};
return ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(gamenight_output)))
}
#[get("/gamenights")]
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
let gamenights = match get_all_gamenights(&conn).await {
@@ -164,7 +239,7 @@ pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
.collect();
match game_results {
Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))),
Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenights_response(result))),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
}
}
@@ -220,7 +295,7 @@ pub async fn gamenights_post_json(
gamenight_id: gamenight_id,
user_id: user.id,
};
match insert_participant(&conn, participant).await {
match add_participant(&conn, participant).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
}
@@ -375,7 +450,7 @@ pub async fn post_participants(
_user: User,
entry_json: Json<GamenightParticipantsEntry>,
) -> ApiResponseVariant {
match insert_participant(&conn, entry_json.into_inner()).await {
match add_participant(&conn, entry_json.into_inner()).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
}

View File

@@ -50,6 +50,8 @@ async fn rocket() -> _ {
.mount(
"/api",
routes![
api::gamenight,
api::patch_gamenight,
api::gamenights,
api::gamenights_unauthorized,
api::gamenights_post_json,

View File

@@ -110,9 +110,9 @@ pub async fn insert_gamenight(
.await?)
}
pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<Gamenight, DatabaseError> {
pub async fn get_gamenight(conn: &DbConn, gamenight_id: Uuid) -> Result<Gamenight, DatabaseError> {
Ok(conn
.run(move |c| gamenight::table.find(game_id).first(c))
.run(move |c| gamenight::table.find(gamenight_id).first(c))
.await?)
}
@@ -190,7 +190,7 @@ pub async fn load_participants(
.await?)
}
pub async fn insert_participant(
pub async fn add_participant(
conn: &DbConn,
participant: GamenightParticipantsEntry,
) -> Result<usize, DatabaseError> {
@@ -198,6 +198,7 @@ pub async fn insert_participant(
.run(move |c| {
diesel::insert_into(gamenight_participants::table)
.values(&participant)
.on_conflict_do_nothing()
.execute(c)
})
.await?)