user_preferences.rs 73 lignes · 1938 octets
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// UserPreferences entity model
///
/// Represents user display preferences (theme, language).
/// One-to-one relationship with User.
#[derive(Clone, Debug, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "user_preferences")]
pub struct Model {
    /// Unique identifier for the preferences
    #[sea_orm(primary_key)]
    pub id: Uuid,
    /// User ID (unique, one-to-one with users)
    #[sea_orm(unique)]
    pub user_id: Uuid,
    /// Theme preference: "light", "dark", or "auto"
    pub theme: String,
    /// Language preference: "fr" or "en"
    pub language: String,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

/// SeaORM relations for the UserPreferences entity.
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    /// The user these preferences belong to.
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::UserId",
        to = "super::user::Column::Id"
    )]
    User,
}

impl Related<super::user::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::User.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_user_preferences_model() {
        let id = Uuid::new_v4();
        let user_id = Uuid::new_v4();
        let now = Utc::now();

        let preferences = Model {
            id,
            user_id,
            theme: "light".to_string(),
            language: "fr".to_string(),
            created_at: now,
            updated_at: now,
        };

        assert_eq!(preferences.id, id);
        assert_eq!(preferences.user_id, user_id);
        assert_eq!(preferences.theme, "light");
        assert_eq!(preferences.language, "fr");
    }
}