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