forked from Roflin/gamenight
Rewrite to chatty but functional API.
This commit is contained in:
@@ -44,6 +44,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(gamenight_get)
|
||||
.service(get_user)
|
||||
.service(get_user_unauthenticated)
|
||||
.service(post_join_gamenight)
|
||||
.service(get_get_participants)
|
||||
})
|
||||
.bind(("::1", 8080))?
|
||||
.run()
|
||||
|
||||
@@ -2,15 +2,15 @@ use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, po
|
||||
use chrono::{DateTime, ParseError};
|
||||
use uuid::Uuid;
|
||||
|
||||
use gamenight_database::{gamenight::Gamenight, DbPool, GetConnection};
|
||||
use gamenight_database::{gamenight, DbPool, GetConnection};
|
||||
|
||||
use crate::{models::{gamenight, add_gamenight_request_body::AddGamenightRequestBody, get_gamenight_request::GetGamenightRequest}, request::authorization::AuthUser};
|
||||
use crate::{models::{add_gamenight_request_body::AddGamenightRequestBody, gamenight::Gamenight, get_gamenight_request_body::GetGamenightRequestBody}, request::authorization::AuthUser};
|
||||
use crate::request::error::ApiError;
|
||||
|
||||
|
||||
impl AddGamenightRequestBody {
|
||||
pub fn into_with_user(&self, user: AuthUser) -> Result<Gamenight, ParseError> {
|
||||
Ok(Gamenight {
|
||||
pub fn into_with_user(&self, user: AuthUser) -> Result<gamenight::Gamenight, ParseError> {
|
||||
Ok(gamenight::Gamenight {
|
||||
datetime: DateTime::parse_from_rfc3339(&self.clone().datetime.unwrap())?.with_timezone(&chrono::Utc),
|
||||
id: Uuid::new_v4(),
|
||||
name: self.clone().name.unwrap().clone(),
|
||||
@@ -19,8 +19,8 @@ impl AddGamenightRequestBody {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GetGamenightRequest> for Uuid {
|
||||
fn from(value: GetGamenightRequest) -> Self {
|
||||
impl From<GetGamenightRequestBody> for Uuid {
|
||||
fn from(value: GetGamenightRequestBody) -> Self {
|
||||
Uuid::parse_str(value.id.unwrap().as_str()).unwrap()
|
||||
}
|
||||
}
|
||||
@@ -28,11 +28,19 @@ impl From<GetGamenightRequest> for Uuid {
|
||||
#[get("/gamenights")]
|
||||
pub async fn gamenights(pool: web::Data<DbPool>, _user: AuthUser) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
let gamenights: Vec<gamenight_database::gamenight::Gamenight> = gamenight_database::gamenights(&mut conn)?;
|
||||
let gamenights: Vec<gamenight::Gamenight> = gamenight_database::gamenights(&mut conn)?;
|
||||
let model: Vec<Gamenight> = gamenights.iter().map(|x| {
|
||||
Gamenight {
|
||||
id: x.id.to_string(),
|
||||
name: x.name.clone(),
|
||||
datetime: x.datetime.to_rfc3339(),
|
||||
owner_id: x.owner_id.to_string()
|
||||
}}
|
||||
).collect();
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&gamenights)?)
|
||||
.body(serde_json::to_string(&model)?)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -40,24 +48,20 @@ pub async fn gamenights(pool: web::Data<DbPool>, _user: AuthUser) -> Result<impl
|
||||
pub async fn gamenight_post(pool: web::Data<DbPool>, user: AuthUser, gamenight_data: web::Json<AddGamenightRequestBody>) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
|
||||
gamenight_database::gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?;
|
||||
gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?;
|
||||
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[get("/gamenight")]
|
||||
pub async fn gamenight_get(pool: web::Data<DbPool>, _user: AuthUser, gamenight_data: web::Json<GetGamenightRequest>) -> Result<impl Responder, ApiError> {
|
||||
pub async fn gamenight_get(pool: web::Data<DbPool>, _user: AuthUser, gamenight_data: web::Json<GetGamenightRequestBody>) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
|
||||
let gamenight = gamenight_database::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?;
|
||||
let participants = gamenight_database::gamenight_participants::get_participants(&mut conn, &gamenight.id)?;
|
||||
|
||||
let model = gamenight::Gamenight{
|
||||
let gamenight = gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?;
|
||||
let model = Gamenight{
|
||||
id: gamenight.id.to_string(),
|
||||
datetime: gamenight.datetime.to_rfc3339(),
|
||||
name: gamenight.name,
|
||||
owner_id: gamenight.owner_id.to_string(),
|
||||
participants: Some(participants.iter().map(|x| {x.to_string()}).collect())
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
|
||||
18
backend-actix/src/request/join_gamenight.rs
Normal file
18
backend-actix/src/request/join_gamenight.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use actix_web::{post, web, HttpResponse, Responder};
|
||||
use gamenight_database::{DbPool, GetConnection, gamenight_participants::{insert_gamenight_participant, GamenightParticipant}};
|
||||
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())
|
||||
}
|
||||
@@ -3,6 +3,8 @@ mod user_handlers;
|
||||
mod gamenight_handlers;
|
||||
mod error;
|
||||
mod authorization;
|
||||
mod join_gamenight;
|
||||
mod participant_handlers;
|
||||
|
||||
pub use user_handlers::login;
|
||||
pub use user_handlers::register;
|
||||
@@ -10,4 +12,6 @@ pub use gamenight_handlers::gamenights;
|
||||
pub use gamenight_handlers::gamenight_post;
|
||||
pub use gamenight_handlers::gamenight_get;
|
||||
pub use user_handlers::get_user;
|
||||
pub use user_handlers::get_user_unauthenticated;
|
||||
pub use user_handlers::get_user_unauthenticated;
|
||||
pub use join_gamenight::post_join_gamenight;
|
||||
pub use participant_handlers::get_get_participants;
|
||||
|
||||
17
backend-actix/src/request/participant_handlers.rs
Normal file
17
backend-actix/src/request/participant_handlers.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use actix_web::{get, http::header::ContentType, web, HttpResponse, Responder};
|
||||
use gamenight_database::{DbPool, GetConnection};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{models::{gamenight_id::GamenightId, participants::Participants}, request::{authorization::AuthUser, error::ApiError}};
|
||||
|
||||
#[get("/participants")]
|
||||
pub async fn get_get_participants(pool: web::Data<DbPool>, _user: AuthUser, gamenight_info: web::Json<GamenightId>) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
|
||||
let users = gamenight_database::get_participants(&mut conn, &Uuid::parse_str(&gamenight_info.into_inner().gamenight_id)?)?
|
||||
.iter().map(|x| x.to_string()).collect();
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&Participants{participants: users})?))
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use crate::models::login::Login;
|
||||
use crate::models::registration::Registration;
|
||||
use crate::models::token::Token;
|
||||
use crate::models::user::User;
|
||||
use crate::models::user_id::UserId;
|
||||
use crate::request::error::ApiError;
|
||||
use crate::request::authorization::get_token;
|
||||
use serde_json;
|
||||
@@ -126,34 +127,29 @@ pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Registra
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct UserInfo {
|
||||
pub uuid: String
|
||||
}
|
||||
|
||||
impl From<gamenight_database::user::User> for User {
|
||||
fn from(value: gamenight_database::user::User) -> Self {
|
||||
Self {
|
||||
id: Some(value.id.to_string()),
|
||||
id: value.id.to_string(),
|
||||
username: value.username,
|
||||
email: None,
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/user/{user_id}")]
|
||||
pub async fn get_user(pool: web::Data<DbPool>, _user: AuthUser, path: web::Path<UserInfo>) -> Result<impl Responder, ApiError> {
|
||||
#[get("/user")]
|
||||
pub async fn get_user(pool: web::Data<DbPool>, _user: AuthUser, user_info: web::Json<UserId>) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
|
||||
let user = gamenight_database::user::get_user(&mut conn, Uuid::parse_str(&path.uuid)?)?;
|
||||
let user = gamenight_database::user::get_user(&mut conn, Uuid::parse_str(&user_info.into_inner().user_id)?)?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&user)?))
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&user)?))
|
||||
}
|
||||
|
||||
#[get("/user/{user_id}")]
|
||||
pub async fn get_user_unauthenticated(_path: web::Path<UserInfo>) -> Result<impl Responder, ApiError> {
|
||||
#[get("/user")]
|
||||
pub async fn get_user_unauthenticated(_path: web::Path<UserId>) -> Result<impl Responder, ApiError> {
|
||||
Ok(HttpResponse::Forbidden())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user