Files
gamenight/backend-actix/src/request/gamenight_participants.rs

54 lines
1.6 KiB
Rust

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<DbPool>,
_user: AuthUser,
gamenight_id: web::Path<Uuid>
) -> Result<impl Responder, ApiError> {
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<DbPool>,
_user: AuthUser,
gamenight_id: web::Path<Uuid>,
user_id: web::Json<UserId>,
) -> 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: gamenight_id.into_inner(),
user_id: Uuid::parse_str(&user_id.user_id)?,
},
)?)
})
.await??;
Ok(HttpResponse::Ok())
}