lynx_core/entities/
rule.rs1use async_trait::async_trait;
3use chrono::Local;
4use sea_orm::{entity::prelude::*, ActiveValue};
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
8#[sea_orm(table_name = "rule")]
9#[serde(rename_all = "camelCase")]
10pub struct Model {
11 #[sea_orm(primary_key)]
12 pub id: i32,
13 pub name: String,
14 pub rule_group_id: i32,
15 pub created_at: u32,
16 pub updated_at: u32,
17}
18
19#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
20pub enum Relation {
21 #[sea_orm(
22 belongs_to = "super::rule_group::Entity",
23 from = "Column::RuleGroupId",
24 to = "super::rule_group::Column::Id"
25 )]
26 RuleGroup,
27 #[sea_orm(has_many = "super::rule_content::Entity")]
28 RuleContent,
29}
30
31impl Related<super::rule_group::Entity> for Entity {
32 fn to() -> RelationDef {
33 Relation::RuleGroup.def()
34 }
35}
36
37impl Related<super::rule_content::Entity> for Entity {
38 fn to() -> RelationDef {
39 Relation::RuleContent.def()
40 }
41}
42
43#[async_trait]
44impl ActiveModelBehavior for ActiveModel {
45 async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
46 where
47 C: ConnectionTrait,
48 {
49 if insert {
50 self.created_at = ActiveValue::Set(Local::now().timestamp_millis() as u32);
51 self.updated_at = ActiveValue::Set(Local::now().timestamp_millis() as u32);
52 } else {
53 self.updated_at = ActiveValue::Set(Local::now().timestamp_millis() as u32);
54 }
55 Ok(self)
56 }
57}