leetcode_api/leetcode/question/
impl_insert_db.rs1use sea_orm::{EntityTrait, IntoActiveModel, sea_query::OnConflict};
2use tracing::error;
3
4use super::{pb_list::NewIndex, qs_index::QsIndex};
5use crate::{
6 dao::{InsertToDB, glob_db},
7 entities::{index, new_index, prelude::*, qs_tag, topic_tags},
8};
9
10impl InsertToDB for QsIndex {
11 type Value = String;
12 type Entity = index::Entity;
13 type Model = index::Model;
14 type ActiveModel = index::ActiveModel;
15
16 fn to_model(&self, category: String) -> Self::Model {
17 let mut model: index::Model = self.clone().into();
18 model.category = category;
19 model
20 }
21
22 async fn insert_to_db(&mut self, category: String) {
23 match Index::insert(
24 self.to_model(category)
25 .into_active_model(),
26 )
27 .on_conflict(Self::on_conflict())
28 .exec(glob_db().await)
29 .await
30 {
31 Ok(_) => {},
32 Err(err) => error!("{}", err),
33 };
34 }
35 fn on_conflict() -> OnConflict {
36 OnConflict::column(index::Column::QuestionId)
37 .update_columns([
38 index::Column::QuestionTitle,
39 index::Column::FrontendQuestionId,
40 index::Column::TotalAcs,
41 index::Column::TotalSubmitted,
42 index::Column::Status,
43 index::Column::Difficulty,
44 index::Column::PaidOnly,
45 index::Column::Frequency,
46 index::Column::Progress,
47 index::Column::PassRate,
48 ])
49 .to_owned()
50 }
51}
52
53impl InsertToDB for topic_tags::Model {
54 type Value = u32;
55 type Entity = TopicTagsDB;
56 type Model = Self;
57 type ActiveModel = topic_tags::ActiveModel;
58
59 fn on_conflict() -> OnConflict {
60 OnConflict::column(topic_tags::Column::TopicSlug)
61 .update_columns([
62 topic_tags::Column::Id,
63 topic_tags::Column::Name,
64 topic_tags::Column::NameTranslated,
65 ])
66 .to_owned()
67 }
68}
69impl InsertToDB for qs_tag::Model {
70 type Value = u32;
71 type Entity = QsTagDB;
72 type Model = Self;
73 type ActiveModel = qs_tag::ActiveModel;
74
75 fn on_conflict() -> OnConflict {
76 OnConflict::columns([qs_tag::Column::TopicSlug, qs_tag::Column::TitleSlug])
77 .do_nothing()
78 .to_owned()
79 }
80}
81impl InsertToDB for NewIndex {
82 type Value = u32;
83 type Entity = NewIndexDB;
84 type Model = new_index::Model;
85 type ActiveModel = new_index::ActiveModel;
86
87 fn to_model(&self, _v: Self::Value) -> Self::Model {
88 self.clone().into()
89 }
90 async fn insert_to_db(&mut self, v: Self::Value) {
91 let topic: Vec<topic_tags::ActiveModel> = self
92 .topic_tags
93 .clone()
94 .unwrap_or_default()
95 .into_iter()
96 .map(sea_orm::IntoActiveModel::into_active_model)
97 .collect();
98
99 let mut qs_tag = Vec::with_capacity(topic.len());
100
101 if let Some(models) = self.topic_tags.take() {
102 for ele in models {
103 let qst = qs_tag::Model {
104 topic_slug: ele.topic_slug,
105 title_slug: self.title_slug.clone(),
106 };
107 qs_tag.push(qst.into_active_model());
108 }
109 }
110
111 tokio::join!(
112 self.insert_one(v),
113 topic_tags::Model::insert_many(topic),
114 qs_tag::Model::insert_many(qs_tag)
115 );
116 }
117
118 fn on_conflict() -> OnConflict {
119 OnConflict::column(new_index::Column::TitleSlug)
120 .update_columns([
121 new_index::Column::Title,
122 new_index::Column::TitleCn,
123 new_index::Column::Status,
124 new_index::Column::AcRate,
125 ])
126 .to_owned()
127 }
128}