Implemented leaving a gamenight on the server/database side

This commit is contained in:
Dennis Brentjes 2025-06-15 19:11:26 +02:00
parent db6f55bc47
commit d11e31149b
5 changed files with 49 additions and 3 deletions

View File

@ -129,6 +129,19 @@ paths:
$ref: '#/components/requestBodies/JoinGamenight' $ref: '#/components/requestBodies/JoinGamenight'
security: security:
- JWT-Auth: [] - 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: components:
schemas: schemas:
@ -280,6 +293,11 @@ components:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/GamenightId' $ref: '#/components/schemas/GamenightId'
LeaveGamenight:
content:
application/json:
schema:
$ref: '#/components/schemas/GamenightId'
responses: responses:
TokenResponse: TokenResponse:
description: Example response description: Example response

View File

@ -1,5 +1,5 @@
use actix_web::{post, web, HttpResponse, Responder}; 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 uuid::Uuid;
use crate::{models::gamenight_id::GamenightId, request::{authorization::AuthUser, error::ApiError}}; use crate::{models::gamenight_id::GamenightId, request::{authorization::AuthUser, error::ApiError}};
@ -16,3 +16,16 @@ pub async fn post_join_gamenight(pool: web::Data<DbPool>, user: AuthUser, gameni
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())
} }
#[post("/leave")]
pub async fn post_leave_gamenight(pool: web::Data<DbPool>, user: AuthUser, gamenight_id: web::Json<GamenightId>) -> Result<impl Responder, ApiError> {
web::block(move || -> Result<usize, ApiError> {
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())
}

View File

@ -1,7 +1,16 @@
use std::fmt::Display;
use uuid::Uuid; use uuid::Uuid;
#[derive(Clone)] #[derive(Clone)]
pub struct User { pub struct User {
pub id: Uuid, pub id: Uuid,
pub username: String pub username: String
}
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.username)
}
} }

View File

@ -23,7 +23,7 @@ impl ViewGamenight {
fn vec_user_to_usernames_string(users: Vec<User>) -> String { fn vec_user_to_usernames_string(users: Vec<User>) -> String {
let string_list: Vec<String> = users.iter().map(|x| {x.username.clone()}).collect(); let string_list: Vec<String> = users.iter().map(|x| {format!("{}", x)}).collect();
string_list.join(", ") string_list.join(", ")
} }

View File

@ -1,4 +1,4 @@
use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl}; use diesel::{BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use uuid::Uuid; use uuid::Uuid;
@ -25,4 +25,10 @@ pub fn get_participants(conn: &mut DbConnection, id: &Uuid) -> Result<Vec<Uuid>,
pub fn insert_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result<usize, DatabaseError> { pub fn insert_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result<usize, DatabaseError> {
Ok(gp.insert_into(gamenight_participant::table).execute(conn)?) Ok(gp.insert_into(gamenight_participant::table).execute(conn)?)
}
pub fn delete_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result<usize, DatabaseError> {
Ok(gamenight_participant::table
.filter(gamenight_participant::gamenight_id.eq(&gp.gamenight_id).and(gamenight_participant::user_id.eq(gp.user_id)))
.execute(conn)?)
} }