forked from Roflin/gamenight
Autogenerate only the models of the API for the backend-server
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#[allow(unused_imports)]
|
||||
pub mod models;
|
||||
pub mod request;
|
||||
|
||||
use actix_cors::Cors;
|
||||
@@ -6,7 +8,7 @@ use actix_web::HttpServer;
|
||||
use actix_web::App;
|
||||
use actix_web::http;
|
||||
use actix_web::web;
|
||||
use request::{login, register, gamenights, gamenight_post, gamenight_get};
|
||||
use request::{*, login, register, gamenights};
|
||||
use tracing_actix_web::TracingLogger;
|
||||
use gamenight_database::*;
|
||||
|
||||
@@ -40,6 +42,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(gamenights)
|
||||
.service(gamenight_post)
|
||||
.service(gamenight_get)
|
||||
.service(get_user)
|
||||
.service(get_user_unauthenticated)
|
||||
})
|
||||
.bind(("::1", 8080))?
|
||||
.run()
|
||||
|
||||
@@ -13,7 +13,6 @@ pub struct ApiError {
|
||||
}
|
||||
|
||||
impl Display for ApiError {
|
||||
// This trait requires `fmt` with this exact signature.
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
@@ -81,3 +80,12 @@ impl From<chrono::ParseError> for ApiError {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<uuid::Error> for ApiError {
|
||||
fn from(value: uuid::Error) -> Self {
|
||||
ApiError {
|
||||
status: 422,
|
||||
message: value.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ pub use user_handlers::login;
|
||||
pub use user_handlers::register;
|
||||
pub use gamenight_handlers::gamenights;
|
||||
pub use gamenight_handlers::gamenight_post;
|
||||
pub use gamenight_handlers::gamenight_get;
|
||||
pub use gamenight_handlers::gamenight_get;
|
||||
pub use user_handlers::get_user;
|
||||
pub use user_handlers::get_user_unauthenticated;
|
||||
@@ -1,7 +1,10 @@
|
||||
|
||||
use actix_web::http::header::ContentType;
|
||||
use actix_web::{web, get, post, HttpResponse, Responder};
|
||||
use actix_web::{get, post, web, HttpResponse, Responder};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
use validator::ValidateArgs;
|
||||
use crate::models::user::User;
|
||||
use crate::request::requests::{Login, Register, RegisterContext};
|
||||
use crate::request::error::ApiError;
|
||||
use crate::request::responses::LoginResponse;
|
||||
@@ -9,6 +12,8 @@ use crate::request::authorization::get_token;
|
||||
use serde_json;
|
||||
use gamenight_database::{DbPool, GetConnection};
|
||||
|
||||
use super::authorization::AuthUser;
|
||||
|
||||
impl From<Login> for gamenight_database::user::LoginUser {
|
||||
fn from(val: Login) -> Self {
|
||||
gamenight_database::user::LoginUser {
|
||||
@@ -63,4 +68,36 @@ pub async fn register(pool: web::Data<DbPool>, register_data: web::Json<Register
|
||||
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()),
|
||||
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> {
|
||||
let mut conn = pool.get_conn();
|
||||
|
||||
let user = gamenight_database::user::get_user(&mut conn, Uuid::parse_str(&path.uuid)?)?;
|
||||
Ok(HttpResponse::Ok()
|
||||
.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> {
|
||||
Ok(HttpResponse::Forbidden())
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user