From d11e31149b87e0c80e74d020279b11e66f621428 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 15 Jun 2025 19:11:26 +0200 Subject: [PATCH] Implemented leaving a gamenight on the server/database side --- backend-actix/gamenight-api.yaml | 18 ++++++++++++++++++ backend-actix/src/request/join_gamenight.rs | 15 ++++++++++++++- gamenight-cli/src/domain/user.rs | 9 +++++++++ gamenight-cli/src/flows/view_gamenight.rs | 2 +- .../src/gamenight_participants.rs | 8 +++++++- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/backend-actix/gamenight-api.yaml b/backend-actix/gamenight-api.yaml index 5a13390..2ae3480 100644 --- a/backend-actix/gamenight-api.yaml +++ b/backend-actix/gamenight-api.yaml @@ -129,6 +129,19 @@ paths: $ref: '#/components/requestBodies/JoinGamenight' security: - JWT-Auth: [] + /leave: + post: + responses: + '200': + description: OK + '401': + $ref: '#/components/responses/FailureResponse' + '422': + $ref: '#/components/responses/FailureResponse' + requestBody: + $ref: '#/components/requestBodies/LeaveGamenight' + security: + - JWT-Auth: [] components: schemas: @@ -280,6 +293,11 @@ components: application/json: schema: $ref: '#/components/schemas/GamenightId' + LeaveGamenight: + content: + application/json: + schema: + $ref: '#/components/schemas/GamenightId' responses: TokenResponse: description: Example response diff --git a/backend-actix/src/request/join_gamenight.rs b/backend-actix/src/request/join_gamenight.rs index 2077395..40d778c 100644 --- a/backend-actix/src/request/join_gamenight.rs +++ b/backend-actix/src/request/join_gamenight.rs @@ -1,5 +1,5 @@ use actix_web::{post, web, HttpResponse, Responder}; -use gamenight_database::{DbPool, GetConnection, gamenight_participants::{insert_gamenight_participant, GamenightParticipant}}; +use gamenight_database::{gamenight_participants::{delete_gamenight_participant, insert_gamenight_participant, GamenightParticipant}, DbPool, GetConnection}; use uuid::Uuid; use crate::{models::gamenight_id::GamenightId, request::{authorization::AuthUser, error::ApiError}}; @@ -16,3 +16,16 @@ pub async fn post_join_gamenight(pool: web::Data, user: AuthUser, gameni Ok(HttpResponse::Ok()) } + +#[post("/leave")] +pub async fn post_leave_gamenight(pool: web::Data, user: AuthUser, gamenight_id: web::Json) -> Result { + web::block(move || -> Result { + let mut conn = pool.get_conn(); + Ok(delete_gamenight_participant(&mut conn, GamenightParticipant { + gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?, + user_id: user.id + })?) + }).await??; + + Ok(HttpResponse::Ok()) +} diff --git a/gamenight-cli/src/domain/user.rs b/gamenight-cli/src/domain/user.rs index 8c9775f..bc051f4 100644 --- a/gamenight-cli/src/domain/user.rs +++ b/gamenight-cli/src/domain/user.rs @@ -1,7 +1,16 @@ +use std::fmt::Display; + use uuid::Uuid; #[derive(Clone)] pub struct User { pub id: Uuid, pub username: String +} + +impl Display for User { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.username) + + } } \ No newline at end of file diff --git a/gamenight-cli/src/flows/view_gamenight.rs b/gamenight-cli/src/flows/view_gamenight.rs index 5fef946..62725f4 100644 --- a/gamenight-cli/src/flows/view_gamenight.rs +++ b/gamenight-cli/src/flows/view_gamenight.rs @@ -23,7 +23,7 @@ impl ViewGamenight { fn vec_user_to_usernames_string(users: Vec) -> String { - let string_list: Vec = users.iter().map(|x| {x.username.clone()}).collect(); + let string_list: Vec = users.iter().map(|x| {format!("{}", x)}).collect(); string_list.join(", ") } diff --git a/gamenight-database/src/gamenight_participants.rs b/gamenight-database/src/gamenight_participants.rs index 7711ab6..96e5f65 100644 --- a/gamenight-database/src/gamenight_participants.rs +++ b/gamenight-database/src/gamenight_participants.rs @@ -1,4 +1,4 @@ -use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl}; +use diesel::{BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl}; use serde::{Serialize, Deserialize}; use uuid::Uuid; @@ -25,4 +25,10 @@ pub fn get_participants(conn: &mut DbConnection, id: &Uuid) -> Result, pub fn insert_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result { Ok(gp.insert_into(gamenight_participant::table).execute(conn)?) +} + +pub fn delete_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result { + Ok(gamenight_participant::table + .filter(gamenight_participant::gamenight_id.eq(&gp.gamenight_id).and(gamenight_participant::user_id.eq(gp.user_id))) + .execute(conn)?) } \ No newline at end of file