refactoring hexagonal and clean architecture
This commit is contained in:
@@ -3,14 +3,14 @@ use chrono::{DateTime, Utc, Duration};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Session {
|
||||
pub id: String,
|
||||
pub user_id: String,
|
||||
pub refresh_token: String,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
pub ip_address: Option<String>,
|
||||
pub user_agent: Option<String>,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub revoked: bool,
|
||||
id: String,
|
||||
user_id: String,
|
||||
refresh_token: String,
|
||||
expires_at: DateTime<Utc>,
|
||||
ip_address: Option<String>,
|
||||
user_agent: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
revoked: bool,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -21,6 +21,13 @@ impl Session {
|
||||
user_agent: Option<String>,
|
||||
expires_in_days: i64,
|
||||
) -> Self {
|
||||
if user_id.is_empty() {
|
||||
panic!("Session user_id cannot be empty");
|
||||
}
|
||||
if refresh_token.is_empty() {
|
||||
panic!("Session refresh_token cannot be empty");
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
Self {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
@@ -33,6 +40,30 @@ impl Session {
|
||||
revoked: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconstruct a Session from persisted data (e.g. database row).
|
||||
/// Skips ID generation — uses the provided values directly.
|
||||
pub fn from_raw(
|
||||
id: String,
|
||||
user_id: String,
|
||||
refresh_token: String,
|
||||
expires_at: DateTime<Utc>,
|
||||
ip_address: Option<String>,
|
||||
user_agent: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
revoked: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
user_id,
|
||||
refresh_token,
|
||||
expires_at,
|
||||
ip_address,
|
||||
user_agent,
|
||||
created_at,
|
||||
revoked,
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
pub fn id(&self) -> &str {
|
||||
@@ -50,6 +81,14 @@ impl Session {
|
||||
pub fn expires_at(&self) -> DateTime<Utc> {
|
||||
self.expires_at
|
||||
}
|
||||
|
||||
pub fn ip_address(&self) -> Option<&str> {
|
||||
self.ip_address.as_deref()
|
||||
}
|
||||
|
||||
pub fn user_agent(&self) -> Option<&str> {
|
||||
self.user_agent.as_deref()
|
||||
}
|
||||
|
||||
pub fn created_at(&self) -> DateTime<Utc> {
|
||||
self.created_at
|
||||
|
||||
Reference in New Issue
Block a user