mayhem_db/models/
user.rs

1use sea_orm::entity::prelude::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Copy, Clone, Default, Debug, DeriveEntity, Serialize, Deserialize)]
5pub struct Entity;
6
7#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)]
8pub struct Model {
9    pub id: i32,
10
11    pub first_name: String,
12    pub last_name: String,
13
14    #[sea_orm(column_type = "Text", unique, indexed)]
15    pub email: String,
16
17    #[sea_orm(column_type = "Text", unique, indexed)]
18    pub username: String,
19    pub password: String,
20}
21
22impl Model {
23    pub fn from_active(active: ActiveModel) -> Model {
24        return Model {
25            id: active.id.unwrap(),
26            first_name: active.first_name.unwrap(),
27            last_name: active.last_name.unwrap(),
28            email: active.email.unwrap(),
29            username: active.username.unwrap(),
30            password: active.password.unwrap(),
31        };
32    }
33}
34
35#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn, Serialize, Deserialize)]
36pub enum Column {
37    Id,
38    FirstName,
39    LastName,
40    Email,
41    Username,
42    Password,
43}
44
45#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey, Serialize, Deserialize)]
46pub enum PrimaryKey {
47    Id,
48}
49
50#[derive(Copy, Clone, Debug, EnumIter, Serialize, Deserialize)]
51pub enum Relation {
52    Settings,
53    Servers,
54}
55
56impl EntityName for Entity {
57    fn table_name(&self) -> &str {
58        return "users";
59    }
60}
61
62impl ColumnTrait for Column {
63    type EntityName = Entity;
64
65    fn def(&self) -> ColumnDef {
66        match self {
67            Self::Id => ColumnType::Integer.def(),
68            Self::FirstName => ColumnType::Text.def(),
69            Self::LastName => ColumnType::Text.def(),
70            Self::Email => ColumnType::Text.def().unique(),
71            Self::Username => ColumnType::Text.def().unique(),
72            Self::Password => ColumnType::Text.def(),
73        }
74    }
75}
76
77impl PrimaryKeyTrait for PrimaryKey {
78    type ValueType = i32;
79
80    fn auto_increment() -> bool {
81        return true;
82    }
83}
84
85impl Related<super::user_settings::Entity> for Entity {
86    fn to() -> RelationDef {
87        return Relation::Settings.def();
88    }
89}
90
91impl Related<super::server::server::Entity> for Entity {
92    fn to() -> RelationDef {
93        return super::user_server::Relation::Server.def();
94    }
95
96    fn via() -> Option<RelationDef> {
97        return Some(super::user_server::Relation::User.def().rev());
98    }
99}
100
101impl RelationTrait for Relation {
102    fn def(&self) -> RelationDef {
103        match self {
104            Self::Settings => Entity::has_one(super::user_settings::Entity).into(),
105            Self::Servers => Entity::has_many(super::server::server::Entity).into(),
106        }
107    }
108}
109
110impl ActiveModelBehavior for ActiveModel {}