Refactor InternalUser to InternalNewUser

This commit is contained in:
2025-11-30 20:15:53 +01:00
parent a3e6563912
commit 6f55add34d
7 changed files with 25 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ tower = { version = "0.5.2", features = ["tokio", "tracing"] }
tower-http = { version = "0.6.7", features = ["cors"] } tower-http = { version = "0.6.7", features = ["cors"] }
tracing = { version = "0.1.43", features = ["max_level_debug"] } tracing = { version = "0.1.43", features = ["max_level_debug"] }
tracing-subscriber = "0.3.22" tracing-subscriber = "0.3.22"
uuid = { version = "1.18.1", features = ["v4", "serde"] }
validator = { version = "0.20.0", features = ["derive"] } validator = { version = "0.20.0", features = ["derive"] }
[build-dependencies] [build-dependencies]

View File

@@ -0,0 +1 @@
pub mod user_routines;

View File

@@ -0,0 +1,5 @@
use std::sync::Arc;
use crate::{domain::user::NewUser, state::AppState};
pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User> {}

View File

@@ -28,7 +28,17 @@ pub struct NewUser {
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct InternalNewUser {
username: String,
email: Option<String>,
password_hash: String,
first_name: Option<String>,
last_name: Option<String>,
}
#[derive(Debug, Clone)]
pub struct InternalUser { pub struct InternalUser {
id: i64,
username: String, username: String,
email: Option<String>, email: Option<String>,
password_hash: String, password_hash: String,
@@ -38,6 +48,7 @@ pub struct InternalUser {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct User { pub struct User {
id: i64,
username: String, username: String,
email: Option<String>, email: Option<String>,
first_name: Option<String>, first_name: Option<String>,
@@ -49,7 +60,7 @@ pub enum UserConversionError {
HashFailed(password_hash::Error), HashFailed(password_hash::Error),
} }
impl TryFrom<NewUser> for InternalUser { impl TryFrom<NewUser> for InternalNewUser {
type Error = UserConversionError; type Error = UserConversionError;
fn try_from(value: NewUser) -> Result<Self, Self::Error> { fn try_from(value: NewUser) -> Result<Self, Self::Error> {
let password_hash = let password_hash =
@@ -67,6 +78,7 @@ impl TryFrom<NewUser> for InternalUser {
impl From<InternalUser> for User { impl From<InternalUser> for User {
fn from(value: InternalUser) -> Self { fn from(value: InternalUser) -> Self {
Self { Self {
id: value.id,
username: value.username, username: value.username,
email: value.email, email: value.email,
first_name: value.first_name, first_name: value.first_name,

View File

@@ -1,4 +1,5 @@
pub mod auth; pub mod auth;
pub mod core;
pub mod domain; pub mod domain;
pub mod router; pub mod router;
pub mod state; pub mod state;

View File

@@ -6,7 +6,7 @@ use tracing::{debug, error, info, warn};
use validator::Validate; use validator::Validate;
use crate::{ use crate::{
domain::user::{InternalUser, NewUser, User}, domain::user::{InternalNewUser, NewUser, User},
state::AppState, state::AppState,
}; };
@@ -18,7 +18,7 @@ pub async fn create(
return Err(StatusCode::BAD_REQUEST); return Err(StatusCode::BAD_REQUEST);
} }
let internal = InternalUser::try_from(new_user).map_err(|e| { let internal = InternalNewUser::try_from(new_user).map_err(|e| {
error!("Conversion to InternalUser failed: {e}"); error!("Conversion to InternalUser failed: {e}");
StatusCode::INTERNAL_SERVER_ERROR StatusCode::INTERNAL_SERVER_ERROR
})?; })?;

View File

@@ -1,9 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::domain::user::InternalUser; use crate::domain::user::InternalNewUser;
pub struct AppState { pub struct AppState {
pub users: HashMap<String, InternalUser>, pub users: HashMap<String, InternalNewUser>,
} }
impl AppState { impl AppState {