1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use sea_orm::{entity::prelude::*, sea_query::OnConflict, IntoActiveModel};
use serde::{Deserialize, Serialize};
use tracing::error;

use crate::dao::glob_db;

#[derive(Clone)]
#[derive(Debug)]
#[derive(Default)]
#[derive(PartialEq, Eq)]
#[derive(DeriveEntityModel)]
#[derive(Serialize, Deserialize)]
#[sea_orm(table_name = "qs_tag")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub title_slug: String,
    #[sea_orm(primary_key, auto_increment = false)]
    pub topic_slug: String,
}

#[derive(Clone, Copy)]
#[derive(Debug)]
#[derive(Default)]
#[derive(PartialEq, Eq)]
#[derive(EnumIter)]
pub enum Relation {
    #[default]
    TitleSlug,
    TagRelation,
}

impl RelationTrait for Relation {
    fn def(&self) -> RelationDef {
        match self {
            Self::TitleSlug => Entity::belongs_to(super::new_index::Entity)
                .from(Column::TitleSlug)
                .to(super::new_index::Column::TitleSlug)
                .into(),
            Self::TagRelation => Entity::belongs_to(super::topic_tags::Entity)
                .from(Column::TopicSlug)
                .to(super::topic_tags::Column::TopicSlug)
                .into(),
        }
    }
}

impl ActiveModelBehavior for ActiveModel {}

impl Model {
    pub async fn inert_to_db(self) {
        let temp = self.into_active_model();

        if let Err(err) = Entity::insert(temp)
            .on_conflict(
                OnConflict::columns([Column::TitleSlug, Column::TopicSlug])
                    .update_columns([Column::TitleSlug, Column::TopicSlug])
                    .to_owned(),
            )
            .exec(glob_db().await)
            .await
        {
            error!("{}", err);
        }
    }
}