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