leetcode_api/entities/
new_index.rs

1use std::fmt::Display;
2
3use lcode_config::global::G_USER_CONFIG;
4use sea_orm::entity::prelude::*;
5
6use crate::leetcode::question::pb_list;
7
8#[derive(Clone)]
9#[derive(Debug)]
10#[derive(Default)]
11#[derive(PartialEq)]
12#[derive(DeriveEntityModel)]
13#[sea_orm(table_name = "new_index")]
14pub struct Model {
15    #[sea_orm(primary_key, auto_increment = false)]
16    pub title_slug: String,
17    pub title: String,
18    pub title_cn: Option<String>,
19    pub is_favor: bool,
20    pub frontend_question_id: String,
21    pub paid_only: bool,
22    pub difficulty: String,
23    pub status: String,
24    #[sea_orm(column_type = "Double", nullable)]
25    pub ac_rate: f64,
26    // pub topic_tags: String,
27}
28
29impl Display for Model {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        let name = if G_USER_CONFIG.config.translate {
32            let mut name = self
33                .title_cn
34                .as_deref()
35                .unwrap_or_default();
36            if name.is_empty() {
37                name = self.title.as_str();
38            }
39            name
40        }
41        else {
42            self.title.as_str()
43        };
44
45        format!(
46            "{id}: {tit}, {st}",
47            id = self.frontend_question_id,
48            tit = name,
49            st = self.status
50        )
51        .fmt(f)
52    }
53}
54
55#[derive(Clone, Copy)]
56#[derive(Debug)]
57#[derive(PartialEq, Eq)]
58#[derive(EnumIter, DeriveRelation)]
59pub enum Relation {}
60
61impl Related<super::topic_tags::Entity> for Entity {
62    fn to() -> RelationDef {
63        super::qs_tag::Relation::TagRelation.def()
64    }
65    fn via() -> Option<RelationDef> {
66        Some(
67            super::qs_tag::Relation::TitleSlug
68                .def()
69                .rev(),
70        )
71    }
72}
73
74impl ActiveModelBehavior for ActiveModel {}
75
76impl From<pb_list::NewIndex> for Model {
77    fn from(value: pb_list::NewIndex) -> Self {
78        Self {
79            title_slug: value.title_slug,
80            title: value.title,
81            title_cn: value.title_cn,
82            is_favor: value.is_favor,
83            frontend_question_id: value.frontend_question_id,
84            paid_only: value.paid_only,
85            difficulty: value.difficulty,
86            status: value.status.unwrap_or_default(),
87            ac_rate: value.ac_rate,
88        }
89    }
90}