Create an API-friendly version of the User object

This commit is contained in:
Savanni D'Gerinel 2025-01-20 20:59:40 -05:00
parent ac3a21f3f0
commit 84ee790f0b
3 changed files with 51 additions and 9 deletions

View File

@ -7,7 +7,9 @@ use axum::{
routing::{get, post, put}, routing::{get, post, put},
Json, Router, Json, Router,
}; };
use serde::{Deserialize, Serialize};
use tower_http::cors::{Any, CorsLayer}; use tower_http::cors::{Any, CorsLayer};
use typeshare::typeshare;
use crate::{ use crate::{
core::Core, core::Core,
@ -18,6 +20,50 @@ use crate::{
}, },
}; };
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type", content = "content")]
#[typeshare]
pub enum AccountState {
Normal,
PasswordReset(String),
Locked,
}
impl From<crate::types::AccountState> for AccountState {
fn from(s: crate::types::AccountState) -> Self {
match s {
crate::types::AccountState::Normal => Self::Normal,
crate::types::AccountState::PasswordReset(r) => {
Self::PasswordReset(format!("{}", r.format("%Y-%m-%d %H:%M:%S")))
}
crate::types::AccountState::Locked => Self::Locked,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct User {
pub id: UserId,
pub name: String,
pub password: String,
pub admin: bool,
pub state: AccountState,
}
impl From<crate::types::User> for User {
fn from(u: crate::types::User) -> Self {
Self {
id: u.id,
name: u.name,
password: u.password,
admin: u.admin,
state: AccountState::from(u.state),
}
}
}
pub fn routes(core: Core) -> Router { pub fn routes(core: Core) -> Router {
Router::new() Router::new()
.route( .route(
@ -127,7 +173,7 @@ mod test {
core::{AuthResponse, Core}, core::{AuthResponse, Core},
database::{Database, DbConn, GameId, SessionId, UserId}, database::{Database, DbConn, GameId, SessionId, UserId},
handlers::CreateGameRequest, handlers::CreateGameRequest,
types::{AccountState, UserOverview}, types::UserOverview,
}; };
async fn initialize_test_server() -> (Core, TestServer) { async fn initialize_test_server() -> (Core, TestServer) {
@ -139,7 +185,7 @@ mod test {
"admin", "admin",
"aoeu", "aoeu",
true, true,
AccountState::PasswordReset(password_exp), crate::types::AccountState::PasswordReset(password_exp),
) )
.await .await
.unwrap(); .unwrap();

View File

@ -75,9 +75,7 @@ pub struct Rgb {
pub blue: u32, pub blue: u32,
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug)]
#[serde(tag = "type", content = "content")]
#[typeshare]
pub enum AccountState { pub enum AccountState {
Normal, Normal,
PasswordReset(DateTime<Utc>), PasswordReset(DateTime<Utc>),
@ -119,9 +117,7 @@ impl ToSql for AccountState {
} }
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct User { pub struct User {
pub id: UserId, pub id: UserId,
pub name: String, pub name: String,

View File

@ -33,7 +33,7 @@
"version": "0.0.1", "version": "0.0.1",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"typescript": "^5.7.2" "typescript": "^5.7.3"
} }
}, },
"node_modules/@adobe/css-tools": { "node_modules/@adobe/css-tools": {