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},
Json, Router,
};
use serde::{Deserialize, Serialize};
use tower_http::cors::{Any, CorsLayer};
use typeshare::typeshare;
use crate::{
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 {
Router::new()
.route(
@ -127,7 +173,7 @@ mod test {
core::{AuthResponse, Core},
database::{Database, DbConn, GameId, SessionId, UserId},
handlers::CreateGameRequest,
types::{AccountState, UserOverview},
types::UserOverview,
};
async fn initialize_test_server() -> (Core, TestServer) {
@ -139,7 +185,7 @@ mod test {
"admin",
"aoeu",
true,
AccountState::PasswordReset(password_exp),
crate::types::AccountState::PasswordReset(password_exp),
)
.await
.unwrap();

View File

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

View File

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