32 lines
1.3 KiB
Rust
32 lines
1.3 KiB
Rust
use actix_web::{post, web, HttpResponse, Responder};
|
|
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}};
|
|
|
|
#[post("/join")]
|
|
pub async fn post_join_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(insert_gamenight_participant(&mut conn, GamenightParticipant {
|
|
gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?,
|
|
user_id: user.id
|
|
})?)
|
|
}).await??;
|
|
|
|
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())
|
|
}
|