forked from Roflin/gamenight
		
	Implemented leaving a gamenight on the server/database side
This commit is contained in:
		
							parent
							
								
									db6f55bc47
								
							
						
					
					
						commit
						d11e31149b
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -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<DbPool>, user: AuthUser, gameni
 | 
			
		||||
 | 
			
		||||
    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())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -23,7 +23,7 @@ impl ViewGamenight {
 | 
			
		||||
 | 
			
		||||
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(", ")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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<Vec<Uuid>,
 | 
			
		||||
 | 
			
		||||
pub fn insert_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result<usize, DatabaseError> {
 | 
			
		||||
    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)?)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user