barreleye_common/models/
label.rs1use eyre::Result;
2use sea_orm::{
3 entity::prelude::*,
4 sea_query::{func::Func, Expr},
5 Condition, Set,
6};
7use serde::{Deserialize, Serialize};
8
9use crate::{
10 models::{BasicModel, PrimaryId},
11 utils, Db, IdPrefix,
12};
13
14#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, DeriveEntityModel)]
15#[sea_orm(table_name = "labels")]
16#[serde(rename_all = "camelCase")]
17pub struct Model {
18 #[sea_orm(primary_key)]
19 #[serde(skip_serializing, skip_deserializing)]
20 pub label_id: PrimaryId,
21 pub id: String,
22 pub name: String,
23 #[serde(skip_serializing)]
24 pub is_enabled: bool,
25 #[serde(skip_serializing)]
26 pub is_hardcoded: bool,
27 pub is_tracked: bool,
28 #[sea_orm(nullable)]
29 #[serde(skip_serializing)]
30 pub updated_at: Option<DateTime>,
31 pub created_at: DateTime,
32}
33
34pub use ActiveModel as LabelActiveModel;
35pub use Model as Label;
36
37#[derive(Copy, Clone, Debug, EnumIter)]
38pub enum Relation {}
39
40impl RelationTrait for Relation {
41 fn def(&self) -> RelationDef {
42 panic!("No RelationDef")
43 }
44}
45
46impl ActiveModelBehavior for ActiveModel {}
47
48impl BasicModel for Model {
49 type ActiveModel = ActiveModel;
50}
51
52impl Model {
53 pub fn new_model(
54 name: String,
55 is_enabled: bool,
56 is_hardcoded: bool,
57 is_tracked: bool,
58 ) -> ActiveModel {
59 ActiveModel {
60 id: Set(utils::new_unique_id(IdPrefix::Label)),
61 name: Set(name),
62 is_enabled: Set(is_enabled),
63 is_hardcoded: Set(is_hardcoded),
64 is_tracked: Set(is_tracked),
65 ..Default::default()
66 }
67 }
68
69 pub async fn get_all_enabled_and_hardcoded(db: &Db) -> Result<Vec<Self>> {
70 Ok(Entity::find()
71 .filter(Column::IsEnabled.eq(true))
72 .filter(Column::IsHardcoded.eq(true))
73 .all(db.get())
74 .await?)
75 }
76
77 pub async fn get_by_name(db: &Db, name: &str) -> Result<Option<Self>> {
78 Ok(Entity::find()
79 .filter(
80 Condition::all()
81 .add(Func::lower(Expr::col(Column::Name)).equals(name.trim().to_lowercase())),
82 )
83 .one(db.get())
84 .await?)
85 }
86}