mayhem_db/models/
user_server.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 user_id: i32,
10    pub server_id: i32,
11}
12
13#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn, Serialize, Deserialize)]
14pub enum Column {
15    UserId,
16    ServerId,
17}
18
19#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey, Serialize, Deserialize)]
20pub enum PrimaryKey {
21    UserId,
22    ServerId,
23}
24
25#[derive(Copy, Clone, Debug, EnumIter, Serialize, Deserialize)]
26pub enum Relation {
27    User,
28    Server,
29}
30
31impl EntityName for Entity {
32    fn table_name(&self) -> &str {
33        return "user_servers";
34    }
35}
36
37impl ColumnTrait for Column {
38    type EntityName = Entity;
39
40    fn def(&self) -> ColumnDef {
41        match self {
42            Self::UserId => ColumnType::Integer.def(),
43            Self::ServerId => ColumnType::Integer.def(),
44        }
45    }
46}
47
48impl PrimaryKeyTrait for PrimaryKey {
49    type ValueType = i32;
50
51    fn auto_increment() -> bool {
52        return true;
53    }
54}
55
56impl Related<super::user::Entity> for Entity {
57    fn to() -> RelationDef {
58        return Relation::User.def();
59    }
60}
61
62impl Related<super::server::server::Entity> for Entity {
63    fn to() -> RelationDef {
64        return Relation::Server.def();
65    }
66}
67
68impl RelationTrait for Relation {
69    fn def(&self) -> RelationDef {
70        match self {
71            Self::User => Entity::belongs_to(super::user::Entity)
72                .from(Column::UserId)
73                .to(super::user::Column::Id)
74                .into(),
75
76            Self::Server => Entity::belongs_to(super::server::server::Entity)
77                .from(Column::ServerId)
78                .to(super::server::server::Column::Id)
79                .into(),
80        }
81    }
82}
83
84impl ActiveModelBehavior for ActiveModel {}