This commit is contained in:
2025-04-19 19:11:13 +02:00
parent 8f8016e748
commit 86bb165bf2
20 changed files with 3803 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
// Export models
pub mod user;
// Re-export models for easier imports
pub use user::User;

View File

@@ -0,0 +1,105 @@
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
/// Represents a user in the system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
/// Unique identifier for the user
pub id: Option<i32>,
/// User's full name
pub name: String,
/// User's email address
pub email: String,
/// User's role in the system
pub role: UserRole,
/// When the user was created
pub created_at: Option<DateTime<Utc>>,
/// When the user was last updated
pub updated_at: Option<DateTime<Utc>>,
}
/// Represents the possible roles a user can have
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum UserRole {
/// Regular user with limited permissions
User,
/// Administrator with full permissions
Admin,
}
impl User {
/// Creates a new user with default values
pub fn new(name: String, email: String) -> Self {
Self {
id: None,
name,
email,
role: UserRole::User,
created_at: Some(Utc::now()),
updated_at: Some(Utc::now()),
}
}
/// Creates a new admin user
pub fn new_admin(name: String, email: String) -> Self {
Self {
id: None,
name,
email,
role: UserRole::Admin,
created_at: Some(Utc::now()),
updated_at: Some(Utc::now()),
}
}
/// Checks if the user is an admin
pub fn is_admin(&self) -> bool {
self.role == UserRole::Admin
}
/// Updates the user's information
pub fn update(&mut self, name: Option<String>, email: Option<String>) {
if let Some(name) = name {
self.name = name;
}
if let Some(email) = email {
self.email = email;
}
self.updated_at = Some(Utc::now());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_user() {
let user = User::new("John Doe".to_string(), "john@example.com".to_string());
assert_eq!(user.name, "John Doe");
assert_eq!(user.email, "john@example.com");
assert!(!user.is_admin());
}
#[test]
fn test_new_admin() {
let admin = User::new_admin("Admin User".to_string(), "admin@example.com".to_string());
assert_eq!(admin.name, "Admin User");
assert_eq!(admin.email, "admin@example.com");
assert!(admin.is_admin());
}
#[test]
fn test_update_user() {
let mut user = User::new("John Doe".to_string(), "john@example.com".to_string());
user.update(Some("Jane Doe".to_string()), None);
assert_eq!(user.name, "Jane Doe");
assert_eq!(user.email, "john@example.com");
user.update(None, Some("jane@example.com".to_string()));
assert_eq!(user.name, "Jane Doe");
assert_eq!(user.email, "jane@example.com");
}
}