use crate::models::participants::Participants; use crate::models::user_id::UserId; use crate::request::authorization::AuthUser; use crate::request::error::ApiError; use actix_web::http::header::ContentType; use actix_web::{get, post, web, HttpResponse, Responder}; use gamenight_database::gamenight_participants::{insert_gamenight_participant, GamenightParticipant}; use gamenight_database::{DbPool, GetConnection}; use uuid::Uuid; #[get("/gamenight/{gamenightId}/participants")] pub async fn get_gamenight_participants( pool: web::Data, _user: AuthUser, gamenight_id: web::Path ) -> Result { let mut conn = pool.get_conn(); let users = gamenight_database::get_participants( &mut conn, &gamenight_id.into_inner(), )? .iter() .map(|x| x.to_string()) .collect(); Ok(HttpResponse::Ok() .content_type(ContentType::json()) .body(serde_json::to_string(&Participants { participants: users, })?)) } #[post("/gamenight/{gamenightId}/participants")] pub async fn post_gamenight_participants( pool: web::Data, _user: AuthUser, gamenight_id: web::Path, user_id: web::Json, ) -> Result { web::block(move || -> Result { let mut conn = pool.get_conn(); Ok(insert_gamenight_participant( &mut conn, GamenightParticipant { gamenight_id: gamenight_id.into_inner(), user_id: Uuid::parse_str(&user_id.user_id)?, }, )?) }) .await??; Ok(HttpResponse::Ok()) }