Kould commited on
Commit
b772fd3
·
1 Parent(s): 738c322

impl kb_info & dialog_info & doc_info (#9)

Browse files

* feat: impl kb_info & dialog_info

* feat: impl doc_info apis

Cargo.toml CHANGED
@@ -9,6 +9,7 @@ edition = "2021"
9
  actix-web = "4.3.1"
10
  actix-rt = "2.8.0"
11
  actix-files = "0.6.2"
 
12
  postgres = "0.19.7"
13
  sea-orm = {version = "0.12.9", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros"]}
14
  serde = { version = "1", features = ["derive"] }
@@ -18,6 +19,7 @@ dotenvy = "0.15.7"
18
  listenfd = "1.0.1"
19
  chrono = "0.4.31"
20
  migration = { path = "./migration" }
 
21
 
22
  [[bin]]
23
  name = "doc_gpt"
 
9
  actix-web = "4.3.1"
10
  actix-rt = "2.8.0"
11
  actix-files = "0.6.2"
12
+ actix-multipart = "0.4"
13
  postgres = "0.19.7"
14
  sea-orm = {version = "0.12.9", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros"]}
15
  serde = { version = "1", features = ["derive"] }
 
19
  listenfd = "1.0.1"
20
  chrono = "0.4.31"
21
  migration = { path = "./migration" }
22
+ futures-util = "0.3.29"
23
 
24
  [[bin]]
25
  name = "doc_gpt"
migration/src/m20220101_000001_create_table.rs CHANGED
@@ -130,6 +130,7 @@ impl MigrationTrait for Migration {
130
  .primary_key())
131
  .col(ColumnDef::new(DocInfo::Uid).big_integer().not_null())
132
  .col(ColumnDef::new(DocInfo::DocName).string().not_null())
 
133
  .col(ColumnDef::new(DocInfo::Size).big_integer().not_null())
134
  .col(ColumnDef::new(DocInfo::Type).string().not_null()).comment("doc|folder")
135
  .col(ColumnDef::new(DocInfo::KbProgress).float().default(0))
@@ -280,6 +281,7 @@ enum DocInfo {
280
  Did,
281
  Uid,
282
  DocName,
 
283
  Size,
284
  Type,
285
  KbProgress,
 
130
  .primary_key())
131
  .col(ColumnDef::new(DocInfo::Uid).big_integer().not_null())
132
  .col(ColumnDef::new(DocInfo::DocName).string().not_null())
133
+ .col(ColumnDef::new(DocInfo::Location).string().not_null())
134
  .col(ColumnDef::new(DocInfo::Size).big_integer().not_null())
135
  .col(ColumnDef::new(DocInfo::Type).string().not_null()).comment("doc|folder")
136
  .col(ColumnDef::new(DocInfo::KbProgress).float().default(0))
 
281
  Did,
282
  Uid,
283
  DocName,
284
+ Location,
285
  Size,
286
  Type,
287
  KbProgress,
src/api/dialog_info.rs ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use std::collections::HashMap;
2
+ use actix_web::{get, HttpResponse, post, web};
3
+ use actix_web::http::Error;
4
+ use crate::api::JsonResponse;
5
+ use crate::AppState;
6
+ use crate::entity::dialog_info;
7
+ use crate::service::dialog_info::Query;
8
+ use crate::service::dialog_info::Mutation;
9
+
10
+ #[get("/v1.0/dialogs")]
11
+ async fn list(model: web::Json<dialog_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
12
+ let dialogs = Query::find_dialog_infos_by_uid(&data.conn, model.uid).await.unwrap();
13
+
14
+ let mut result = HashMap::new();
15
+ result.insert("dialogs", dialogs);
16
+
17
+ let json_response = JsonResponse {
18
+ code: 200,
19
+ err: "".to_owned(),
20
+ data: result,
21
+ };
22
+
23
+ Ok(HttpResponse::Ok()
24
+ .content_type("application/json")
25
+ .body(serde_json::to_string(&json_response).unwrap()))
26
+ }
27
+
28
+ #[get("/v1.0/dialog")]
29
+ async fn detail(model: web::Json<dialog_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
30
+ let dialogs = Query::find_dialog_info_by_id(&data.conn, model.dialog_id).await.unwrap();
31
+
32
+ let mut result = HashMap::new();
33
+ result.insert("dialogs", dialogs);
34
+
35
+ let json_response = JsonResponse {
36
+ code: 200,
37
+ err: "".to_owned(),
38
+ data: result,
39
+ };
40
+
41
+ Ok(HttpResponse::Ok()
42
+ .content_type("application/json")
43
+ .body(serde_json::to_string(&json_response).unwrap()))
44
+ }
45
+
46
+ #[post("/v1.0/delete_dialog")]
47
+ async fn delete(model: web::Json<dialog_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
48
+ let _ = Mutation::delete_dialog_info(&data.conn, model.dialog_id).await.unwrap();
49
+
50
+ let json_response = JsonResponse {
51
+ code: 200,
52
+ err: "".to_owned(),
53
+ data: (),
54
+ };
55
+
56
+ Ok(HttpResponse::Ok()
57
+ .content_type("application/json")
58
+ .body(serde_json::to_string(&json_response).unwrap()))
59
+ }
60
+
61
+ #[post("/v1.0/create_kb")]
62
+ async fn create(model: web::Json<dialog_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
63
+ let model = Mutation::create_dialog_info(&data.conn, model.into_inner()).await.unwrap();
64
+
65
+ let mut result = HashMap::new();
66
+ result.insert("dialog_id", model.dialog_id.unwrap());
67
+
68
+ let json_response = JsonResponse {
69
+ code: 200,
70
+ err: "".to_owned(),
71
+ data: result,
72
+ };
73
+
74
+ Ok(HttpResponse::Ok()
75
+ .content_type("application/json")
76
+ .body(serde_json::to_string(&json_response).unwrap()))
77
+ }
src/api/doc_info.rs ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use std::collections::HashMap;
2
+ use actix_multipart::Multipart;
3
+ use actix_web::{get, HttpResponse, post, web};
4
+ use actix_web::http::Error;
5
+ use chrono::Local;
6
+ use futures_util::{AsyncWriteExt, StreamExt};
7
+ use serde::Deserialize;
8
+ use std::io::Write;
9
+ use crate::api::JsonResponse;
10
+ use crate::AppState;
11
+ use crate::entity::doc_info::Model;
12
+ use crate::service::doc_info::{Mutation, Query};
13
+
14
+ #[derive(Debug, Deserialize)]
15
+ pub struct Params {
16
+ pub uid: i64,
17
+ pub filter: FilterParams,
18
+ pub sortby: String,
19
+ pub page: u64,
20
+ pub per_page: u64,
21
+ }
22
+
23
+ #[derive(Debug, Deserialize)]
24
+ pub struct FilterParams {
25
+ pub keywords: Option<String>,
26
+ pub folder_id: Option<i64>,
27
+ pub tag_id: Option<i64>,
28
+ pub kb_id: Option<i64>,
29
+ }
30
+
31
+ #[derive(Debug, Deserialize)]
32
+ pub struct MvParams {
33
+ pub dids: Vec<i64>,
34
+ pub dest_did: i64,
35
+ }
36
+
37
+ #[get("/v1.0/docs")]
38
+ async fn list(params: web::Json<Params>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
39
+ let docs = Query::find_doc_infos_by_params(&data.conn, params.into_inner())
40
+ .await
41
+ .unwrap();
42
+
43
+ let mut result = HashMap::new();
44
+ result.insert("docs", docs);
45
+
46
+ let json_response = JsonResponse {
47
+ code: 200,
48
+ err: "".to_owned(),
49
+ data: result,
50
+ };
51
+
52
+ Ok(HttpResponse::Ok()
53
+ .content_type("application/json")
54
+ .body(serde_json::to_string(&json_response).unwrap()))
55
+ }
56
+
57
+ #[post("/v1.0/upload")]
58
+ async fn upload(mut payload: Multipart, filename: web::Data<String>, did: web::Data<i64>, uid: web::Data<i64>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
59
+ let mut size = 0;
60
+
61
+ while let Some(item) = payload.next().await {
62
+ let mut field = item.unwrap();
63
+
64
+ let filepath = format!("./uploads/{}", filename.as_str());
65
+
66
+ let mut file = web::block(|| std::fs::File::create(filepath))
67
+ .await
68
+ .unwrap()
69
+ .unwrap();
70
+
71
+ while let Some(chunk) = field.next().await {
72
+ let data = chunk.unwrap();
73
+ size += data.len() as u64;
74
+ file = web::block(move || file.write_all(&data).map(|_| file))
75
+ .await
76
+ .unwrap()
77
+ .unwrap();
78
+ }
79
+ }
80
+
81
+ let _ = Mutation::create_doc_info(&data.conn, Model {
82
+ did: *did.into_inner(),
83
+ uid: *uid.into_inner(),
84
+ doc_name: filename.to_string(),
85
+ size,
86
+ kb_infos: Vec::new(),
87
+ kb_progress: 0.0,
88
+ location: "".to_string(),
89
+ r#type: "".to_string(),
90
+ created_at: Local::now().date_naive(),
91
+ updated_at: Local::now().date_naive(),
92
+ }).await.unwrap();
93
+
94
+ Ok(HttpResponse::Ok().body("File uploaded successfully"))
95
+ }
96
+
97
+ #[post("/v1.0/delete_docs")]
98
+ async fn delete(doc_ids: web::Json<Vec<i64>>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
99
+ for doc_id in doc_ids.iter() {
100
+ let _ = Mutation::delete_doc_info(&data.conn, *doc_id).await.unwrap();
101
+ }
102
+
103
+ let json_response = JsonResponse {
104
+ code: 200,
105
+ err: "".to_owned(),
106
+ data: (),
107
+ };
108
+
109
+ Ok(HttpResponse::Ok()
110
+ .content_type("application/json")
111
+ .body(serde_json::to_string(&json_response).unwrap()))
112
+ }
113
+
114
+ #[post("/v1.0/mv_docs")]
115
+ async fn mv(params: web::Json<MvParams>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
116
+ Mutation::mv_doc_info(&data.conn, params.dest_did, &params.dids).await.unwrap();
117
+
118
+ let json_response = JsonResponse {
119
+ code: 200,
120
+ err: "".to_owned(),
121
+ data: (),
122
+ };
123
+
124
+ Ok(HttpResponse::Ok()
125
+ .content_type("application/json")
126
+ .body(serde_json::to_string(&json_response).unwrap()))
127
+ }
src/api/kb_info.rs ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use std::collections::HashMap;
2
+ use actix_web::{get, HttpResponse, post, web};
3
+ use actix_web::http::Error;
4
+ use crate::api::JsonResponse;
5
+ use crate::AppState;
6
+ use crate::entity::kb_info;
7
+ use crate::service::kb_info::Mutation;
8
+ use crate::service::kb_info::Query;
9
+
10
+ #[post("/v1.0/create_kb")]
11
+ async fn create(model: web::Json<kb_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
12
+ let model = Mutation::create_kb_info(&data.conn, model.into_inner()).await.unwrap();
13
+
14
+ let mut result = HashMap::new();
15
+ result.insert("kb_id", model.kb_id.unwrap());
16
+
17
+ let json_response = JsonResponse {
18
+ code: 200,
19
+ err: "".to_owned(),
20
+ data: result,
21
+ };
22
+
23
+ Ok(HttpResponse::Ok()
24
+ .content_type("application/json")
25
+ .body(serde_json::to_string(&json_response).unwrap()))
26
+ }
27
+
28
+ #[get("/v1.0/kbs")]
29
+ async fn list(model: web::Json<kb_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
30
+ let kbs = Query::find_kb_infos_by_uid(&data.conn, model.uid).await.unwrap();
31
+
32
+ let mut result = HashMap::new();
33
+ result.insert("kbs", kbs);
34
+
35
+ let json_response = JsonResponse {
36
+ code: 200,
37
+ err: "".to_owned(),
38
+ data: result,
39
+ };
40
+
41
+ Ok(HttpResponse::Ok()
42
+ .content_type("application/json")
43
+ .body(serde_json::to_string(&json_response).unwrap()))
44
+ }
45
+
46
+ #[post("/v1.0/delete_kb")]
47
+ async fn delete(model: web::Json<kb_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
48
+ let _ = Mutation::delete_kb_info(&data.conn, model.kb_id).await.unwrap();
49
+
50
+ let json_response = JsonResponse {
51
+ code: 200,
52
+ err: "".to_owned(),
53
+ data: (),
54
+ };
55
+
56
+ Ok(HttpResponse::Ok()
57
+ .content_type("application/json")
58
+ .body(serde_json::to_string(&json_response).unwrap()))
59
+ }
src/api/mod.rs CHANGED
@@ -1,6 +1,9 @@
1
  use serde::{Deserialize, Serialize};
2
 
3
  pub(crate) mod tag;
 
 
 
4
 
5
  #[derive(Debug, Deserialize, Serialize)]
6
  struct JsonResponse<T> {
 
1
  use serde::{Deserialize, Serialize};
2
 
3
  pub(crate) mod tag;
4
+ pub(crate) mod kb_info;
5
+ pub(crate) mod dialog_info;
6
+ pub(crate) mod doc_info;
7
 
8
  #[derive(Debug, Deserialize, Serialize)]
9
  struct JsonResponse<T> {
src/entity/doc_info.rs CHANGED
@@ -1,5 +1,6 @@
1
  use sea_orm::entity::prelude::*;
2
  use serde::{Deserialize, Serialize};
 
3
 
4
  #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
5
  #[sea_orm(table_name = "doc_info")]
@@ -9,10 +10,13 @@ pub struct Model {
9
  #[sea_orm(index)]
10
  pub uid: i64,
11
  pub doc_name: String,
12
- pub size: i64,
13
  #[sea_orm(column_name = "type")]
14
  pub r#type: String,
15
  pub kb_progress: f64,
 
 
 
16
 
17
  #[serde(skip_deserializing)]
18
  pub created_at: Date,
 
1
  use sea_orm::entity::prelude::*;
2
  use serde::{Deserialize, Serialize};
3
+ use crate::entity::kb_info;
4
 
5
  #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
6
  #[sea_orm(table_name = "doc_info")]
 
10
  #[sea_orm(index)]
11
  pub uid: i64,
12
  pub doc_name: String,
13
+ pub size: u64,
14
  #[sea_orm(column_name = "type")]
15
  pub r#type: String,
16
  pub kb_progress: f64,
17
+ pub location: String,
18
+ #[sea_orm(ignore)]
19
+ pub kb_infos: Vec<kb_info::Model>,
20
 
21
  #[serde(skip_deserializing)]
22
  pub created_at: Date,
src/entity/mod.rs CHANGED
@@ -3,7 +3,7 @@ pub(crate) mod tag_info;
3
  mod tag2_doc;
4
  mod kb2_doc;
5
  mod dialog2_kb;
6
- mod doc2_doc;
7
  pub(crate) mod kb_info;
8
  pub(crate) mod doc_info;
9
  pub(crate) mod dialog_info;
 
3
  mod tag2_doc;
4
  mod kb2_doc;
5
  mod dialog2_kb;
6
+ pub(crate) mod doc2_doc;
7
  pub(crate) mod kb_info;
8
  pub(crate) mod doc_info;
9
  pub(crate) mod dialog_info;
src/service/dialog_info.rs CHANGED
@@ -1,5 +1,9 @@
1
- use sea_orm::{DbConn, DbErr, EntityTrait, PaginatorTrait, QueryOrder};
2
- use crate::entity::dialog_info;
 
 
 
 
3
  use crate::entity::dialog_info::Entity;
4
 
5
  pub struct Query;
@@ -9,6 +13,17 @@ impl Query {
9
  Entity::find_by_id(id).one(db).await
10
  }
11
 
 
 
 
 
 
 
 
 
 
 
 
12
  pub async fn find_dialog_infos_in_page(
13
  db: &DbConn,
14
  page: u64,
@@ -23,4 +38,61 @@ impl Query {
23
  // Fetch paginated posts
24
  paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
25
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
 
1
+ use chrono::Local;
2
+ use sea_orm::{ActiveModelTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryOrder};
3
+ use sea_orm::ActiveValue::Set;
4
+ use sea_orm::QueryFilter;
5
+ use sea_orm::ColumnTrait;
6
+ use crate::entity::{dialog_info, kb_info};
7
  use crate::entity::dialog_info::Entity;
8
 
9
  pub struct Query;
 
13
  Entity::find_by_id(id).one(db).await
14
  }
15
 
16
+ pub async fn find_dialog_infos(db: &DbConn) -> Result<Vec<dialog_info::Model>, DbErr> {
17
+ Entity::find().all(db).await
18
+ }
19
+
20
+ pub async fn find_dialog_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<dialog_info::Model>, DbErr> {
21
+ Entity::find()
22
+ .filter(dialog_info::Column::Uid.eq(uid))
23
+ .all(db)
24
+ .await
25
+ }
26
+
27
  pub async fn find_dialog_infos_in_page(
28
  db: &DbConn,
29
  page: u64,
 
38
  // Fetch paginated posts
39
  paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
40
  }
41
+ }
42
+
43
+ pub struct Mutation;
44
+
45
+ impl Mutation {
46
+ pub async fn create_dialog_info(
47
+ db: &DbConn,
48
+ form_data: dialog_info::Model,
49
+ ) -> Result<dialog_info::ActiveModel, DbErr> {
50
+ dialog_info::ActiveModel {
51
+ dialog_id: Default::default(),
52
+ uid: Set(form_data.uid.to_owned()),
53
+ dialog_name: Set(form_data.dialog_name.to_owned()),
54
+ history: Set(form_data.history.to_owned()),
55
+ created_at: Set(Local::now().date_naive()),
56
+ updated_at: Set(Local::now().date_naive()),
57
+ }
58
+ .save(db)
59
+ .await
60
+ }
61
+
62
+ pub async fn update_dialog_info_by_id(
63
+ db: &DbConn,
64
+ id: i64,
65
+ form_data: dialog_info::Model,
66
+ ) -> Result<dialog_info::Model, DbErr> {
67
+ let dialog_info: dialog_info::ActiveModel = Entity::find_by_id(id)
68
+ .one(db)
69
+ .await?
70
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
71
+ .map(Into::into)?;
72
+
73
+ dialog_info::ActiveModel {
74
+ dialog_id: dialog_info.dialog_id,
75
+ uid: dialog_info.uid,
76
+ dialog_name: Set(form_data.dialog_name.to_owned()),
77
+ history: Set(form_data.history.to_owned()),
78
+ created_at: Default::default(),
79
+ updated_at: Set(Local::now().date_naive()),
80
+ }
81
+ .update(db)
82
+ .await
83
+ }
84
+
85
+ pub async fn delete_dialog_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
86
+ let tag: dialog_info::ActiveModel = Entity::find_by_id(kb_id)
87
+ .one(db)
88
+ .await?
89
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
90
+ .map(Into::into)?;
91
+
92
+ tag.delete(db).await
93
+ }
94
+
95
+ pub async fn delete_all_dialog_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
96
+ Entity::delete_many().exec(db).await
97
+ }
98
  }
src/service/doc_info.rs ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use chrono::Local;
2
+ use postgres::fallible_iterator::FallibleIterator;
3
+ use sea_orm::{ActiveModelTrait, ColumnTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryOrder};
4
+ use sea_orm::ActiveValue::Set;
5
+ use sea_orm::QueryFilter;
6
+ use crate::api::doc_info::{FilterParams, Params};
7
+ use crate::entity::{doc2_doc, doc_info, kb_info, tag_info};
8
+ use crate::entity::doc_info::Entity;
9
+
10
+ pub struct Query;
11
+
12
+ impl Query {
13
+ pub async fn find_doc_info_by_id(db: &DbConn, id: i64) -> Result<Option<doc_info::Model>, DbErr> {
14
+ Entity::find_by_id(id).one(db).await
15
+ }
16
+
17
+ pub async fn find_doc_infos(db: &DbConn) -> Result<Vec<doc_info::Model>, DbErr> {
18
+ Entity::find().all(db).await
19
+ }
20
+
21
+ pub async fn find_doc_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<doc_info::Model>, DbErr> {
22
+ Entity::find()
23
+ .filter(doc_info::Column::Uid.eq(uid))
24
+ .all(db)
25
+ .await
26
+ }
27
+
28
+ pub async fn find_doc_infos_by_params(db: &DbConn, params: Params) -> Result<Vec<doc_info::Model>, DbErr> {
29
+ // Setup paginator
30
+ let paginator = Entity::find();
31
+
32
+ // Fetch paginated posts
33
+ let mut query = paginator
34
+ .find_with_related(kb_info::Entity);
35
+ if let Some(kb_id) = params.filter.kb_id {
36
+ query = query.filter(kb_info::Column::KbId.eq(kb_id));
37
+ }
38
+ if let Some(folder_id) = params.filter.folder_id {
39
+
40
+ }
41
+ if let Some(tag_id) = params.filter.tag_id {
42
+ query = query.filter(tag_info::Column::Tid.eq(tag_id));
43
+ }
44
+ if let Some(keywords) = params.filter.keywords {
45
+
46
+ }
47
+ Ok(query.order_by_asc(doc_info::Column::Did)
48
+ .all(db)
49
+ .await?
50
+ .into_iter()
51
+ .map(|(mut doc_info, kb_infos)| {
52
+ doc_info.kb_infos = kb_infos;
53
+ doc_info
54
+ })
55
+ .collect())
56
+ }
57
+
58
+ pub async fn find_doc_infos_in_page(
59
+ db: &DbConn,
60
+ page: u64,
61
+ posts_per_page: u64,
62
+ ) -> Result<(Vec<doc_info::Model>, u64), DbErr> {
63
+ // Setup paginator
64
+ let paginator = Entity::find()
65
+ .order_by_asc(doc_info::Column::Did)
66
+ .paginate(db, posts_per_page);
67
+ let num_pages = paginator.num_pages().await?;
68
+
69
+ // Fetch paginated posts
70
+ paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
71
+ }
72
+ }
73
+
74
+ pub struct Mutation;
75
+
76
+ impl Mutation {
77
+
78
+ pub async fn mv_doc_info(
79
+ db: &DbConn,
80
+ dest_did: i64,
81
+ dids: &[i64]
82
+ ) -> Result<(), DbErr> {
83
+ for did in dids {
84
+ let _ = doc2_doc::ActiveModel {
85
+ parent_id: Set(dest_did),
86
+ did: Set(*did),
87
+ }
88
+ .save(db)
89
+ .await
90
+ .unwrap();
91
+ }
92
+
93
+ Ok(())
94
+ }
95
+
96
+ pub async fn create_doc_info(
97
+ db: &DbConn,
98
+ form_data: doc_info::Model,
99
+ ) -> Result<doc_info::ActiveModel, DbErr> {
100
+ doc_info::ActiveModel {
101
+ did: Default::default(),
102
+ uid: Set(form_data.uid.to_owned()),
103
+ doc_name: Set(form_data.doc_name.to_owned()),
104
+ size: Set(form_data.size.to_owned()),
105
+ r#type: Set(form_data.r#type.to_owned()),
106
+ kb_progress: Set(form_data.kb_progress.to_owned()),
107
+ location: Set(form_data.location.to_owned()),
108
+ created_at: Set(Local::now().date_naive()),
109
+ updated_at: Set(Local::now().date_naive()),
110
+ }
111
+ .save(db)
112
+ .await
113
+ }
114
+
115
+ pub async fn update_doc_info_by_id(
116
+ db: &DbConn,
117
+ id: i64,
118
+ form_data: doc_info::Model,
119
+ ) -> Result<doc_info::Model, DbErr> {
120
+ let doc_info: doc_info::ActiveModel = Entity::find_by_id(id)
121
+ .one(db)
122
+ .await?
123
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
124
+ .map(Into::into)?;
125
+
126
+ doc_info::ActiveModel {
127
+ did: doc_info.did,
128
+ uid: Set(form_data.uid.to_owned()),
129
+ doc_name: Set(form_data.doc_name.to_owned()),
130
+ size: Set(form_data.size.to_owned()),
131
+ r#type: Set(form_data.r#type.to_owned()),
132
+ kb_progress: Set(form_data.kb_progress.to_owned()),
133
+ location: Set(form_data.location.to_owned()),
134
+ created_at: Default::default(),
135
+ updated_at: Set(Local::now().date_naive()),
136
+ }
137
+ .update(db)
138
+ .await
139
+ }
140
+
141
+ pub async fn delete_doc_info(db: &DbConn, doc_id: i64) -> Result<DeleteResult, DbErr> {
142
+ let tag: doc_info::ActiveModel = Entity::find_by_id(doc_id)
143
+ .one(db)
144
+ .await?
145
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
146
+ .map(Into::into)?;
147
+
148
+ tag.delete(db).await
149
+ }
150
+
151
+ pub async fn delete_all_doc_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
152
+ Entity::delete_many().exec(db).await
153
+ }
154
+ }
src/service/kb_info.rs ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use chrono::{Local, NaiveDate};
2
+ use sea_orm::{ActiveModelTrait, ColumnTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder};
3
+ use sea_orm::ActiveValue::Set;
4
+ use crate::entity::kb_info;
5
+ use crate::entity::kb_info::Entity;
6
+
7
+ pub struct Query;
8
+
9
+ impl Query {
10
+ pub async fn find_kb_info_by_id(db: &DbConn, id: i64) -> Result<Option<kb_info::Model>, DbErr> {
11
+ Entity::find_by_id(id).one(db).await
12
+ }
13
+
14
+ pub async fn find_kb_infos(db: &DbConn) -> Result<Vec<kb_info::Model>, DbErr> {
15
+ Entity::find().all(db).await
16
+ }
17
+
18
+ pub async fn find_kb_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<kb_info::Model>, DbErr> {
19
+ Entity::find()
20
+ .filter(kb_info::Column::Uid.eq(uid))
21
+ .all(db)
22
+ .await
23
+ }
24
+
25
+ pub async fn find_kb_infos_in_page(
26
+ db: &DbConn,
27
+ page: u64,
28
+ posts_per_page: u64,
29
+ ) -> Result<(Vec<kb_info::Model>, u64), DbErr> {
30
+ // Setup paginator
31
+ let paginator = Entity::find()
32
+ .order_by_asc(kb_info::Column::KbId)
33
+ .paginate(db, posts_per_page);
34
+ let num_pages = paginator.num_pages().await?;
35
+
36
+ // Fetch paginated posts
37
+ paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
38
+ }
39
+ }
40
+
41
+ pub struct Mutation;
42
+
43
+ impl Mutation {
44
+ pub async fn create_kb_info(
45
+ db: &DbConn,
46
+ form_data: kb_info::Model,
47
+ ) -> Result<kb_info::ActiveModel, DbErr> {
48
+ kb_info::ActiveModel {
49
+ kb_id: Default::default(),
50
+ uid: Set(form_data.uid.to_owned()),
51
+ kn_name: Set(form_data.kn_name.to_owned()),
52
+ icon: Set(form_data.icon.to_owned()),
53
+ created_at: Set(Local::now().date_naive()),
54
+ updated_at: Set(Local::now().date_naive()),
55
+ }
56
+ .save(db)
57
+ .await
58
+ }
59
+
60
+ pub async fn update_kb_info_by_id(
61
+ db: &DbConn,
62
+ id: i64,
63
+ form_data: kb_info::Model,
64
+ ) -> Result<kb_info::Model, DbErr> {
65
+ let kb_info: kb_info::ActiveModel = Entity::find_by_id(id)
66
+ .one(db)
67
+ .await?
68
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
69
+ .map(Into::into)?;
70
+
71
+ kb_info::ActiveModel {
72
+ kb_id: kb_info.kb_id,
73
+ uid: kb_info.uid,
74
+ kn_name: Set(form_data.kn_name.to_owned()),
75
+ icon: Set(form_data.icon.to_owned()),
76
+ created_at: Default::default(),
77
+ updated_at: Set(Local::now().date_naive()),
78
+ }
79
+ .update(db)
80
+ .await
81
+ }
82
+
83
+ pub async fn delete_kb_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
84
+ let tag: kb_info::ActiveModel = Entity::find_by_id(kb_id)
85
+ .one(db)
86
+ .await?
87
+ .ok_or(DbErr::Custom("Cannot find.".to_owned()))
88
+ .map(Into::into)?;
89
+
90
+ tag.delete(db).await
91
+ }
92
+
93
+ pub async fn delete_all_kb_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
94
+ Entity::delete_many().exec(db).await
95
+ }
96
+ }
src/service/mod.rs CHANGED
@@ -1,2 +1,4 @@
1
  pub(crate) mod dialog_info;
2
- pub(crate) mod tag_info;
 
 
 
1
  pub(crate) mod dialog_info;
2
+ pub(crate) mod tag_info;
3
+ pub(crate) mod kb_info;
4
+ pub(crate) mod doc_info;
src/service/tag_info.rs CHANGED
@@ -22,7 +22,7 @@ impl Query {
22
  ) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
23
  // Setup paginator
24
  let paginator = Entity::find()
25
- .order_by_asc(tag_info::Column::Uid)
26
  .paginate(db, posts_per_page);
27
  let num_pages = paginator.num_pages().await?;
28
 
 
22
  ) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
23
  // Setup paginator
24
  let paginator = Entity::find()
25
+ .order_by_asc(tag_info::Column::Tid)
26
  .paginate(db, posts_per_page);
27
  let num_pages = paginator.num_pages().await?;
28