Converted the Api to a Restful api.
This commit is contained in:
54
backend-actix/src/request/gamenight_participants.rs
Normal file
54
backend-actix/src/request/gamenight_participants.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
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())
|
||||
}
|
||||
Reference in New Issue
Block a user