KevinHuSh
commited on
Commit
·
6858ec5
1
Parent(s):
8a65ad8
format code (#14)
Browse files- migration/src/m20220101_000001_create_table.rs +282 -231
- python/llm/__init__.py +1 -0
- python/llm/embedding_model.py +31 -0
- python/svr/parse_user_docs.py +2 -4
- python/util/config.py +3 -1
- python/util/db_conn.py +0 -4
- src/api/dialog_info.rs +80 -45
- src/api/doc_info.rs +100 -53
- src/api/kb_info.rs +69 -35
- src/api/mod.rs +2 -2
- src/api/tag_info.rs +31 -19
- src/api/user_info.rs +48 -37
- src/entity/dialog2_kb.rs +12 -10
- src/entity/dialog_info.rs +4 -4
- src/entity/doc2_doc.rs +13 -11
- src/entity/doc_info.rs +3 -3
- src/entity/kb2_doc.rs +13 -11
- src/entity/kb_info.rs +3 -3
- src/entity/mod.rs +1 -1
- src/entity/tag2_doc.rs +12 -10
- src/entity/tag_info.rs +3 -3
- src/entity/user_info.rs +3 -4
- src/errors.rs +23 -28
- src/main.rs +16 -14
- src/service/dialog_info.rs +30 -20
- src/service/doc_info.rs +139 -94
- src/service/kb_info.rs +59 -56
- src/service/mod.rs +1 -1
- src/service/tag_info.rs +32 -28
- src/service/user_info.rs +44 -37
migration/src/m20220101_000001_create_table.rs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
use sea_orm_migration::{prelude::*, sea_orm::Statement};
|
| 2 |
-
use chrono::{FixedOffset, Utc};
|
| 3 |
|
| 4 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 5 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 6 |
}
|
| 7 |
#[derive(DeriveMigrationName)]
|
| 8 |
pub struct Migration;
|
|
@@ -10,226 +10,295 @@ pub struct Migration;
|
|
| 10 |
#[async_trait::async_trait]
|
| 11 |
impl MigrationTrait for Migration {
|
| 12 |
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
| 13 |
-
manager
|
| 14 |
-
|
| 15 |
-
Table
|
| 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 |
-
.col(ColumnDef::new(TagInfo::IsDeleted).boolean().default(false))
|
| 61 |
-
.to_owned(),
|
| 62 |
-
)
|
| 63 |
-
.await?;
|
| 64 |
-
|
| 65 |
-
manager
|
| 66 |
-
.create_table(
|
| 67 |
-
Table::create()
|
| 68 |
-
.table(Tag2Doc::Table)
|
| 69 |
-
.if_not_exists()
|
| 70 |
-
.col(
|
| 71 |
-
ColumnDef::new(Tag2Doc::Id)
|
| 72 |
-
.big_integer()
|
| 73 |
-
.not_null()
|
| 74 |
-
.auto_increment()
|
| 75 |
-
.primary_key(),
|
| 76 |
-
)
|
| 77 |
-
.col(ColumnDef::new(Tag2Doc::TagId).big_integer())
|
| 78 |
-
.col(ColumnDef::new(Tag2Doc::Did).big_integer())
|
| 79 |
-
.to_owned(),
|
| 80 |
-
)
|
| 81 |
-
.await?;
|
| 82 |
-
|
| 83 |
-
manager
|
| 84 |
-
.create_table(
|
| 85 |
-
Table::create()
|
| 86 |
-
.table(Kb2Doc::Table)
|
| 87 |
-
.if_not_exists()
|
| 88 |
-
.col(
|
| 89 |
-
ColumnDef::new(Kb2Doc::Id)
|
| 90 |
-
.big_integer()
|
| 91 |
-
.not_null()
|
| 92 |
-
.auto_increment()
|
| 93 |
-
.primary_key(),
|
| 94 |
-
)
|
| 95 |
-
.col(ColumnDef::new(Kb2Doc::KbId).big_integer())
|
| 96 |
-
.col(ColumnDef::new(Kb2Doc::Did).big_integer())
|
| 97 |
-
.col(ColumnDef::new(Kb2Doc::KbProgress).float().default(0))
|
| 98 |
-
.col(ColumnDef::new(Kb2Doc::KbProgressMsg).string().default(""))
|
| 99 |
-
.col(ColumnDef::new(Kb2Doc::UpdatedAt).timestamp_with_time_zone().default(Expr::current_timestamp()).not_null())
|
| 100 |
-
.col(ColumnDef::new(Kb2Doc::IsDeleted).boolean().default(false))
|
| 101 |
-
.to_owned(),
|
| 102 |
-
)
|
| 103 |
-
.await?;
|
| 104 |
-
|
| 105 |
-
manager
|
| 106 |
-
.create_table(
|
| 107 |
-
Table::create()
|
| 108 |
-
.table(Dialog2Kb::Table)
|
| 109 |
-
.if_not_exists()
|
| 110 |
-
.col(
|
| 111 |
-
ColumnDef::new(Dialog2Kb::Id)
|
| 112 |
-
.big_integer()
|
| 113 |
-
.not_null()
|
| 114 |
-
.auto_increment()
|
| 115 |
-
.primary_key(),
|
| 116 |
-
)
|
| 117 |
-
.col(ColumnDef::new(Dialog2Kb::DialogId).big_integer())
|
| 118 |
-
.col(ColumnDef::new(Dialog2Kb::KbId).big_integer())
|
| 119 |
-
.to_owned(),
|
| 120 |
-
)
|
| 121 |
-
.await?;
|
| 122 |
-
|
| 123 |
-
manager
|
| 124 |
-
.create_table(
|
| 125 |
-
Table::create()
|
| 126 |
-
.table(Doc2Doc::Table)
|
| 127 |
-
.if_not_exists()
|
| 128 |
-
.col(
|
| 129 |
-
ColumnDef::new(Doc2Doc::Id)
|
| 130 |
-
.big_integer()
|
| 131 |
-
.not_null()
|
| 132 |
-
.auto_increment()
|
| 133 |
-
.primary_key(),
|
| 134 |
-
)
|
| 135 |
-
.col(ColumnDef::new(Doc2Doc::ParentId).big_integer())
|
| 136 |
-
.col(ColumnDef::new(Doc2Doc::Did).big_integer())
|
| 137 |
-
.to_owned(),
|
| 138 |
-
)
|
| 139 |
-
.await?;
|
| 140 |
-
|
| 141 |
-
manager
|
| 142 |
-
.create_table(
|
| 143 |
-
Table::create()
|
| 144 |
-
.table(KbInfo::Table)
|
| 145 |
-
.if_not_exists()
|
| 146 |
-
.col(ColumnDef::new(KbInfo::KbId).big_integer()
|
| 147 |
.auto_increment()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
.not_null()
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
.create_table(
|
| 162 |
-
Table::create()
|
| 163 |
-
.table(DocInfo::Table)
|
| 164 |
-
.if_not_exists()
|
| 165 |
-
.col(ColumnDef::new(DocInfo::Did).big_integer()
|
| 166 |
.not_null()
|
| 167 |
.auto_increment()
|
| 168 |
-
.primary_key()
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
.create_table(
|
| 183 |
-
Table::create()
|
| 184 |
-
.table(DialogInfo::Table)
|
| 185 |
-
.if_not_exists()
|
| 186 |
-
.col(ColumnDef::new(DialogInfo::DialogId)
|
| 187 |
-
.big_integer()
|
| 188 |
.not_null()
|
| 189 |
.auto_increment()
|
| 190 |
-
.primary_key()
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
let tm = now();
|
| 203 |
let root_insert = Query::insert()
|
| 204 |
.into_table(UserInfo::Table)
|
| 205 |
.columns([UserInfo::Email, UserInfo::Nickname, UserInfo::Password])
|
| 206 |
-
.values_panic([
|
| 207 |
-
"[email protected]".into(),
|
| 208 |
-
"root".into(),
|
| 209 |
-
"123456".into()
|
| 210 |
-
])
|
| 211 |
.to_owned();
|
| 212 |
|
| 213 |
let doc_insert = Query::insert()
|
| 214 |
.into_table(DocInfo::Table)
|
| 215 |
-
.columns([
|
| 216 |
-
DocInfo::
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
])
|
| 224 |
.to_owned();
|
| 225 |
|
| 226 |
let tag_insert = Query::insert()
|
| 227 |
.into_table(TagInfo::Table)
|
| 228 |
.columns([TagInfo::Uid, TagInfo::TagName, TagInfo::Regx, TagInfo::Color, TagInfo::Icon])
|
| 229 |
-
.values_panic([
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
.to_owned();
|
| 234 |
|
| 235 |
manager.exec_stmt(root_insert).await?;
|
|
@@ -239,41 +308,23 @@ impl MigrationTrait for Migration {
|
|
| 239 |
}
|
| 240 |
|
| 241 |
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
| 242 |
-
manager
|
| 243 |
-
.drop_table(Table::drop().table(UserInfo::Table).to_owned())
|
| 244 |
-
.await?;
|
| 245 |
|
| 246 |
-
manager
|
| 247 |
-
.drop_table(Table::drop().table(TagInfo::Table).to_owned())
|
| 248 |
-
.await?;
|
| 249 |
|
| 250 |
-
manager
|
| 251 |
-
.drop_table(Table::drop().table(Tag2Doc::Table).to_owned())
|
| 252 |
-
.await?;
|
| 253 |
|
| 254 |
-
manager
|
| 255 |
-
.drop_table(Table::drop().table(Kb2Doc::Table).to_owned())
|
| 256 |
-
.await?;
|
| 257 |
|
| 258 |
-
manager
|
| 259 |
-
.drop_table(Table::drop().table(Dialog2Kb::Table).to_owned())
|
| 260 |
-
.await?;
|
| 261 |
|
| 262 |
-
manager
|
| 263 |
-
.drop_table(Table::drop().table(Doc2Doc::Table).to_owned())
|
| 264 |
-
.await?;
|
| 265 |
|
| 266 |
-
manager
|
| 267 |
-
.drop_table(Table::drop().table(KbInfo::Table).to_owned())
|
| 268 |
-
.await?;
|
| 269 |
|
| 270 |
-
manager
|
| 271 |
-
.drop_table(Table::drop().table(DocInfo::Table).to_owned())
|
| 272 |
-
.await?;
|
| 273 |
|
| 274 |
-
manager
|
| 275 |
-
.drop_table(Table::drop().table(DialogInfo::Table).to_owned())
|
| 276 |
-
.await?;
|
| 277 |
|
| 278 |
Ok(())
|
| 279 |
}
|
|
|
|
| 1 |
+
use sea_orm_migration::{ prelude::*, sea_orm::Statement };
|
| 2 |
+
use chrono::{ FixedOffset, Utc };
|
| 3 |
|
| 4 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 5 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 6 |
}
|
| 7 |
#[derive(DeriveMigrationName)]
|
| 8 |
pub struct Migration;
|
|
|
|
| 10 |
#[async_trait::async_trait]
|
| 11 |
impl MigrationTrait for Migration {
|
| 12 |
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
| 13 |
+
manager.create_table(
|
| 14 |
+
Table::create()
|
| 15 |
+
.table(UserInfo::Table)
|
| 16 |
+
.if_not_exists()
|
| 17 |
+
.col(
|
| 18 |
+
ColumnDef::new(UserInfo::Uid)
|
| 19 |
+
.big_integer()
|
| 20 |
+
.not_null()
|
| 21 |
+
.auto_increment()
|
| 22 |
+
.primary_key()
|
| 23 |
+
)
|
| 24 |
+
.col(ColumnDef::new(UserInfo::Email).string().not_null())
|
| 25 |
+
.col(ColumnDef::new(UserInfo::Nickname).string().not_null())
|
| 26 |
+
.col(ColumnDef::new(UserInfo::AvatarBase64).string())
|
| 27 |
+
.col(ColumnDef::new(UserInfo::ColorScheme).string().default("dark"))
|
| 28 |
+
.col(ColumnDef::new(UserInfo::ListStyle).string().default("list"))
|
| 29 |
+
.col(ColumnDef::new(UserInfo::Language).string().default("chinese"))
|
| 30 |
+
.col(ColumnDef::new(UserInfo::Password).string().not_null())
|
| 31 |
+
.col(
|
| 32 |
+
ColumnDef::new(UserInfo::LastLoginAt)
|
| 33 |
+
.timestamp_with_time_zone()
|
| 34 |
+
.default(Expr::current_timestamp())
|
| 35 |
+
)
|
| 36 |
+
.col(
|
| 37 |
+
ColumnDef::new(UserInfo::CreatedAt)
|
| 38 |
+
.timestamp_with_time_zone()
|
| 39 |
+
.default(Expr::current_timestamp())
|
| 40 |
+
.not_null()
|
| 41 |
+
)
|
| 42 |
+
.col(
|
| 43 |
+
ColumnDef::new(UserInfo::UpdatedAt)
|
| 44 |
+
.timestamp_with_time_zone()
|
| 45 |
+
.default(Expr::current_timestamp())
|
| 46 |
+
.not_null()
|
| 47 |
+
)
|
| 48 |
+
.col(ColumnDef::new(UserInfo::IsDeleted).boolean().default(false))
|
| 49 |
+
.to_owned()
|
| 50 |
+
).await?;
|
| 51 |
+
|
| 52 |
+
manager.create_table(
|
| 53 |
+
Table::create()
|
| 54 |
+
.table(TagInfo::Table)
|
| 55 |
+
.if_not_exists()
|
| 56 |
+
.col(
|
| 57 |
+
ColumnDef::new(TagInfo::Tid)
|
| 58 |
+
.big_integer()
|
| 59 |
+
.not_null()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
.auto_increment()
|
| 61 |
+
.primary_key()
|
| 62 |
+
)
|
| 63 |
+
.col(ColumnDef::new(TagInfo::Uid).big_integer().not_null())
|
| 64 |
+
.col(ColumnDef::new(TagInfo::TagName).string().not_null())
|
| 65 |
+
.col(ColumnDef::new(TagInfo::Regx).string())
|
| 66 |
+
.col(ColumnDef::new(TagInfo::Color).tiny_unsigned().default(1))
|
| 67 |
+
.col(ColumnDef::new(TagInfo::Icon).tiny_unsigned().default(1))
|
| 68 |
+
.col(ColumnDef::new(TagInfo::FolderId).big_integer())
|
| 69 |
+
.col(
|
| 70 |
+
ColumnDef::new(TagInfo::CreatedAt)
|
| 71 |
+
.timestamp_with_time_zone()
|
| 72 |
+
.default(Expr::current_timestamp())
|
| 73 |
+
.not_null()
|
| 74 |
+
)
|
| 75 |
+
.col(
|
| 76 |
+
ColumnDef::new(TagInfo::UpdatedAt)
|
| 77 |
+
.timestamp_with_time_zone()
|
| 78 |
+
.default(Expr::current_timestamp())
|
| 79 |
.not_null()
|
| 80 |
+
)
|
| 81 |
+
.col(ColumnDef::new(TagInfo::IsDeleted).boolean().default(false))
|
| 82 |
+
.to_owned()
|
| 83 |
+
).await?;
|
| 84 |
+
|
| 85 |
+
manager.create_table(
|
| 86 |
+
Table::create()
|
| 87 |
+
.table(Tag2Doc::Table)
|
| 88 |
+
.if_not_exists()
|
| 89 |
+
.col(
|
| 90 |
+
ColumnDef::new(Tag2Doc::Id)
|
| 91 |
+
.big_integer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
.not_null()
|
| 93 |
.auto_increment()
|
| 94 |
+
.primary_key()
|
| 95 |
+
)
|
| 96 |
+
.col(ColumnDef::new(Tag2Doc::TagId).big_integer())
|
| 97 |
+
.col(ColumnDef::new(Tag2Doc::Did).big_integer())
|
| 98 |
+
.to_owned()
|
| 99 |
+
).await?;
|
| 100 |
+
|
| 101 |
+
manager.create_table(
|
| 102 |
+
Table::create()
|
| 103 |
+
.table(Kb2Doc::Table)
|
| 104 |
+
.if_not_exists()
|
| 105 |
+
.col(
|
| 106 |
+
ColumnDef::new(Kb2Doc::Id)
|
| 107 |
+
.big_integer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
.not_null()
|
| 109 |
.auto_increment()
|
| 110 |
+
.primary_key()
|
| 111 |
+
)
|
| 112 |
+
.col(ColumnDef::new(Kb2Doc::KbId).big_integer())
|
| 113 |
+
.col(ColumnDef::new(Kb2Doc::Did).big_integer())
|
| 114 |
+
.col(ColumnDef::new(Kb2Doc::KbProgress).float().default(0))
|
| 115 |
+
.col(ColumnDef::new(Kb2Doc::KbProgressMsg).string().default(""))
|
| 116 |
+
.col(
|
| 117 |
+
ColumnDef::new(Kb2Doc::UpdatedAt)
|
| 118 |
+
.timestamp_with_time_zone()
|
| 119 |
+
.default(Expr::current_timestamp())
|
| 120 |
+
.not_null()
|
| 121 |
+
)
|
| 122 |
+
.col(ColumnDef::new(Kb2Doc::IsDeleted).boolean().default(false))
|
| 123 |
+
.to_owned()
|
| 124 |
+
).await?;
|
| 125 |
+
|
| 126 |
+
manager.create_table(
|
| 127 |
+
Table::create()
|
| 128 |
+
.table(Dialog2Kb::Table)
|
| 129 |
+
.if_not_exists()
|
| 130 |
+
.col(
|
| 131 |
+
ColumnDef::new(Dialog2Kb::Id)
|
| 132 |
+
.big_integer()
|
| 133 |
+
.not_null()
|
| 134 |
+
.auto_increment()
|
| 135 |
+
.primary_key()
|
| 136 |
+
)
|
| 137 |
+
.col(ColumnDef::new(Dialog2Kb::DialogId).big_integer())
|
| 138 |
+
.col(ColumnDef::new(Dialog2Kb::KbId).big_integer())
|
| 139 |
+
.to_owned()
|
| 140 |
+
).await?;
|
| 141 |
+
|
| 142 |
+
manager.create_table(
|
| 143 |
+
Table::create()
|
| 144 |
+
.table(Doc2Doc::Table)
|
| 145 |
+
.if_not_exists()
|
| 146 |
+
.col(
|
| 147 |
+
ColumnDef::new(Doc2Doc::Id)
|
| 148 |
+
.big_integer()
|
| 149 |
+
.not_null()
|
| 150 |
+
.auto_increment()
|
| 151 |
+
.primary_key()
|
| 152 |
+
)
|
| 153 |
+
.col(ColumnDef::new(Doc2Doc::ParentId).big_integer())
|
| 154 |
+
.col(ColumnDef::new(Doc2Doc::Did).big_integer())
|
| 155 |
+
.to_owned()
|
| 156 |
+
).await?;
|
| 157 |
+
|
| 158 |
+
manager.create_table(
|
| 159 |
+
Table::create()
|
| 160 |
+
.table(KbInfo::Table)
|
| 161 |
+
.if_not_exists()
|
| 162 |
+
.col(
|
| 163 |
+
ColumnDef::new(KbInfo::KbId)
|
| 164 |
+
.big_integer()
|
| 165 |
+
.auto_increment()
|
| 166 |
+
.not_null()
|
| 167 |
+
.primary_key()
|
| 168 |
+
)
|
| 169 |
+
.col(ColumnDef::new(KbInfo::Uid).big_integer().not_null())
|
| 170 |
+
.col(ColumnDef::new(KbInfo::KbName).string().not_null())
|
| 171 |
+
.col(ColumnDef::new(KbInfo::Icon).tiny_unsigned().default(1))
|
| 172 |
+
.col(
|
| 173 |
+
ColumnDef::new(KbInfo::CreatedAt)
|
| 174 |
+
.timestamp_with_time_zone()
|
| 175 |
+
.default(Expr::current_timestamp())
|
| 176 |
+
.not_null()
|
| 177 |
+
)
|
| 178 |
+
.col(
|
| 179 |
+
ColumnDef::new(KbInfo::UpdatedAt)
|
| 180 |
+
.timestamp_with_time_zone()
|
| 181 |
+
.default(Expr::current_timestamp())
|
| 182 |
+
.not_null()
|
| 183 |
+
)
|
| 184 |
+
.col(ColumnDef::new(KbInfo::IsDeleted).boolean().default(false))
|
| 185 |
+
.to_owned()
|
| 186 |
+
).await?;
|
| 187 |
+
|
| 188 |
+
manager.create_table(
|
| 189 |
+
Table::create()
|
| 190 |
+
.table(DocInfo::Table)
|
| 191 |
+
.if_not_exists()
|
| 192 |
+
.col(
|
| 193 |
+
ColumnDef::new(DocInfo::Did)
|
| 194 |
+
.big_integer()
|
| 195 |
+
.not_null()
|
| 196 |
+
.auto_increment()
|
| 197 |
+
.primary_key()
|
| 198 |
+
)
|
| 199 |
+
.col(ColumnDef::new(DocInfo::Uid).big_integer().not_null())
|
| 200 |
+
.col(ColumnDef::new(DocInfo::DocName).string().not_null())
|
| 201 |
+
.col(ColumnDef::new(DocInfo::Location).string().not_null())
|
| 202 |
+
.col(ColumnDef::new(DocInfo::Size).big_integer().not_null())
|
| 203 |
+
.col(ColumnDef::new(DocInfo::Type).string().not_null())
|
| 204 |
+
.comment("doc|folder")
|
| 205 |
+
.col(
|
| 206 |
+
ColumnDef::new(DocInfo::CreatedAt)
|
| 207 |
+
.timestamp_with_time_zone()
|
| 208 |
+
.default(Expr::current_timestamp())
|
| 209 |
+
.not_null()
|
| 210 |
+
)
|
| 211 |
+
.col(
|
| 212 |
+
ColumnDef::new(DocInfo::UpdatedAt)
|
| 213 |
+
.timestamp_with_time_zone()
|
| 214 |
+
.default(Expr::current_timestamp())
|
| 215 |
+
.not_null()
|
| 216 |
+
)
|
| 217 |
+
.col(ColumnDef::new(DocInfo::IsDeleted).boolean().default(false))
|
| 218 |
+
.to_owned()
|
| 219 |
+
).await?;
|
| 220 |
+
|
| 221 |
+
manager.create_table(
|
| 222 |
+
Table::create()
|
| 223 |
+
.table(DialogInfo::Table)
|
| 224 |
+
.if_not_exists()
|
| 225 |
+
.col(
|
| 226 |
+
ColumnDef::new(DialogInfo::DialogId)
|
| 227 |
+
.big_integer()
|
| 228 |
+
.not_null()
|
| 229 |
+
.auto_increment()
|
| 230 |
+
.primary_key()
|
| 231 |
+
)
|
| 232 |
+
.col(ColumnDef::new(DialogInfo::Uid).big_integer().not_null())
|
| 233 |
+
.col(ColumnDef::new(DialogInfo::KbId).big_integer().not_null())
|
| 234 |
+
.col(ColumnDef::new(DialogInfo::DialogName).string().not_null())
|
| 235 |
+
.col(ColumnDef::new(DialogInfo::History).string().comment("json"))
|
| 236 |
+
.col(
|
| 237 |
+
ColumnDef::new(DialogInfo::CreatedAt)
|
| 238 |
+
.timestamp_with_time_zone()
|
| 239 |
+
.default(Expr::current_timestamp())
|
| 240 |
+
.not_null()
|
| 241 |
+
)
|
| 242 |
+
.col(
|
| 243 |
+
ColumnDef::new(DialogInfo::UpdatedAt)
|
| 244 |
+
.timestamp_with_time_zone()
|
| 245 |
+
.default(Expr::current_timestamp())
|
| 246 |
+
.not_null()
|
| 247 |
+
)
|
| 248 |
+
.col(ColumnDef::new(DialogInfo::IsDeleted).boolean().default(false))
|
| 249 |
+
.to_owned()
|
| 250 |
+
).await?;
|
| 251 |
|
| 252 |
let tm = now();
|
| 253 |
let root_insert = Query::insert()
|
| 254 |
.into_table(UserInfo::Table)
|
| 255 |
.columns([UserInfo::Email, UserInfo::Nickname, UserInfo::Password])
|
| 256 |
+
.values_panic(["[email protected]".into(), "root".into(), "123456".into()])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
.to_owned();
|
| 258 |
|
| 259 |
let doc_insert = Query::insert()
|
| 260 |
.into_table(DocInfo::Table)
|
| 261 |
+
.columns([
|
| 262 |
+
DocInfo::Uid,
|
| 263 |
+
DocInfo::DocName,
|
| 264 |
+
DocInfo::Size,
|
| 265 |
+
DocInfo::Type,
|
| 266 |
+
DocInfo::Location,
|
| 267 |
+
])
|
| 268 |
+
.values_panic([(1).into(), "/".into(), (0).into(), "folder".into(), "".into()])
|
|
|
|
| 269 |
.to_owned();
|
| 270 |
|
| 271 |
let tag_insert = Query::insert()
|
| 272 |
.into_table(TagInfo::Table)
|
| 273 |
.columns([TagInfo::Uid, TagInfo::TagName, TagInfo::Regx, TagInfo::Color, TagInfo::Icon])
|
| 274 |
+
.values_panic([
|
| 275 |
+
(1).into(),
|
| 276 |
+
"视频".into(),
|
| 277 |
+
".*\\.(mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa)".into(),
|
| 278 |
+
(1).into(),
|
| 279 |
+
(1).into(),
|
| 280 |
+
])
|
| 281 |
+
.values_panic([
|
| 282 |
+
(1).into(),
|
| 283 |
+
"图片".into(),
|
| 284 |
+
".*\\.(png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng)".into(),
|
| 285 |
+
(2).into(),
|
| 286 |
+
(2).into(),
|
| 287 |
+
])
|
| 288 |
+
.values_panic([
|
| 289 |
+
(1).into(),
|
| 290 |
+
"音乐".into(),
|
| 291 |
+
".*\\.(WAV|FLAC|APE|ALAC|WavPack|WV|MP3|AAC|Ogg|Vorbis|Opus)".into(),
|
| 292 |
+
(3).into(),
|
| 293 |
+
(3).into(),
|
| 294 |
+
])
|
| 295 |
+
.values_panic([
|
| 296 |
+
(1).into(),
|
| 297 |
+
"文档".into(),
|
| 298 |
+
".*\\.(pdf|doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp)".into(),
|
| 299 |
+
(3).into(),
|
| 300 |
+
(3).into(),
|
| 301 |
+
])
|
| 302 |
.to_owned();
|
| 303 |
|
| 304 |
manager.exec_stmt(root_insert).await?;
|
|
|
|
| 308 |
}
|
| 309 |
|
| 310 |
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
| 311 |
+
manager.drop_table(Table::drop().table(UserInfo::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 312 |
|
| 313 |
+
manager.drop_table(Table::drop().table(TagInfo::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 314 |
|
| 315 |
+
manager.drop_table(Table::drop().table(Tag2Doc::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 316 |
|
| 317 |
+
manager.drop_table(Table::drop().table(Kb2Doc::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
manager.drop_table(Table::drop().table(Dialog2Kb::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 320 |
|
| 321 |
+
manager.drop_table(Table::drop().table(Doc2Doc::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
manager.drop_table(Table::drop().table(KbInfo::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 324 |
|
| 325 |
+
manager.drop_table(Table::drop().table(DocInfo::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 326 |
|
| 327 |
+
manager.drop_table(Table::drop().table(DialogInfo::Table).to_owned()).await?;
|
|
|
|
|
|
|
| 328 |
|
| 329 |
Ok(())
|
| 330 |
}
|
python/llm/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from .embedding_model import HuEmbedding
|
python/llm/embedding_model.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC
|
| 2 |
+
from FlagEmbedding import FlagModel
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
class Base(ABC):
|
| 6 |
+
def encode(self, texts: list, batch_size=32):
|
| 7 |
+
raise NotImplementedError("Please implement encode method!")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class HuEmbedding(Base):
|
| 11 |
+
def __init__(self):
|
| 12 |
+
"""
|
| 13 |
+
If you have trouble downloading HuggingFace models, -_^ this might help!!
|
| 14 |
+
|
| 15 |
+
For Linux:
|
| 16 |
+
export HF_ENDPOINT=https://hf-mirror.com
|
| 17 |
+
|
| 18 |
+
For Windows:
|
| 19 |
+
Good luck
|
| 20 |
+
^_-
|
| 21 |
+
|
| 22 |
+
"""
|
| 23 |
+
self.model = FlagModel("BAAI/bge-large-zh-v1.5",
|
| 24 |
+
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
|
| 25 |
+
use_fp16=torch.cuda.is_available())
|
| 26 |
+
|
| 27 |
+
def encode(self, texts: list, batch_size=32):
|
| 28 |
+
res = []
|
| 29 |
+
for i in range(0, len(texts), batch_size):
|
| 30 |
+
res.extend(self.encode(texts[i:i+batch_size]))
|
| 31 |
+
return res
|
python/svr/parse_user_docs.py
CHANGED
|
@@ -209,10 +209,8 @@ def rm_doc_from_kb(df):
|
|
| 209 |
|
| 210 |
def main(comm, mod):
|
| 211 |
global model
|
| 212 |
-
from
|
| 213 |
-
model =
|
| 214 |
-
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
|
| 215 |
-
use_fp16=torch.cuda.is_available())
|
| 216 |
tm_fnm = f"res/{comm}-{mod}.tm"
|
| 217 |
tm = findMaxDt(tm_fnm)
|
| 218 |
rows = collect(comm, mod, tm)
|
|
|
|
| 209 |
|
| 210 |
def main(comm, mod):
|
| 211 |
global model
|
| 212 |
+
from llm import HuEmbedding
|
| 213 |
+
model = HuEmbedding()
|
|
|
|
|
|
|
| 214 |
tm_fnm = f"res/{comm}-{mod}.tm"
|
| 215 |
tm = findMaxDt(tm_fnm)
|
| 216 |
rows = collect(comm, mod, tm)
|
python/util/config.py
CHANGED
|
@@ -16,7 +16,9 @@ class Config:
|
|
| 16 |
|
| 17 |
def get(self, key, default=None):
|
| 18 |
global CF
|
| 19 |
-
return
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def init(env):
|
| 22 |
return Config(env)
|
|
|
|
| 16 |
|
| 17 |
def get(self, key, default=None):
|
| 18 |
global CF
|
| 19 |
+
return os.environ.get(key.upper(), \
|
| 20 |
+
CF[self.env].get(key, default)
|
| 21 |
+
)
|
| 22 |
|
| 23 |
def init(env):
|
| 24 |
return Config(env)
|
python/util/db_conn.py
CHANGED
|
@@ -49,11 +49,7 @@ class Postgres(object):
|
|
| 49 |
cur = self.conn.cursor()
|
| 50 |
cur.execute(sql)
|
| 51 |
updated_rows = cur.rowcount
|
| 52 |
-
<<<<<<< HEAD
|
| 53 |
self.conn.commit()
|
| 54 |
-
=======
|
| 55 |
-
conn.commit()
|
| 56 |
-
>>>>>>> upstream/main
|
| 57 |
cur.close()
|
| 58 |
return updated_rows
|
| 59 |
except Exception as e:
|
|
|
|
| 49 |
cur = self.conn.cursor()
|
| 50 |
cur.execute(sql)
|
| 51 |
updated_rows = cur.rowcount
|
|
|
|
| 52 |
self.conn.commit()
|
|
|
|
|
|
|
|
|
|
| 53 |
cur.close()
|
| 54 |
return updated_rows
|
| 55 |
except Exception as e:
|
src/api/dialog_info.rs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
-
use actix_web::{HttpResponse, post, web};
|
| 3 |
use serde::Deserialize;
|
| 4 |
use serde_json::Value;
|
| 5 |
use serde_json::json;
|
| 6 |
use crate::api::JsonResponse;
|
| 7 |
use crate::AppState;
|
| 8 |
-
use crate::entity::dialog_info;
|
| 9 |
use crate::errors::AppError;
|
| 10 |
use crate::service::dialog_info::Query;
|
| 11 |
use crate::service::dialog_info::Mutation;
|
|
@@ -13,17 +12,23 @@ use crate::service::dialog_info::Mutation;
|
|
| 13 |
#[derive(Debug, Deserialize)]
|
| 14 |
pub struct ListParams {
|
| 15 |
pub uid: i64,
|
| 16 |
-
pub dialog_id: Option<i64
|
| 17 |
}
|
| 18 |
#[post("/v1.0/dialogs")]
|
| 19 |
-
async fn list(
|
|
|
|
|
|
|
|
|
|
| 20 |
let mut result = HashMap::new();
|
| 21 |
-
if let Some(dia_id) = params.dialog_id{
|
| 22 |
let dia = Query::find_dialog_info_by_id(&data.conn, dia_id).await?.unwrap();
|
| 23 |
-
let kb = crate::service::kb_info::Query
|
|
|
|
|
|
|
| 24 |
print!("{:?}", dia.history);
|
| 25 |
-
let hist:Value = serde_json::from_str(&dia.history)?;
|
| 26 |
-
let detail =
|
|
|
|
| 27 |
"dialog_id": dia_id,
|
| 28 |
"dialog_name": dia.dialog_name.to_owned(),
|
| 29 |
"created_at": dia.created_at.to_string().to_owned(),
|
|
@@ -33,20 +38,23 @@ async fn list(params: web::Json<ListParams>, data: web::Data<AppState>) -> Resul
|
|
| 33 |
});
|
| 34 |
|
| 35 |
result.insert("dialogs", vec![detail]);
|
| 36 |
-
}
|
| 37 |
-
else{
|
| 38 |
let mut dias = Vec::<Value>::new();
|
| 39 |
-
for dia in Query::find_dialog_infos_by_uid(&data.conn, params.uid).await?{
|
| 40 |
-
let kb = crate::service::kb_info::Query
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
"dialog_id": dia.dialog_id,
|
| 44 |
"dialog_name": dia.dialog_name.to_owned(),
|
| 45 |
"created_at": dia.created_at.to_string().to_owned(),
|
| 46 |
"updated_at": dia.updated_at.to_string().to_owned(),
|
| 47 |
"history": hist,
|
| 48 |
"kb_info": kb
|
| 49 |
-
})
|
|
|
|
| 50 |
}
|
| 51 |
result.insert("dialogs", dias);
|
| 52 |
}
|
|
@@ -56,18 +64,23 @@ async fn list(params: web::Json<ListParams>, data: web::Data<AppState>) -> Resul
|
|
| 56 |
data: result,
|
| 57 |
};
|
| 58 |
|
| 59 |
-
Ok(
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
#[derive(Debug, Deserialize)]
|
| 65 |
pub struct RmParams {
|
| 66 |
pub uid: i64,
|
| 67 |
-
pub dialog_id: i64
|
| 68 |
}
|
| 69 |
#[post("/v1.0/delete_dialog")]
|
| 70 |
-
async fn delete(
|
|
|
|
|
|
|
|
|
|
| 71 |
let _ = Mutation::delete_dialog_info(&data.conn, params.dialog_id).await?;
|
| 72 |
|
| 73 |
let json_response = JsonResponse {
|
|
@@ -76,9 +89,11 @@ async fn delete(params: web::Json<RmParams>, data: web::Data<AppState>) -> Resul
|
|
| 76 |
data: (),
|
| 77 |
};
|
| 78 |
|
| 79 |
-
Ok(
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
#[derive(Debug, Deserialize)]
|
|
@@ -86,18 +101,30 @@ pub struct CreateParams {
|
|
| 86 |
pub uid: i64,
|
| 87 |
pub dialog_id: Option<i64>,
|
| 88 |
pub kb_id: i64,
|
| 89 |
-
pub name: String
|
| 90 |
}
|
| 91 |
#[post("/v1.0/create_dialog")]
|
| 92 |
-
async fn create(
|
|
|
|
|
|
|
|
|
|
| 93 |
let mut result = HashMap::new();
|
| 94 |
if let Some(dia_id) = param.dialog_id {
|
| 95 |
result.insert("dialog_id", dia_id);
|
| 96 |
let dia = Query::find_dialog_info_by_id(&data.conn, dia_id).await?;
|
| 97 |
-
let _ = Mutation::update_dialog_info_by_id(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
result.insert("dialog_id", dia.dialog_id.unwrap());
|
| 102 |
}
|
| 103 |
|
|
@@ -107,37 +134,45 @@ async fn create(param: web::Json<CreateParams>, data: web::Data<AppState>) -> Re
|
|
| 107 |
data: result,
|
| 108 |
};
|
| 109 |
|
| 110 |
-
Ok(
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
-
|
| 116 |
#[derive(Debug, Deserialize)]
|
| 117 |
pub struct UpdateHistoryParams {
|
| 118 |
pub uid: i64,
|
| 119 |
pub dialog_id: i64,
|
| 120 |
-
pub history: Value
|
| 121 |
}
|
| 122 |
#[post("/v1.0/update_history")]
|
| 123 |
-
async fn update_history(
|
|
|
|
|
|
|
|
|
|
| 124 |
let mut json_response = JsonResponse {
|
| 125 |
code: 200,
|
| 126 |
err: "".to_owned(),
|
| 127 |
data: (),
|
| 128 |
};
|
| 129 |
|
| 130 |
-
if let Some(dia) = Query::find_dialog_info_by_id(&data.conn, param.dialog_id).await?{
|
| 131 |
-
let _ = Mutation::update_dialog_info_by_id(
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
json_response.code = 500;
|
| 136 |
json_response.err = "Can't find dialog data!".to_owned();
|
| 137 |
}
|
| 138 |
-
|
| 139 |
|
| 140 |
-
Ok(
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
+
use actix_web::{ HttpResponse, post, web };
|
| 3 |
use serde::Deserialize;
|
| 4 |
use serde_json::Value;
|
| 5 |
use serde_json::json;
|
| 6 |
use crate::api::JsonResponse;
|
| 7 |
use crate::AppState;
|
|
|
|
| 8 |
use crate::errors::AppError;
|
| 9 |
use crate::service::dialog_info::Query;
|
| 10 |
use crate::service::dialog_info::Mutation;
|
|
|
|
| 12 |
#[derive(Debug, Deserialize)]
|
| 13 |
pub struct ListParams {
|
| 14 |
pub uid: i64,
|
| 15 |
+
pub dialog_id: Option<i64>,
|
| 16 |
}
|
| 17 |
#[post("/v1.0/dialogs")]
|
| 18 |
+
async fn list(
|
| 19 |
+
params: web::Json<ListParams>,
|
| 20 |
+
data: web::Data<AppState>
|
| 21 |
+
) -> Result<HttpResponse, AppError> {
|
| 22 |
let mut result = HashMap::new();
|
| 23 |
+
if let Some(dia_id) = params.dialog_id {
|
| 24 |
let dia = Query::find_dialog_info_by_id(&data.conn, dia_id).await?.unwrap();
|
| 25 |
+
let kb = crate::service::kb_info::Query
|
| 26 |
+
::find_kb_info_by_id(&data.conn, dia.kb_id).await?
|
| 27 |
+
.unwrap();
|
| 28 |
print!("{:?}", dia.history);
|
| 29 |
+
let hist: Value = serde_json::from_str(&dia.history)?;
|
| 30 |
+
let detail =
|
| 31 |
+
json!({
|
| 32 |
"dialog_id": dia_id,
|
| 33 |
"dialog_name": dia.dialog_name.to_owned(),
|
| 34 |
"created_at": dia.created_at.to_string().to_owned(),
|
|
|
|
| 38 |
});
|
| 39 |
|
| 40 |
result.insert("dialogs", vec![detail]);
|
| 41 |
+
} else {
|
|
|
|
| 42 |
let mut dias = Vec::<Value>::new();
|
| 43 |
+
for dia in Query::find_dialog_infos_by_uid(&data.conn, params.uid).await? {
|
| 44 |
+
let kb = crate::service::kb_info::Query
|
| 45 |
+
::find_kb_info_by_id(&data.conn, dia.kb_id).await?
|
| 46 |
+
.unwrap();
|
| 47 |
+
let hist: Value = serde_json::from_str(&dia.history)?;
|
| 48 |
+
dias.push(
|
| 49 |
+
json!({
|
| 50 |
"dialog_id": dia.dialog_id,
|
| 51 |
"dialog_name": dia.dialog_name.to_owned(),
|
| 52 |
"created_at": dia.created_at.to_string().to_owned(),
|
| 53 |
"updated_at": dia.updated_at.to_string().to_owned(),
|
| 54 |
"history": hist,
|
| 55 |
"kb_info": kb
|
| 56 |
+
})
|
| 57 |
+
);
|
| 58 |
}
|
| 59 |
result.insert("dialogs", dias);
|
| 60 |
}
|
|
|
|
| 64 |
data: result,
|
| 65 |
};
|
| 66 |
|
| 67 |
+
Ok(
|
| 68 |
+
HttpResponse::Ok()
|
| 69 |
+
.content_type("application/json")
|
| 70 |
+
.body(serde_json::to_string(&json_response)?)
|
| 71 |
+
)
|
| 72 |
}
|
| 73 |
|
| 74 |
#[derive(Debug, Deserialize)]
|
| 75 |
pub struct RmParams {
|
| 76 |
pub uid: i64,
|
| 77 |
+
pub dialog_id: i64,
|
| 78 |
}
|
| 79 |
#[post("/v1.0/delete_dialog")]
|
| 80 |
+
async fn delete(
|
| 81 |
+
params: web::Json<RmParams>,
|
| 82 |
+
data: web::Data<AppState>
|
| 83 |
+
) -> Result<HttpResponse, AppError> {
|
| 84 |
let _ = Mutation::delete_dialog_info(&data.conn, params.dialog_id).await?;
|
| 85 |
|
| 86 |
let json_response = JsonResponse {
|
|
|
|
| 89 |
data: (),
|
| 90 |
};
|
| 91 |
|
| 92 |
+
Ok(
|
| 93 |
+
HttpResponse::Ok()
|
| 94 |
+
.content_type("application/json")
|
| 95 |
+
.body(serde_json::to_string(&json_response)?)
|
| 96 |
+
)
|
| 97 |
}
|
| 98 |
|
| 99 |
#[derive(Debug, Deserialize)]
|
|
|
|
| 101 |
pub uid: i64,
|
| 102 |
pub dialog_id: Option<i64>,
|
| 103 |
pub kb_id: i64,
|
| 104 |
+
pub name: String,
|
| 105 |
}
|
| 106 |
#[post("/v1.0/create_dialog")]
|
| 107 |
+
async fn create(
|
| 108 |
+
param: web::Json<CreateParams>,
|
| 109 |
+
data: web::Data<AppState>
|
| 110 |
+
) -> Result<HttpResponse, AppError> {
|
| 111 |
let mut result = HashMap::new();
|
| 112 |
if let Some(dia_id) = param.dialog_id {
|
| 113 |
result.insert("dialog_id", dia_id);
|
| 114 |
let dia = Query::find_dialog_info_by_id(&data.conn, dia_id).await?;
|
| 115 |
+
let _ = Mutation::update_dialog_info_by_id(
|
| 116 |
+
&data.conn,
|
| 117 |
+
dia_id,
|
| 118 |
+
¶m.name,
|
| 119 |
+
&dia.unwrap().history
|
| 120 |
+
).await?;
|
| 121 |
+
} else {
|
| 122 |
+
let dia = Mutation::create_dialog_info(
|
| 123 |
+
&data.conn,
|
| 124 |
+
param.uid,
|
| 125 |
+
param.kb_id,
|
| 126 |
+
¶m.name
|
| 127 |
+
).await?;
|
| 128 |
result.insert("dialog_id", dia.dialog_id.unwrap());
|
| 129 |
}
|
| 130 |
|
|
|
|
| 134 |
data: result,
|
| 135 |
};
|
| 136 |
|
| 137 |
+
Ok(
|
| 138 |
+
HttpResponse::Ok()
|
| 139 |
+
.content_type("application/json")
|
| 140 |
+
.body(serde_json::to_string(&json_response)?)
|
| 141 |
+
)
|
| 142 |
}
|
| 143 |
|
|
|
|
| 144 |
#[derive(Debug, Deserialize)]
|
| 145 |
pub struct UpdateHistoryParams {
|
| 146 |
pub uid: i64,
|
| 147 |
pub dialog_id: i64,
|
| 148 |
+
pub history: Value,
|
| 149 |
}
|
| 150 |
#[post("/v1.0/update_history")]
|
| 151 |
+
async fn update_history(
|
| 152 |
+
param: web::Json<UpdateHistoryParams>,
|
| 153 |
+
data: web::Data<AppState>
|
| 154 |
+
) -> Result<HttpResponse, AppError> {
|
| 155 |
let mut json_response = JsonResponse {
|
| 156 |
code: 200,
|
| 157 |
err: "".to_owned(),
|
| 158 |
data: (),
|
| 159 |
};
|
| 160 |
|
| 161 |
+
if let Some(dia) = Query::find_dialog_info_by_id(&data.conn, param.dialog_id).await? {
|
| 162 |
+
let _ = Mutation::update_dialog_info_by_id(
|
| 163 |
+
&data.conn,
|
| 164 |
+
param.dialog_id,
|
| 165 |
+
&dia.dialog_name,
|
| 166 |
+
¶m.history.to_string()
|
| 167 |
+
).await?;
|
| 168 |
+
} else {
|
| 169 |
json_response.code = 500;
|
| 170 |
json_response.err = "Can't find dialog data!".to_owned();
|
| 171 |
}
|
|
|
|
| 172 |
|
| 173 |
+
Ok(
|
| 174 |
+
HttpResponse::Ok()
|
| 175 |
+
.content_type("application/json")
|
| 176 |
+
.body(serde_json::to_string(&json_response)?)
|
| 177 |
+
)
|
| 178 |
+
}
|
src/api/doc_info.rs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
use std::io::Write;
|
| 3 |
-
use actix_multipart_extract::{File, Multipart, MultipartForm};
|
| 4 |
-
use actix_web::{
|
| 5 |
-
use chrono::{Utc, FixedOffset};
|
| 6 |
use sea_orm::DbConn;
|
| 7 |
use crate::api::JsonResponse;
|
| 8 |
use crate::AppState;
|
| 9 |
use crate::entity::doc_info::Model;
|
| 10 |
use crate::errors::AppError;
|
| 11 |
-
use crate::service::doc_info::{Mutation, Query};
|
| 12 |
use serde::Deserialize;
|
| 13 |
|
| 14 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 15 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 16 |
}
|
| 17 |
|
| 18 |
#[derive(Debug, Deserialize)]
|
|
@@ -33,9 +33,11 @@ pub struct FilterParams {
|
|
| 33 |
}
|
| 34 |
|
| 35 |
#[post("/v1.0/docs")]
|
| 36 |
-
async fn list(
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
let mut result = HashMap::new();
|
| 41 |
result.insert("docs", docs);
|
|
@@ -46,53 +48,78 @@ async fn list(params: web::Json<ListParams>, data: web::Data<AppState>) -> Resul
|
|
| 46 |
data: result,
|
| 47 |
};
|
| 48 |
|
| 49 |
-
Ok(
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
#[derive(Deserialize, MultipartForm, Debug)]
|
| 55 |
pub struct UploadForm {
|
| 56 |
#[multipart(max_size = 512MB)]
|
| 57 |
file_field: File,
|
| 58 |
-
uid: i64,
|
| 59 |
-
did: i64
|
| 60 |
}
|
| 61 |
|
| 62 |
#[post("/v1.0/upload")]
|
| 63 |
-
async fn upload(
|
|
|
|
|
|
|
|
|
|
| 64 |
let uid = payload.uid;
|
| 65 |
-
async fn add_number_to_filename(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
let mut i = 0;
|
| 67 |
let mut new_file_name = file_name.to_string();
|
| 68 |
let arr: Vec<&str> = file_name.split(".").collect();
|
| 69 |
-
let suffix = String::from(arr[arr.len()-1]);
|
| 70 |
-
let preffix = arr[..arr.len()-1].join(".");
|
| 71 |
-
let mut docs = Query::find_doc_infos_by_name(
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
i += 1;
|
| 74 |
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
| 75 |
-
docs = Query::find_doc_infos_by_name(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
new_file_name
|
| 78 |
}
|
| 79 |
-
let fnm = add_number_to_filename(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
std::fs::create_dir_all(format!("./upload/{}/", uid));
|
| 82 |
let filepath = format!("./upload/{}/{}-{}", payload.uid, payload.did, fnm.clone());
|
| 83 |
-
let mut f =std::fs::File::create(&filepath)?;
|
| 84 |
f.write(&payload.file_field.bytes)?;
|
| 85 |
-
|
| 86 |
let doc = Mutation::create_doc_info(&data.conn, Model {
|
| 87 |
-
did:Default::default(),
|
| 88 |
-
uid:
|
| 89 |
doc_name: fnm,
|
| 90 |
size: payload.file_field.bytes.len() as i64,
|
| 91 |
location: filepath,
|
| 92 |
r#type: "doc".to_string(),
|
| 93 |
created_at: now(),
|
| 94 |
updated_at: now(),
|
| 95 |
-
is_deleted:Default::default(),
|
| 96 |
}).await?;
|
| 97 |
|
| 98 |
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
|
@@ -102,11 +129,14 @@ async fn upload(payload: Multipart<UploadForm>, data: web::Data<AppState>) -> Re
|
|
| 102 |
|
| 103 |
#[derive(Deserialize, Debug)]
|
| 104 |
pub struct RmDocsParam {
|
| 105 |
-
uid: i64,
|
| 106 |
-
dids: Vec<i64
|
| 107 |
}
|
| 108 |
#[post("/v1.0/delete_docs")]
|
| 109 |
-
async fn delete(
|
|
|
|
|
|
|
|
|
|
| 110 |
let _ = Mutation::delete_doc_info(&data.conn, ¶ms.dids).await?;
|
| 111 |
|
| 112 |
let json_response = JsonResponse {
|
|
@@ -115,20 +145,25 @@ async fn delete(params: web::Json<RmDocsParam>, data: web::Data<AppState>) -> Re
|
|
| 115 |
data: (),
|
| 116 |
};
|
| 117 |
|
| 118 |
-
Ok(
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
}
|
| 122 |
|
| 123 |
#[derive(Debug, Deserialize)]
|
| 124 |
pub struct MvParams {
|
| 125 |
-
pub uid:i64,
|
| 126 |
pub dids: Vec<i64>,
|
| 127 |
pub dest_did: i64,
|
| 128 |
}
|
| 129 |
|
| 130 |
#[post("/v1.0/mv_docs")]
|
| 131 |
-
async fn mv(
|
|
|
|
|
|
|
|
|
|
| 132 |
Mutation::mv_doc_info(&data.conn, params.dest_did, ¶ms.dids).await?;
|
| 133 |
|
| 134 |
let json_response = JsonResponse {
|
|
@@ -137,30 +172,35 @@ async fn mv(params: web::Json<MvParams>, data: web::Data<AppState>) -> Result<Ht
|
|
| 137 |
data: (),
|
| 138 |
};
|
| 139 |
|
| 140 |
-
Ok(
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
|
| 145 |
#[derive(Debug, Deserialize)]
|
| 146 |
pub struct NewFoldParams {
|
| 147 |
pub uid: i64,
|
| 148 |
pub parent_id: i64,
|
| 149 |
-
pub name: String
|
| 150 |
}
|
| 151 |
|
| 152 |
#[post("/v1.0/new_folder")]
|
| 153 |
-
async fn new_folder(
|
|
|
|
|
|
|
|
|
|
| 154 |
let doc = Mutation::create_doc_info(&data.conn, Model {
|
| 155 |
-
did:Default::default(),
|
| 156 |
-
uid:
|
| 157 |
doc_name: params.name.to_string(),
|
| 158 |
-
size:0,
|
| 159 |
r#type: "folder".to_string(),
|
| 160 |
location: "".to_owned(),
|
| 161 |
created_at: now(),
|
| 162 |
updated_at: now(),
|
| 163 |
-
is_deleted:Default::default(),
|
| 164 |
}).await?;
|
| 165 |
let _ = Mutation::place_doc(&data.conn, params.parent_id, doc.did.unwrap()).await?;
|
| 166 |
|
|
@@ -171,21 +211,26 @@ async fn new_folder(params: web::Json<NewFoldParams>, data: web::Data<AppState>)
|
|
| 171 |
pub struct RenameParams {
|
| 172 |
pub uid: i64,
|
| 173 |
pub did: i64,
|
| 174 |
-
pub name: String
|
| 175 |
}
|
| 176 |
|
| 177 |
#[post("/v1.0/rename")]
|
| 178 |
-
async fn rename(
|
|
|
|
|
|
|
|
|
|
| 179 |
let docs = Query::find_doc_infos_by_name(&data.conn, params.uid, ¶ms.name, None).await?;
|
| 180 |
-
if docs.len()>0{
|
| 181 |
let json_response = JsonResponse {
|
| 182 |
code: 500,
|
| 183 |
err: "Name duplicated!".to_owned(),
|
| 184 |
data: (),
|
| 185 |
};
|
| 186 |
-
return Ok(
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
}
|
| 190 |
let doc = Mutation::rename(&data.conn, params.did, ¶ms.name).await?;
|
| 191 |
|
|
@@ -195,7 +240,9 @@ async fn rename(params: web::Json<RenameParams>, data: web::Data<AppState>) -> R
|
|
| 195 |
data: doc,
|
| 196 |
};
|
| 197 |
|
| 198 |
-
Ok(
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
| 201 |
}
|
|
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
use std::io::Write;
|
| 3 |
+
use actix_multipart_extract::{ File, Multipart, MultipartForm };
|
| 4 |
+
use actix_web::{ HttpResponse, post, web };
|
| 5 |
+
use chrono::{ Utc, FixedOffset };
|
| 6 |
use sea_orm::DbConn;
|
| 7 |
use crate::api::JsonResponse;
|
| 8 |
use crate::AppState;
|
| 9 |
use crate::entity::doc_info::Model;
|
| 10 |
use crate::errors::AppError;
|
| 11 |
+
use crate::service::doc_info::{ Mutation, Query };
|
| 12 |
use serde::Deserialize;
|
| 13 |
|
| 14 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 15 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 16 |
}
|
| 17 |
|
| 18 |
#[derive(Debug, Deserialize)]
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
#[post("/v1.0/docs")]
|
| 36 |
+
async fn list(
|
| 37 |
+
params: web::Json<ListParams>,
|
| 38 |
+
data: web::Data<AppState>
|
| 39 |
+
) -> Result<HttpResponse, AppError> {
|
| 40 |
+
let docs = Query::find_doc_infos_by_params(&data.conn, params.into_inner()).await?;
|
| 41 |
|
| 42 |
let mut result = HashMap::new();
|
| 43 |
result.insert("docs", docs);
|
|
|
|
| 48 |
data: result,
|
| 49 |
};
|
| 50 |
|
| 51 |
+
Ok(
|
| 52 |
+
HttpResponse::Ok()
|
| 53 |
+
.content_type("application/json")
|
| 54 |
+
.body(serde_json::to_string(&json_response)?)
|
| 55 |
+
)
|
| 56 |
}
|
| 57 |
|
| 58 |
#[derive(Deserialize, MultipartForm, Debug)]
|
| 59 |
pub struct UploadForm {
|
| 60 |
#[multipart(max_size = 512MB)]
|
| 61 |
file_field: File,
|
| 62 |
+
uid: i64,
|
| 63 |
+
did: i64,
|
| 64 |
}
|
| 65 |
|
| 66 |
#[post("/v1.0/upload")]
|
| 67 |
+
async fn upload(
|
| 68 |
+
payload: Multipart<UploadForm>,
|
| 69 |
+
data: web::Data<AppState>
|
| 70 |
+
) -> Result<HttpResponse, AppError> {
|
| 71 |
let uid = payload.uid;
|
| 72 |
+
async fn add_number_to_filename(
|
| 73 |
+
file_name: String,
|
| 74 |
+
conn: &DbConn,
|
| 75 |
+
uid: i64,
|
| 76 |
+
parent_id: i64
|
| 77 |
+
) -> String {
|
| 78 |
let mut i = 0;
|
| 79 |
let mut new_file_name = file_name.to_string();
|
| 80 |
let arr: Vec<&str> = file_name.split(".").collect();
|
| 81 |
+
let suffix = String::from(arr[arr.len() - 1]);
|
| 82 |
+
let preffix = arr[..arr.len() - 1].join(".");
|
| 83 |
+
let mut docs = Query::find_doc_infos_by_name(
|
| 84 |
+
conn,
|
| 85 |
+
uid,
|
| 86 |
+
&new_file_name,
|
| 87 |
+
Some(parent_id)
|
| 88 |
+
).await.unwrap();
|
| 89 |
+
while docs.len() > 0 {
|
| 90 |
i += 1;
|
| 91 |
new_file_name = format!("{}_{}.{}", preffix, i, suffix);
|
| 92 |
+
docs = Query::find_doc_infos_by_name(
|
| 93 |
+
conn,
|
| 94 |
+
uid,
|
| 95 |
+
&new_file_name,
|
| 96 |
+
Some(parent_id)
|
| 97 |
+
).await.unwrap();
|
| 98 |
}
|
| 99 |
new_file_name
|
| 100 |
}
|
| 101 |
+
let fnm = add_number_to_filename(
|
| 102 |
+
payload.file_field.name.clone(),
|
| 103 |
+
&data.conn,
|
| 104 |
+
uid,
|
| 105 |
+
payload.did
|
| 106 |
+
).await;
|
| 107 |
|
| 108 |
std::fs::create_dir_all(format!("./upload/{}/", uid));
|
| 109 |
let filepath = format!("./upload/{}/{}-{}", payload.uid, payload.did, fnm.clone());
|
| 110 |
+
let mut f = std::fs::File::create(&filepath)?;
|
| 111 |
f.write(&payload.file_field.bytes)?;
|
| 112 |
+
|
| 113 |
let doc = Mutation::create_doc_info(&data.conn, Model {
|
| 114 |
+
did: Default::default(),
|
| 115 |
+
uid: uid,
|
| 116 |
doc_name: fnm,
|
| 117 |
size: payload.file_field.bytes.len() as i64,
|
| 118 |
location: filepath,
|
| 119 |
r#type: "doc".to_string(),
|
| 120 |
created_at: now(),
|
| 121 |
updated_at: now(),
|
| 122 |
+
is_deleted: Default::default(),
|
| 123 |
}).await?;
|
| 124 |
|
| 125 |
let _ = Mutation::place_doc(&data.conn, payload.did, doc.did.unwrap()).await?;
|
|
|
|
| 129 |
|
| 130 |
#[derive(Deserialize, Debug)]
|
| 131 |
pub struct RmDocsParam {
|
| 132 |
+
uid: i64,
|
| 133 |
+
dids: Vec<i64>,
|
| 134 |
}
|
| 135 |
#[post("/v1.0/delete_docs")]
|
| 136 |
+
async fn delete(
|
| 137 |
+
params: web::Json<RmDocsParam>,
|
| 138 |
+
data: web::Data<AppState>
|
| 139 |
+
) -> Result<HttpResponse, AppError> {
|
| 140 |
let _ = Mutation::delete_doc_info(&data.conn, ¶ms.dids).await?;
|
| 141 |
|
| 142 |
let json_response = JsonResponse {
|
|
|
|
| 145 |
data: (),
|
| 146 |
};
|
| 147 |
|
| 148 |
+
Ok(
|
| 149 |
+
HttpResponse::Ok()
|
| 150 |
+
.content_type("application/json")
|
| 151 |
+
.body(serde_json::to_string(&json_response)?)
|
| 152 |
+
)
|
| 153 |
}
|
| 154 |
|
| 155 |
#[derive(Debug, Deserialize)]
|
| 156 |
pub struct MvParams {
|
| 157 |
+
pub uid: i64,
|
| 158 |
pub dids: Vec<i64>,
|
| 159 |
pub dest_did: i64,
|
| 160 |
}
|
| 161 |
|
| 162 |
#[post("/v1.0/mv_docs")]
|
| 163 |
+
async fn mv(
|
| 164 |
+
params: web::Json<MvParams>,
|
| 165 |
+
data: web::Data<AppState>
|
| 166 |
+
) -> Result<HttpResponse, AppError> {
|
| 167 |
Mutation::mv_doc_info(&data.conn, params.dest_did, ¶ms.dids).await?;
|
| 168 |
|
| 169 |
let json_response = JsonResponse {
|
|
|
|
| 172 |
data: (),
|
| 173 |
};
|
| 174 |
|
| 175 |
+
Ok(
|
| 176 |
+
HttpResponse::Ok()
|
| 177 |
+
.content_type("application/json")
|
| 178 |
+
.body(serde_json::to_string(&json_response)?)
|
| 179 |
+
)
|
| 180 |
}
|
| 181 |
|
| 182 |
#[derive(Debug, Deserialize)]
|
| 183 |
pub struct NewFoldParams {
|
| 184 |
pub uid: i64,
|
| 185 |
pub parent_id: i64,
|
| 186 |
+
pub name: String,
|
| 187 |
}
|
| 188 |
|
| 189 |
#[post("/v1.0/new_folder")]
|
| 190 |
+
async fn new_folder(
|
| 191 |
+
params: web::Json<NewFoldParams>,
|
| 192 |
+
data: web::Data<AppState>
|
| 193 |
+
) -> Result<HttpResponse, AppError> {
|
| 194 |
let doc = Mutation::create_doc_info(&data.conn, Model {
|
| 195 |
+
did: Default::default(),
|
| 196 |
+
uid: params.uid,
|
| 197 |
doc_name: params.name.to_string(),
|
| 198 |
+
size: 0,
|
| 199 |
r#type: "folder".to_string(),
|
| 200 |
location: "".to_owned(),
|
| 201 |
created_at: now(),
|
| 202 |
updated_at: now(),
|
| 203 |
+
is_deleted: Default::default(),
|
| 204 |
}).await?;
|
| 205 |
let _ = Mutation::place_doc(&data.conn, params.parent_id, doc.did.unwrap()).await?;
|
| 206 |
|
|
|
|
| 211 |
pub struct RenameParams {
|
| 212 |
pub uid: i64,
|
| 213 |
pub did: i64,
|
| 214 |
+
pub name: String,
|
| 215 |
}
|
| 216 |
|
| 217 |
#[post("/v1.0/rename")]
|
| 218 |
+
async fn rename(
|
| 219 |
+
params: web::Json<RenameParams>,
|
| 220 |
+
data: web::Data<AppState>
|
| 221 |
+
) -> Result<HttpResponse, AppError> {
|
| 222 |
let docs = Query::find_doc_infos_by_name(&data.conn, params.uid, ¶ms.name, None).await?;
|
| 223 |
+
if docs.len() > 0 {
|
| 224 |
let json_response = JsonResponse {
|
| 225 |
code: 500,
|
| 226 |
err: "Name duplicated!".to_owned(),
|
| 227 |
data: (),
|
| 228 |
};
|
| 229 |
+
return Ok(
|
| 230 |
+
HttpResponse::Ok()
|
| 231 |
+
.content_type("application/json")
|
| 232 |
+
.body(serde_json::to_string(&json_response)?)
|
| 233 |
+
);
|
| 234 |
}
|
| 235 |
let doc = Mutation::rename(&data.conn, params.did, ¶ms.name).await?;
|
| 236 |
|
|
|
|
| 240 |
data: doc,
|
| 241 |
};
|
| 242 |
|
| 243 |
+
Ok(
|
| 244 |
+
HttpResponse::Ok()
|
| 245 |
+
.content_type("application/json")
|
| 246 |
+
.body(serde_json::to_string(&json_response)?)
|
| 247 |
+
)
|
| 248 |
}
|
src/api/kb_info.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
-
use actix_web::{get, HttpResponse, post, web};
|
| 3 |
use serde::Serialize;
|
| 4 |
use crate::api::JsonResponse;
|
| 5 |
use crate::AppState;
|
|
@@ -16,18 +16,26 @@ pub struct AddDocs2KbParams {
|
|
| 16 |
pub kb_id: i64,
|
| 17 |
}
|
| 18 |
#[post("/v1.0/create_kb")]
|
| 19 |
-
async fn create(
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
let json_response = JsonResponse {
|
| 23 |
code: 201,
|
| 24 |
err: "Duplicated name.".to_owned(),
|
| 25 |
-
data: ()
|
| 26 |
};
|
| 27 |
-
Ok(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
let model = Mutation::create_kb_info(&data.conn, model.into_inner()).await?;
|
| 32 |
|
| 33 |
let mut result = HashMap::new();
|
|
@@ -39,14 +47,19 @@ async fn create(model: web::Json<kb_info::Model>, data: web::Data<AppState>) ->
|
|
| 39 |
data: result,
|
| 40 |
};
|
| 41 |
|
| 42 |
-
Ok(
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
#[post("/v1.0/add_docs_to_kb")]
|
| 49 |
-
async fn add_docs_to_kb(
|
|
|
|
|
|
|
|
|
|
| 50 |
let _ = Mutation::add_docs(&data.conn, param.kb_id, param.dids.to_owned()).await?;
|
| 51 |
|
| 52 |
let json_response = JsonResponse {
|
|
@@ -55,13 +68,18 @@ async fn add_docs_to_kb(param: web::Json<AddDocs2KbParams>, data: web::Data<AppS
|
|
| 55 |
data: (),
|
| 56 |
};
|
| 57 |
|
| 58 |
-
Ok(
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
#[post("/v1.0/anti_kb_docs")]
|
| 64 |
-
async fn anti_kb_docs(
|
|
|
|
|
|
|
|
|
|
| 65 |
let _ = Mutation::remove_docs(&data.conn, param.dids.to_owned(), Some(param.kb_id)).await?;
|
| 66 |
|
| 67 |
let json_response = JsonResponse {
|
|
@@ -70,12 +88,17 @@ async fn anti_kb_docs(param: web::Json<AddDocs2KbParams>, data: web::Data<AppSta
|
|
| 70 |
data: (),
|
| 71 |
};
|
| 72 |
|
| 73 |
-
Ok(
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
#[get("/v1.0/kbs")]
|
| 78 |
-
async fn list(
|
|
|
|
|
|
|
|
|
|
| 79 |
let kbs = Query::find_kb_infos_by_uid(&data.conn, model.uid).await?;
|
| 80 |
|
| 81 |
let mut result = HashMap::new();
|
|
@@ -87,13 +110,18 @@ async fn list(model: web::Json<kb_info::Model>, data: web::Data<AppState>) -> Re
|
|
| 87 |
data: result,
|
| 88 |
};
|
| 89 |
|
| 90 |
-
Ok(
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
#[post("/v1.0/delete_kb")]
|
| 96 |
-
async fn delete(
|
|
|
|
|
|
|
|
|
|
| 97 |
let _ = Mutation::delete_kb_info(&data.conn, model.kb_id).await?;
|
| 98 |
|
| 99 |
let json_response = JsonResponse {
|
|
@@ -102,19 +130,24 @@ async fn delete(model: web::Json<kb_info::Model>, data: web::Data<AppState>) ->
|
|
| 102 |
data: (),
|
| 103 |
};
|
| 104 |
|
| 105 |
-
Ok(
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
}
|
| 109 |
|
| 110 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
| 111 |
pub struct DocIdsParams {
|
| 112 |
pub uid: i64,
|
| 113 |
-
pub dids: Vec<i64
|
| 114 |
}
|
| 115 |
|
| 116 |
#[post("/v1.0/all_relevents")]
|
| 117 |
-
async fn all_relevents(
|
|
|
|
|
|
|
|
|
|
| 118 |
let dids = crate::service::doc_info::Query::all_descendent_ids(&data.conn, ¶ms.dids).await?;
|
| 119 |
let mut result = HashMap::new();
|
| 120 |
let kbs = Query::find_kb_by_docs(&data.conn, dids).await?;
|
|
@@ -125,8 +158,9 @@ async fn all_relevents(params: web::Json<DocIdsParams>, data: web::Data<AppState
|
|
| 125 |
data: result,
|
| 126 |
};
|
| 127 |
|
| 128 |
-
Ok(
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
+
use actix_web::{ get, HttpResponse, post, web };
|
| 3 |
use serde::Serialize;
|
| 4 |
use crate::api::JsonResponse;
|
| 5 |
use crate::AppState;
|
|
|
|
| 16 |
pub kb_id: i64,
|
| 17 |
}
|
| 18 |
#[post("/v1.0/create_kb")]
|
| 19 |
+
async fn create(
|
| 20 |
+
model: web::Json<kb_info::Model>,
|
| 21 |
+
data: web::Data<AppState>
|
| 22 |
+
) -> Result<HttpResponse, AppError> {
|
| 23 |
+
let mut docs = Query::find_kb_infos_by_name(
|
| 24 |
+
&data.conn,
|
| 25 |
+
model.kb_name.to_owned()
|
| 26 |
+
).await.unwrap();
|
| 27 |
+
if docs.len() > 0 {
|
| 28 |
let json_response = JsonResponse {
|
| 29 |
code: 201,
|
| 30 |
err: "Duplicated name.".to_owned(),
|
| 31 |
+
data: (),
|
| 32 |
};
|
| 33 |
+
Ok(
|
| 34 |
+
HttpResponse::Ok()
|
| 35 |
+
.content_type("application/json")
|
| 36 |
+
.body(serde_json::to_string(&json_response)?)
|
| 37 |
+
)
|
| 38 |
+
} else {
|
| 39 |
let model = Mutation::create_kb_info(&data.conn, model.into_inner()).await?;
|
| 40 |
|
| 41 |
let mut result = HashMap::new();
|
|
|
|
| 47 |
data: result,
|
| 48 |
};
|
| 49 |
|
| 50 |
+
Ok(
|
| 51 |
+
HttpResponse::Ok()
|
| 52 |
+
.content_type("application/json")
|
| 53 |
+
.body(serde_json::to_string(&json_response)?)
|
| 54 |
+
)
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
#[post("/v1.0/add_docs_to_kb")]
|
| 59 |
+
async fn add_docs_to_kb(
|
| 60 |
+
param: web::Json<AddDocs2KbParams>,
|
| 61 |
+
data: web::Data<AppState>
|
| 62 |
+
) -> Result<HttpResponse, AppError> {
|
| 63 |
let _ = Mutation::add_docs(&data.conn, param.kb_id, param.dids.to_owned()).await?;
|
| 64 |
|
| 65 |
let json_response = JsonResponse {
|
|
|
|
| 68 |
data: (),
|
| 69 |
};
|
| 70 |
|
| 71 |
+
Ok(
|
| 72 |
+
HttpResponse::Ok()
|
| 73 |
+
.content_type("application/json")
|
| 74 |
+
.body(serde_json::to_string(&json_response)?)
|
| 75 |
+
)
|
| 76 |
}
|
| 77 |
|
| 78 |
#[post("/v1.0/anti_kb_docs")]
|
| 79 |
+
async fn anti_kb_docs(
|
| 80 |
+
param: web::Json<AddDocs2KbParams>,
|
| 81 |
+
data: web::Data<AppState>
|
| 82 |
+
) -> Result<HttpResponse, AppError> {
|
| 83 |
let _ = Mutation::remove_docs(&data.conn, param.dids.to_owned(), Some(param.kb_id)).await?;
|
| 84 |
|
| 85 |
let json_response = JsonResponse {
|
|
|
|
| 88 |
data: (),
|
| 89 |
};
|
| 90 |
|
| 91 |
+
Ok(
|
| 92 |
+
HttpResponse::Ok()
|
| 93 |
+
.content_type("application/json")
|
| 94 |
+
.body(serde_json::to_string(&json_response)?)
|
| 95 |
+
)
|
| 96 |
}
|
| 97 |
#[get("/v1.0/kbs")]
|
| 98 |
+
async fn list(
|
| 99 |
+
model: web::Json<kb_info::Model>,
|
| 100 |
+
data: web::Data<AppState>
|
| 101 |
+
) -> Result<HttpResponse, AppError> {
|
| 102 |
let kbs = Query::find_kb_infos_by_uid(&data.conn, model.uid).await?;
|
| 103 |
|
| 104 |
let mut result = HashMap::new();
|
|
|
|
| 110 |
data: result,
|
| 111 |
};
|
| 112 |
|
| 113 |
+
Ok(
|
| 114 |
+
HttpResponse::Ok()
|
| 115 |
+
.content_type("application/json")
|
| 116 |
+
.body(serde_json::to_string(&json_response)?)
|
| 117 |
+
)
|
| 118 |
}
|
| 119 |
|
| 120 |
#[post("/v1.0/delete_kb")]
|
| 121 |
+
async fn delete(
|
| 122 |
+
model: web::Json<kb_info::Model>,
|
| 123 |
+
data: web::Data<AppState>
|
| 124 |
+
) -> Result<HttpResponse, AppError> {
|
| 125 |
let _ = Mutation::delete_kb_info(&data.conn, model.kb_id).await?;
|
| 126 |
|
| 127 |
let json_response = JsonResponse {
|
|
|
|
| 130 |
data: (),
|
| 131 |
};
|
| 132 |
|
| 133 |
+
Ok(
|
| 134 |
+
HttpResponse::Ok()
|
| 135 |
+
.content_type("application/json")
|
| 136 |
+
.body(serde_json::to_string(&json_response)?)
|
| 137 |
+
)
|
| 138 |
}
|
| 139 |
|
| 140 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
| 141 |
pub struct DocIdsParams {
|
| 142 |
pub uid: i64,
|
| 143 |
+
pub dids: Vec<i64>,
|
| 144 |
}
|
| 145 |
|
| 146 |
#[post("/v1.0/all_relevents")]
|
| 147 |
+
async fn all_relevents(
|
| 148 |
+
params: web::Json<DocIdsParams>,
|
| 149 |
+
data: web::Data<AppState>
|
| 150 |
+
) -> Result<HttpResponse, AppError> {
|
| 151 |
let dids = crate::service::doc_info::Query::all_descendent_ids(&data.conn, ¶ms.dids).await?;
|
| 152 |
let mut result = HashMap::new();
|
| 153 |
let kbs = Query::find_kb_by_docs(&data.conn, dids).await?;
|
|
|
|
| 158 |
data: result,
|
| 159 |
};
|
| 160 |
|
| 161 |
+
Ok(
|
| 162 |
+
HttpResponse::Ok()
|
| 163 |
+
.content_type("application/json")
|
| 164 |
+
.body(serde_json::to_string(&json_response)?)
|
| 165 |
+
)
|
| 166 |
+
}
|
src/api/mod.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
use serde::{Deserialize, Serialize};
|
| 2 |
|
| 3 |
pub(crate) mod tag_info;
|
| 4 |
pub(crate) mod kb_info;
|
|
@@ -11,4 +11,4 @@ struct JsonResponse<T> {
|
|
| 11 |
code: u32,
|
| 12 |
err: String,
|
| 13 |
data: T,
|
| 14 |
-
}
|
|
|
|
| 1 |
+
use serde::{ Deserialize, Serialize };
|
| 2 |
|
| 3 |
pub(crate) mod tag_info;
|
| 4 |
pub(crate) mod kb_info;
|
|
|
|
| 11 |
code: u32,
|
| 12 |
err: String,
|
| 13 |
data: T,
|
| 14 |
+
}
|
src/api/tag_info.rs
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
-
use actix_web::{get, HttpResponse, post, web};
|
| 3 |
-
use actix_web_httpauth::middleware::HttpAuthentication;
|
| 4 |
use serde::Deserialize;
|
| 5 |
-
use crate::validator;
|
| 6 |
use crate::api::JsonResponse;
|
| 7 |
use crate::AppState;
|
| 8 |
use crate::entity::tag_info;
|
| 9 |
use crate::errors::AppError;
|
| 10 |
-
use crate::service::tag_info::{Mutation, Query};
|
| 11 |
|
| 12 |
#[derive(Debug, Deserialize)]
|
| 13 |
pub struct TagListParams {
|
| 14 |
-
pub uid: i64
|
| 15 |
}
|
| 16 |
|
| 17 |
#[post("/v1.0/create_tag")]
|
| 18 |
-
async fn create(
|
|
|
|
|
|
|
|
|
|
| 19 |
let model = Mutation::create_tag(&data.conn, model.into_inner()).await?;
|
| 20 |
|
| 21 |
let mut result = HashMap::new();
|
|
@@ -27,13 +28,18 @@ async fn create(model: web::Json<tag_info::Model>, data: web::Data<AppState>) ->
|
|
| 27 |
data: result,
|
| 28 |
};
|
| 29 |
|
| 30 |
-
Ok(
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
#[post("/v1.0/delete_tag")]
|
| 36 |
-
async fn delete(
|
|
|
|
|
|
|
|
|
|
| 37 |
let _ = Mutation::delete_tag(&data.conn, model.tid).await?;
|
| 38 |
|
| 39 |
let json_response = JsonResponse {
|
|
@@ -42,16 +48,20 @@ async fn delete(model: web::Json<tag_info::Model>, data: web::Data<AppState>) ->
|
|
| 42 |
data: (),
|
| 43 |
};
|
| 44 |
|
| 45 |
-
Ok(
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
-
|
| 51 |
//#[get("/v1.0/tags", wrap = "HttpAuthentication::bearer(validator)")]
|
| 52 |
|
| 53 |
#[post("/v1.0/tags")]
|
| 54 |
-
async fn list(
|
|
|
|
|
|
|
|
|
|
| 55 |
let tags = Query::find_tags_by_uid(param.uid, &data.conn).await?;
|
| 56 |
|
| 57 |
let mut result = HashMap::new();
|
|
@@ -63,7 +73,9 @@ async fn list(param: web::Json<TagListParams>, data: web::Data<AppState>) -> Res
|
|
| 63 |
data: result,
|
| 64 |
};
|
| 65 |
|
| 66 |
-
Ok(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
| 1 |
use std::collections::HashMap;
|
| 2 |
+
use actix_web::{ get, HttpResponse, post, web };
|
|
|
|
| 3 |
use serde::Deserialize;
|
|
|
|
| 4 |
use crate::api::JsonResponse;
|
| 5 |
use crate::AppState;
|
| 6 |
use crate::entity::tag_info;
|
| 7 |
use crate::errors::AppError;
|
| 8 |
+
use crate::service::tag_info::{ Mutation, Query };
|
| 9 |
|
| 10 |
#[derive(Debug, Deserialize)]
|
| 11 |
pub struct TagListParams {
|
| 12 |
+
pub uid: i64,
|
| 13 |
}
|
| 14 |
|
| 15 |
#[post("/v1.0/create_tag")]
|
| 16 |
+
async fn create(
|
| 17 |
+
model: web::Json<tag_info::Model>,
|
| 18 |
+
data: web::Data<AppState>
|
| 19 |
+
) -> Result<HttpResponse, AppError> {
|
| 20 |
let model = Mutation::create_tag(&data.conn, model.into_inner()).await?;
|
| 21 |
|
| 22 |
let mut result = HashMap::new();
|
|
|
|
| 28 |
data: result,
|
| 29 |
};
|
| 30 |
|
| 31 |
+
Ok(
|
| 32 |
+
HttpResponse::Ok()
|
| 33 |
+
.content_type("application/json")
|
| 34 |
+
.body(serde_json::to_string(&json_response)?)
|
| 35 |
+
)
|
| 36 |
}
|
| 37 |
|
| 38 |
#[post("/v1.0/delete_tag")]
|
| 39 |
+
async fn delete(
|
| 40 |
+
model: web::Json<tag_info::Model>,
|
| 41 |
+
data: web::Data<AppState>
|
| 42 |
+
) -> Result<HttpResponse, AppError> {
|
| 43 |
let _ = Mutation::delete_tag(&data.conn, model.tid).await?;
|
| 44 |
|
| 45 |
let json_response = JsonResponse {
|
|
|
|
| 48 |
data: (),
|
| 49 |
};
|
| 50 |
|
| 51 |
+
Ok(
|
| 52 |
+
HttpResponse::Ok()
|
| 53 |
+
.content_type("application/json")
|
| 54 |
+
.body(serde_json::to_string(&json_response)?)
|
| 55 |
+
)
|
| 56 |
}
|
| 57 |
|
|
|
|
| 58 |
//#[get("/v1.0/tags", wrap = "HttpAuthentication::bearer(validator)")]
|
| 59 |
|
| 60 |
#[post("/v1.0/tags")]
|
| 61 |
+
async fn list(
|
| 62 |
+
param: web::Json<TagListParams>,
|
| 63 |
+
data: web::Data<AppState>
|
| 64 |
+
) -> Result<HttpResponse, AppError> {
|
| 65 |
let tags = Query::find_tags_by_uid(param.uid, &data.conn).await?;
|
| 66 |
|
| 67 |
let mut result = HashMap::new();
|
|
|
|
| 73 |
data: result,
|
| 74 |
};
|
| 75 |
|
| 76 |
+
Ok(
|
| 77 |
+
HttpResponse::Ok()
|
| 78 |
+
.content_type("application/json")
|
| 79 |
+
.body(serde_json::to_string(&json_response)?)
|
| 80 |
+
)
|
| 81 |
+
}
|
src/api/user_info.rs
CHANGED
|
@@ -2,27 +2,24 @@ use std::collections::HashMap;
|
|
| 2 |
use std::io::SeekFrom;
|
| 3 |
use std::ptr::null;
|
| 4 |
use actix_identity::Identity;
|
| 5 |
-
use actix_web::{HttpResponse, post, web};
|
| 6 |
-
use chrono::{FixedOffset, Utc};
|
| 7 |
use sea_orm::ActiveValue::NotSet;
|
| 8 |
-
use serde::{Deserialize, Serialize};
|
| 9 |
use crate::api::JsonResponse;
|
| 10 |
use crate::AppState;
|
| 11 |
-
use crate::entity::{doc_info, tag_info};
|
| 12 |
use crate::entity::user_info::Model;
|
| 13 |
-
use crate::errors::{AppError, UserError};
|
| 14 |
use crate::service::user_info::Mutation;
|
| 15 |
use crate::service::user_info::Query;
|
| 16 |
|
| 17 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 18 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 19 |
}
|
| 20 |
|
| 21 |
pub(crate) fn create_auth_token(user: &Model) -> u64 {
|
| 22 |
-
use std::{
|
| 23 |
-
collections::hash_map::DefaultHasher,
|
| 24 |
-
hash::{Hash, Hasher},
|
| 25 |
-
};
|
| 26 |
|
| 27 |
let mut hasher = DefaultHasher::new();
|
| 28 |
user.hash(&mut hasher);
|
|
@@ -43,7 +40,7 @@ async fn login(
|
|
| 43 |
) -> Result<HttpResponse, AppError> {
|
| 44 |
match Query::login(&data.conn, &input.email, &input.password).await? {
|
| 45 |
Some(user) => {
|
| 46 |
-
let _ = Mutation::update_login_status(user.uid
|
| 47 |
let token = create_auth_token(&user).to_string();
|
| 48 |
|
| 49 |
identity.remember(token.clone());
|
|
@@ -54,17 +51,22 @@ async fn login(
|
|
| 54 |
data: token.clone(),
|
| 55 |
};
|
| 56 |
|
| 57 |
-
Ok(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
-
None => Err(UserError::LoginFailed.into())
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
#[post("/v1.0/register")]
|
| 67 |
-
async fn register(
|
|
|
|
|
|
|
|
|
|
| 68 |
let mut result = HashMap::new();
|
| 69 |
let u = Query::find_user_infos(&data.conn, &model.email).await?;
|
| 70 |
if let Some(_) = u {
|
|
@@ -73,25 +75,27 @@ async fn register(model: web::Json<Model>, data: web::Data<AppState>) -> Result<
|
|
| 73 |
err: "Email registered!".to_owned(),
|
| 74 |
data: (),
|
| 75 |
};
|
| 76 |
-
return Ok(
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
let usr = Mutation::create_user(&data.conn, &model).await?;
|
| 82 |
result.insert("uid", usr.uid.clone().unwrap());
|
| 83 |
-
crate::service::doc_info::Mutation::create_doc_info(&data.conn, doc_info::Model{
|
| 84 |
-
did:Default::default(),
|
| 85 |
-
uid:
|
| 86 |
doc_name: "/".into(),
|
| 87 |
size: 0,
|
| 88 |
location: "".into(),
|
| 89 |
r#type: "folder".to_string(),
|
| 90 |
created_at: now(),
|
| 91 |
updated_at: now(),
|
| 92 |
-
is_deleted:Default::default(),
|
| 93 |
}).await?;
|
| 94 |
-
let tnm = vec!["视频","图片","音乐","文档"];
|
| 95 |
let tregx = vec![
|
| 96 |
".*\\.(mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa)",
|
| 97 |
".*\\.(png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng)",
|
|
@@ -99,13 +103,13 @@ async fn register(model: web::Json<Model>, data: web::Data<AppState>) -> Result<
|
|
| 99 |
".*\\.(pdf|doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp)"
|
| 100 |
];
|
| 101 |
for i in 0..4 {
|
| 102 |
-
crate::service::tag_info::Mutation::create_tag(&data.conn, tag_info::Model{
|
| 103 |
tid: Default::default(),
|
| 104 |
uid: usr.uid.clone().unwrap(),
|
| 105 |
tag_name: tnm[i].to_owned(),
|
| 106 |
regx: tregx[i].to_owned(),
|
| 107 |
-
color: (i+1).to_owned() as i16,
|
| 108 |
-
icon: (i+1).to_owned() as i16,
|
| 109 |
folder_id: 0,
|
| 110 |
created_at: Default::default(),
|
| 111 |
updated_at: Default::default(),
|
|
@@ -117,13 +121,18 @@ async fn register(model: web::Json<Model>, data: web::Data<AppState>) -> Result<
|
|
| 117 |
data: result,
|
| 118 |
};
|
| 119 |
|
| 120 |
-
Ok(
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
#[post("/v1.0/setting")]
|
| 126 |
-
async fn setting(
|
|
|
|
|
|
|
|
|
|
| 127 |
let _ = Mutation::update_user_by_id(&data.conn, &model).await?;
|
| 128 |
let json_response = JsonResponse {
|
| 129 |
code: 200,
|
|
@@ -131,7 +140,9 @@ async fn setting(model: web::Json<Model>, data: web::Data<AppState>) -> Result<H
|
|
| 131 |
data: (),
|
| 132 |
};
|
| 133 |
|
| 134 |
-
Ok(
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
| 2 |
use std::io::SeekFrom;
|
| 3 |
use std::ptr::null;
|
| 4 |
use actix_identity::Identity;
|
| 5 |
+
use actix_web::{ HttpResponse, post, web };
|
| 6 |
+
use chrono::{ FixedOffset, Utc };
|
| 7 |
use sea_orm::ActiveValue::NotSet;
|
| 8 |
+
use serde::{ Deserialize, Serialize };
|
| 9 |
use crate::api::JsonResponse;
|
| 10 |
use crate::AppState;
|
| 11 |
+
use crate::entity::{ doc_info, tag_info };
|
| 12 |
use crate::entity::user_info::Model;
|
| 13 |
+
use crate::errors::{ AppError, UserError };
|
| 14 |
use crate::service::user_info::Mutation;
|
| 15 |
use crate::service::user_info::Query;
|
| 16 |
|
| 17 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 18 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 19 |
}
|
| 20 |
|
| 21 |
pub(crate) fn create_auth_token(user: &Model) -> u64 {
|
| 22 |
+
use std::{ collections::hash_map::DefaultHasher, hash::{ Hash, Hasher } };
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
let mut hasher = DefaultHasher::new();
|
| 25 |
user.hash(&mut hasher);
|
|
|
|
| 40 |
) -> Result<HttpResponse, AppError> {
|
| 41 |
match Query::login(&data.conn, &input.email, &input.password).await? {
|
| 42 |
Some(user) => {
|
| 43 |
+
let _ = Mutation::update_login_status(user.uid, &data.conn).await?;
|
| 44 |
let token = create_auth_token(&user).to_string();
|
| 45 |
|
| 46 |
identity.remember(token.clone());
|
|
|
|
| 51 |
data: token.clone(),
|
| 52 |
};
|
| 53 |
|
| 54 |
+
Ok(
|
| 55 |
+
HttpResponse::Ok()
|
| 56 |
+
.content_type("application/json")
|
| 57 |
+
.append_header(("X-Auth-Token", token))
|
| 58 |
+
.body(serde_json::to_string(&json_response)?)
|
| 59 |
+
)
|
| 60 |
}
|
| 61 |
+
None => Err(UserError::LoginFailed.into()),
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
#[post("/v1.0/register")]
|
| 66 |
+
async fn register(
|
| 67 |
+
model: web::Json<Model>,
|
| 68 |
+
data: web::Data<AppState>
|
| 69 |
+
) -> Result<HttpResponse, AppError> {
|
| 70 |
let mut result = HashMap::new();
|
| 71 |
let u = Query::find_user_infos(&data.conn, &model.email).await?;
|
| 72 |
if let Some(_) = u {
|
|
|
|
| 75 |
err: "Email registered!".to_owned(),
|
| 76 |
data: (),
|
| 77 |
};
|
| 78 |
+
return Ok(
|
| 79 |
+
HttpResponse::Ok()
|
| 80 |
+
.content_type("application/json")
|
| 81 |
+
.body(serde_json::to_string(&json_response)?)
|
| 82 |
+
);
|
| 83 |
}
|
| 84 |
|
| 85 |
let usr = Mutation::create_user(&data.conn, &model).await?;
|
| 86 |
result.insert("uid", usr.uid.clone().unwrap());
|
| 87 |
+
crate::service::doc_info::Mutation::create_doc_info(&data.conn, doc_info::Model {
|
| 88 |
+
did: Default::default(),
|
| 89 |
+
uid: usr.uid.clone().unwrap(),
|
| 90 |
doc_name: "/".into(),
|
| 91 |
size: 0,
|
| 92 |
location: "".into(),
|
| 93 |
r#type: "folder".to_string(),
|
| 94 |
created_at: now(),
|
| 95 |
updated_at: now(),
|
| 96 |
+
is_deleted: Default::default(),
|
| 97 |
}).await?;
|
| 98 |
+
let tnm = vec!["视频", "图片", "音乐", "文档"];
|
| 99 |
let tregx = vec![
|
| 100 |
".*\\.(mpg|mpeg|avi|rm|rmvb|mov|wmv|asf|dat|asx|wvx|mpe|mpa)",
|
| 101 |
".*\\.(png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|avif|apng)",
|
|
|
|
| 103 |
".*\\.(pdf|doc|ppt|yml|xml|htm|json|csv|txt|ini|xsl|wps|rtf|hlp)"
|
| 104 |
];
|
| 105 |
for i in 0..4 {
|
| 106 |
+
crate::service::tag_info::Mutation::create_tag(&data.conn, tag_info::Model {
|
| 107 |
tid: Default::default(),
|
| 108 |
uid: usr.uid.clone().unwrap(),
|
| 109 |
tag_name: tnm[i].to_owned(),
|
| 110 |
regx: tregx[i].to_owned(),
|
| 111 |
+
color: (i + 1).to_owned() as i16,
|
| 112 |
+
icon: (i + 1).to_owned() as i16,
|
| 113 |
folder_id: 0,
|
| 114 |
created_at: Default::default(),
|
| 115 |
updated_at: Default::default(),
|
|
|
|
| 121 |
data: result,
|
| 122 |
};
|
| 123 |
|
| 124 |
+
Ok(
|
| 125 |
+
HttpResponse::Ok()
|
| 126 |
+
.content_type("application/json")
|
| 127 |
+
.body(serde_json::to_string(&json_response)?)
|
| 128 |
+
)
|
| 129 |
}
|
| 130 |
|
| 131 |
#[post("/v1.0/setting")]
|
| 132 |
+
async fn setting(
|
| 133 |
+
model: web::Json<Model>,
|
| 134 |
+
data: web::Data<AppState>
|
| 135 |
+
) -> Result<HttpResponse, AppError> {
|
| 136 |
let _ = Mutation::update_user_by_id(&data.conn, &model).await?;
|
| 137 |
let json_response = JsonResponse {
|
| 138 |
code: 200,
|
|
|
|
| 140 |
data: (),
|
| 141 |
};
|
| 142 |
|
| 143 |
+
Ok(
|
| 144 |
+
HttpResponse::Ok()
|
| 145 |
+
.content_type("application/json")
|
| 146 |
+
.body(serde_json::to_string(&json_response)?)
|
| 147 |
+
)
|
| 148 |
+
}
|
src/entity/dialog2_kb.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "dialog2_kb")]
|
|
@@ -21,16 +21,18 @@ pub enum Relation {
|
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> RelationDef {
|
| 23 |
match self {
|
| 24 |
-
Self::DialogInfo =>
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "dialog2_kb")]
|
|
|
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> RelationDef {
|
| 23 |
match self {
|
| 24 |
+
Self::DialogInfo =>
|
| 25 |
+
Entity::belongs_to(super::dialog_info::Entity)
|
| 26 |
+
.from(Column::DialogId)
|
| 27 |
+
.to(super::dialog_info::Column::DialogId)
|
| 28 |
+
.into(),
|
| 29 |
+
Self::KbInfo =>
|
| 30 |
+
Entity::belongs_to(super::kb_info::Entity)
|
| 31 |
+
.from(Column::KbId)
|
| 32 |
+
.to(super::kb_info::Column::KbId)
|
| 33 |
+
.into(),
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/dialog_info.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
use chrono::{DateTime, FixedOffset};
|
| 2 |
use sea_orm::entity::prelude::*;
|
| 3 |
-
use serde::{Deserialize, Serialize};
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "dialog_info")]
|
|
@@ -19,7 +19,7 @@ pub struct Model {
|
|
| 19 |
#[serde(skip_deserializing)]
|
| 20 |
pub updated_at: DateTime<FixedOffset>,
|
| 21 |
#[serde(skip_deserializing)]
|
| 22 |
-
pub is_deleted: bool
|
| 23 |
}
|
| 24 |
|
| 25 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
@@ -35,4 +35,4 @@ impl Related<super::kb_info::Entity> for Entity {
|
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
+
use chrono::{ DateTime, FixedOffset };
|
| 2 |
use sea_orm::entity::prelude::*;
|
| 3 |
+
use serde::{ Deserialize, Serialize };
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "dialog_info")]
|
|
|
|
| 19 |
#[serde(skip_deserializing)]
|
| 20 |
pub updated_at: DateTime<FixedOffset>,
|
| 21 |
#[serde(skip_deserializing)]
|
| 22 |
+
pub is_deleted: bool,
|
| 23 |
}
|
| 24 |
|
| 25 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
|
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/doc2_doc.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "doc2_doc")]
|
|
@@ -15,22 +15,24 @@ pub struct Model {
|
|
| 15 |
#[derive(Debug, Clone, Copy, EnumIter)]
|
| 16 |
pub enum Relation {
|
| 17 |
Parent,
|
| 18 |
-
Child
|
| 19 |
}
|
| 20 |
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> RelationDef {
|
| 23 |
match self {
|
| 24 |
-
Self::Parent =>
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "doc2_doc")]
|
|
|
|
| 15 |
#[derive(Debug, Clone, Copy, EnumIter)]
|
| 16 |
pub enum Relation {
|
| 17 |
Parent,
|
| 18 |
+
Child,
|
| 19 |
}
|
| 20 |
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> RelationDef {
|
| 23 |
match self {
|
| 24 |
+
Self::Parent =>
|
| 25 |
+
Entity::belongs_to(super::doc_info::Entity)
|
| 26 |
+
.from(Column::ParentId)
|
| 27 |
+
.to(super::doc_info::Column::Did)
|
| 28 |
+
.into(),
|
| 29 |
+
Self::Child =>
|
| 30 |
+
Entity::belongs_to(super::doc_info::Entity)
|
| 31 |
+
.from(Column::Did)
|
| 32 |
+
.to(super::doc_info::Column::Did)
|
| 33 |
+
.into(),
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/doc_info.rs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
use crate::entity::kb_info;
|
| 4 |
-
use chrono::{DateTime, FixedOffset};
|
| 5 |
|
| 6 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 7 |
#[sea_orm(table_name = "doc_info")]
|
|
@@ -21,7 +21,7 @@ pub struct Model {
|
|
| 21 |
#[serde(skip_deserializing)]
|
| 22 |
pub updated_at: DateTime<FixedOffset>,
|
| 23 |
#[serde(skip_deserializing)]
|
| 24 |
-
pub is_deleted: bool
|
| 25 |
}
|
| 26 |
|
| 27 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
use crate::entity::kb_info;
|
| 4 |
+
use chrono::{ DateTime, FixedOffset };
|
| 5 |
|
| 6 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 7 |
#[sea_orm(table_name = "doc_info")]
|
|
|
|
| 21 |
#[serde(skip_deserializing)]
|
| 22 |
pub updated_at: DateTime<FixedOffset>,
|
| 23 |
#[serde(skip_deserializing)]
|
| 24 |
+
pub is_deleted: bool,
|
| 25 |
}
|
| 26 |
|
| 27 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
src/entity/kb2_doc.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
-
use chrono::{DateTime, FixedOffset};
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "kb2_doc")]
|
|
@@ -30,16 +30,18 @@ pub enum Relation {
|
|
| 30 |
impl RelationTrait for Relation {
|
| 31 |
fn def(&self) -> RelationDef {
|
| 32 |
match self {
|
| 33 |
-
Self::DocInfo =>
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
+
use chrono::{ DateTime, FixedOffset };
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "kb2_doc")]
|
|
|
|
| 30 |
impl RelationTrait for Relation {
|
| 31 |
fn def(&self) -> RelationDef {
|
| 32 |
match self {
|
| 33 |
+
Self::DocInfo =>
|
| 34 |
+
Entity::belongs_to(super::doc_info::Entity)
|
| 35 |
+
.from(Column::Did)
|
| 36 |
+
.to(super::doc_info::Column::Did)
|
| 37 |
+
.into(),
|
| 38 |
+
Self::KbInfo =>
|
| 39 |
+
Entity::belongs_to(super::kb_info::Entity)
|
| 40 |
+
.from(Column::KbId)
|
| 41 |
+
.to(super::kb_info::Column::KbId)
|
| 42 |
+
.into(),
|
| 43 |
}
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/kb_info.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
-
use chrono::{DateTime, FixedOffset};
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "kb_info")]
|
|
@@ -44,4 +44,4 @@ impl Related<super::dialog_info::Entity> for Entity {
|
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
+
use chrono::{ DateTime, FixedOffset };
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "kb_info")]
|
|
|
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/mod.rs
CHANGED
|
@@ -6,4 +6,4 @@ pub(crate) 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;
|
|
|
|
| 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/entity/tag2_doc.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "tag2_doc")]
|
|
@@ -21,16 +21,18 @@ pub enum Relation {
|
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> sea_orm::RelationDef {
|
| 23 |
match self {
|
| 24 |
-
Self::DocInfo =>
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
|
| 4 |
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
|
| 5 |
#[sea_orm(table_name = "tag2_doc")]
|
|
|
|
| 21 |
impl RelationTrait for Relation {
|
| 22 |
fn def(&self) -> sea_orm::RelationDef {
|
| 23 |
match self {
|
| 24 |
+
Self::DocInfo =>
|
| 25 |
+
Entity::belongs_to(super::doc_info::Entity)
|
| 26 |
+
.from(Column::Uid)
|
| 27 |
+
.to(super::doc_info::Column::Uid)
|
| 28 |
+
.into(),
|
| 29 |
+
Self::Tag =>
|
| 30 |
+
Entity::belongs_to(super::tag_info::Entity)
|
| 31 |
+
.from(Column::TagId)
|
| 32 |
+
.to(super::tag_info::Column::Tid)
|
| 33 |
+
.into(),
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/tag_info.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
-
use chrono::{DateTime, FixedOffset};
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "tag_info")]
|
|
@@ -37,4 +37,4 @@ impl Related<super::doc_info::Entity> for Entity {
|
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
+
use chrono::{ DateTime, FixedOffset };
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "tag_info")]
|
|
|
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
+
impl ActiveModelBehavior for ActiveModel {}
|
src/entity/user_info.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
-
use serde::{Deserialize, Serialize};
|
| 3 |
-
use chrono::{DateTime, FixedOffset};
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, Hash, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "user_info")]
|
|
@@ -27,5 +27,4 @@ pub struct Model {
|
|
| 27 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
| 28 |
pub enum Relation {}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
| 1 |
use sea_orm::entity::prelude::*;
|
| 2 |
+
use serde::{ Deserialize, Serialize };
|
| 3 |
+
use chrono::{ DateTime, FixedOffset };
|
| 4 |
|
| 5 |
#[derive(Clone, Debug, PartialEq, Eq, Hash, DeriveEntityModel, Deserialize, Serialize)]
|
| 6 |
#[sea_orm(table_name = "user_info")]
|
|
|
|
| 27 |
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
| 28 |
pub enum Relation {}
|
| 29 |
|
| 30 |
+
impl ActiveModelBehavior for ActiveModel {}
|
|
|
src/errors.rs
CHANGED
|
@@ -1,22 +1,17 @@
|
|
| 1 |
-
use actix_web::{HttpResponse, ResponseError};
|
| 2 |
use thiserror::Error;
|
| 3 |
|
| 4 |
#[derive(Debug, Error)]
|
| 5 |
pub(crate) enum AppError {
|
| 6 |
-
#[error("`{0}`")]
|
| 7 |
-
User(#[from] UserError),
|
| 8 |
|
| 9 |
-
#[error("`{0}`")]
|
| 10 |
-
Json(#[from] serde_json::Error),
|
| 11 |
|
| 12 |
-
#[error("`{0}`")]
|
| 13 |
-
Actix(#[from] actix_web::Error),
|
| 14 |
|
| 15 |
-
#[error("`{0}`")]
|
| 16 |
-
Db(#[from] sea_orm::DbErr),
|
| 17 |
|
| 18 |
-
#[error("`{0}`")]
|
| 19 |
-
Std(#[from] std::io::Error),
|
| 20 |
}
|
| 21 |
|
| 22 |
#[derive(Debug, Error)]
|
|
@@ -33,8 +28,7 @@ pub(crate) enum UserError {
|
|
| 33 |
#[error("`password` field of `User` cannot contain whitespaces!")]
|
| 34 |
PasswordInvalidCharacter,
|
| 35 |
|
| 36 |
-
#[error("Could not find any `User` for id: `{0}`!")]
|
| 37 |
-
NotFound(i64),
|
| 38 |
|
| 39 |
#[error("Failed to login user!")]
|
| 40 |
LoginFailed,
|
|
@@ -52,21 +46,22 @@ pub(crate) enum UserError {
|
|
| 52 |
impl ResponseError for AppError {
|
| 53 |
fn status_code(&self) -> actix_web::http::StatusCode {
|
| 54 |
match self {
|
| 55 |
-
AppError::User(user_error) =>
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
-
UserError::EmptyPassword => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
|
| 61 |
-
UserError::PasswordInvalidCharacter => {
|
| 62 |
-
actix_web::http::StatusCode::UNPROCESSABLE_ENTITY
|
| 63 |
-
}
|
| 64 |
-
UserError::NotFound(_) => actix_web::http::StatusCode::NOT_FOUND,
|
| 65 |
-
UserError::NotLoggedIn => actix_web::http::StatusCode::UNAUTHORIZED,
|
| 66 |
-
UserError::Empty => actix_web::http::StatusCode::NOT_FOUND,
|
| 67 |
-
UserError::LoginFailed => actix_web::http::StatusCode::NOT_FOUND,
|
| 68 |
-
UserError::InvalidToken => actix_web::http::StatusCode::UNAUTHORIZED,
|
| 69 |
-
},
|
| 70 |
AppError::Json(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
|
| 71 |
AppError::Actix(fail) => fail.as_response_error().status_code(),
|
| 72 |
AppError::Db(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
@@ -79,4 +74,4 @@ impl ResponseError for AppError {
|
|
| 79 |
let response = HttpResponse::build(status_code).body(self.to_string());
|
| 80 |
response
|
| 81 |
}
|
| 82 |
-
}
|
|
|
|
| 1 |
+
use actix_web::{ HttpResponse, ResponseError };
|
| 2 |
use thiserror::Error;
|
| 3 |
|
| 4 |
#[derive(Debug, Error)]
|
| 5 |
pub(crate) enum AppError {
|
| 6 |
+
#[error("`{0}`")] User(#[from] UserError),
|
|
|
|
| 7 |
|
| 8 |
+
#[error("`{0}`")] Json(#[from] serde_json::Error),
|
|
|
|
| 9 |
|
| 10 |
+
#[error("`{0}`")] Actix(#[from] actix_web::Error),
|
|
|
|
| 11 |
|
| 12 |
+
#[error("`{0}`")] Db(#[from] sea_orm::DbErr),
|
|
|
|
| 13 |
|
| 14 |
+
#[error("`{0}`")] Std(#[from] std::io::Error),
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
#[derive(Debug, Error)]
|
|
|
|
| 28 |
#[error("`password` field of `User` cannot contain whitespaces!")]
|
| 29 |
PasswordInvalidCharacter,
|
| 30 |
|
| 31 |
+
#[error("Could not find any `User` for id: `{0}`!")] NotFound(i64),
|
|
|
|
| 32 |
|
| 33 |
#[error("Failed to login user!")]
|
| 34 |
LoginFailed,
|
|
|
|
| 46 |
impl ResponseError for AppError {
|
| 47 |
fn status_code(&self) -> actix_web::http::StatusCode {
|
| 48 |
match self {
|
| 49 |
+
AppError::User(user_error) =>
|
| 50 |
+
match user_error {
|
| 51 |
+
UserError::EmptyUsername => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
|
| 52 |
+
UserError::UsernameInvalidCharacter => {
|
| 53 |
+
actix_web::http::StatusCode::UNPROCESSABLE_ENTITY
|
| 54 |
+
}
|
| 55 |
+
UserError::EmptyPassword => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
|
| 56 |
+
UserError::PasswordInvalidCharacter => {
|
| 57 |
+
actix_web::http::StatusCode::UNPROCESSABLE_ENTITY
|
| 58 |
+
}
|
| 59 |
+
UserError::NotFound(_) => actix_web::http::StatusCode::NOT_FOUND,
|
| 60 |
+
UserError::NotLoggedIn => actix_web::http::StatusCode::UNAUTHORIZED,
|
| 61 |
+
UserError::Empty => actix_web::http::StatusCode::NOT_FOUND,
|
| 62 |
+
UserError::LoginFailed => actix_web::http::StatusCode::NOT_FOUND,
|
| 63 |
+
UserError::InvalidToken => actix_web::http::StatusCode::UNAUTHORIZED,
|
| 64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
AppError::Json(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
|
| 66 |
AppError::Actix(fail) => fail.as_response_error().status_code(),
|
| 67 |
AppError::Db(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
| 74 |
let response = HttpResponse::build(status_code).body(self.to_string());
|
| 75 |
response
|
| 76 |
}
|
| 77 |
+
}
|
src/main.rs
CHANGED
|
@@ -5,16 +5,16 @@ mod errors;
|
|
| 5 |
|
| 6 |
use std::env;
|
| 7 |
use actix_files::Files;
|
| 8 |
-
use actix_identity::{CookieIdentityPolicy, IdentityService, RequestIdentity};
|
| 9 |
use actix_session::CookieSession;
|
| 10 |
-
use actix_web::{web, App, HttpServer, middleware, Error};
|
| 11 |
use actix_web::cookie::time::Duration;
|
| 12 |
use actix_web::dev::ServiceRequest;
|
| 13 |
use actix_web::error::ErrorUnauthorized;
|
| 14 |
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
| 15 |
use listenfd::ListenFd;
|
| 16 |
-
use sea_orm::{Database, DatabaseConnection};
|
| 17 |
-
use migration::{Migrator, MigratorTrait};
|
| 18 |
use crate::errors::UserError;
|
| 19 |
|
| 20 |
#[derive(Debug, Clone)]
|
|
@@ -24,10 +24,10 @@ struct AppState {
|
|
| 24 |
|
| 25 |
pub(crate) async fn validator(
|
| 26 |
req: ServiceRequest,
|
| 27 |
-
credentials: BearerAuth
|
| 28 |
) -> Result<ServiceRequest, Error> {
|
| 29 |
if let Some(token) = req.get_identity() {
|
| 30 |
-
println!("{}, {}",credentials.token(), token);
|
| 31 |
(credentials.token() == token)
|
| 32 |
.then(|| req)
|
| 33 |
.ok_or(ErrorUnauthorized(UserError::InvalidToken))
|
|
@@ -61,18 +61,20 @@ async fn main() -> std::io::Result<()> {
|
|
| 61 |
App::new()
|
| 62 |
.service(Files::new("/static", "./static"))
|
| 63 |
.app_data(web::Data::new(state.clone()))
|
| 64 |
-
.wrap(
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
.wrap(
|
| 71 |
CookieSession::signed(&[0; 32])
|
| 72 |
.name("session-cookie")
|
| 73 |
.secure(false)
|
| 74 |
// WARNING(alex): This uses the `time` crate, not `std::time`!
|
| 75 |
-
.expires_in_time(Duration::seconds(60))
|
| 76 |
)
|
| 77 |
.wrap(middleware::Logger::default())
|
| 78 |
.configure(init)
|
|
@@ -116,4 +118,4 @@ fn init(cfg: &mut web::ServiceConfig) {
|
|
| 116 |
cfg.service(api::user_info::login);
|
| 117 |
cfg.service(api::user_info::register);
|
| 118 |
cfg.service(api::user_info::setting);
|
| 119 |
-
}
|
|
|
|
| 5 |
|
| 6 |
use std::env;
|
| 7 |
use actix_files::Files;
|
| 8 |
+
use actix_identity::{ CookieIdentityPolicy, IdentityService, RequestIdentity };
|
| 9 |
use actix_session::CookieSession;
|
| 10 |
+
use actix_web::{ web, App, HttpServer, middleware, Error };
|
| 11 |
use actix_web::cookie::time::Duration;
|
| 12 |
use actix_web::dev::ServiceRequest;
|
| 13 |
use actix_web::error::ErrorUnauthorized;
|
| 14 |
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
| 15 |
use listenfd::ListenFd;
|
| 16 |
+
use sea_orm::{ Database, DatabaseConnection };
|
| 17 |
+
use migration::{ Migrator, MigratorTrait };
|
| 18 |
use crate::errors::UserError;
|
| 19 |
|
| 20 |
#[derive(Debug, Clone)]
|
|
|
|
| 24 |
|
| 25 |
pub(crate) async fn validator(
|
| 26 |
req: ServiceRequest,
|
| 27 |
+
credentials: BearerAuth
|
| 28 |
) -> Result<ServiceRequest, Error> {
|
| 29 |
if let Some(token) = req.get_identity() {
|
| 30 |
+
println!("{}, {}", credentials.token(), token);
|
| 31 |
(credentials.token() == token)
|
| 32 |
.then(|| req)
|
| 33 |
.ok_or(ErrorUnauthorized(UserError::InvalidToken))
|
|
|
|
| 61 |
App::new()
|
| 62 |
.service(Files::new("/static", "./static"))
|
| 63 |
.app_data(web::Data::new(state.clone()))
|
| 64 |
+
.wrap(
|
| 65 |
+
IdentityService::new(
|
| 66 |
+
CookieIdentityPolicy::new(&[0; 32])
|
| 67 |
+
.name("auth-cookie")
|
| 68 |
+
.login_deadline(Duration::seconds(120))
|
| 69 |
+
.secure(false)
|
| 70 |
+
)
|
| 71 |
+
)
|
| 72 |
.wrap(
|
| 73 |
CookieSession::signed(&[0; 32])
|
| 74 |
.name("session-cookie")
|
| 75 |
.secure(false)
|
| 76 |
// WARNING(alex): This uses the `time` crate, not `std::time`!
|
| 77 |
+
.expires_in_time(Duration::seconds(60))
|
| 78 |
)
|
| 79 |
.wrap(middleware::Logger::default())
|
| 80 |
.configure(init)
|
|
|
|
| 118 |
cfg.service(api::user_info::login);
|
| 119 |
cfg.service(api::user_info::register);
|
| 120 |
cfg.service(api::user_info::setting);
|
| 121 |
+
}
|
src/service/dialog_info.rs
CHANGED
|
@@ -1,19 +1,31 @@
|
|
| 1 |
-
use chrono::{Local, FixedOffset, Utc};
|
| 2 |
use migration::Expr;
|
| 3 |
-
use sea_orm::{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
use sea_orm::ActiveValue::Set;
|
| 5 |
use sea_orm::QueryFilter;
|
| 6 |
use sea_orm::ColumnTrait;
|
| 7 |
use crate::entity::dialog_info;
|
| 8 |
use crate::entity::dialog_info::Entity;
|
| 9 |
|
| 10 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 11 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 12 |
}
|
| 13 |
pub struct Query;
|
| 14 |
|
| 15 |
impl Query {
|
| 16 |
-
pub async fn find_dialog_info_by_id(
|
|
|
|
|
|
|
|
|
|
| 17 |
Entity::find_by_id(id).one(db).await
|
| 18 |
}
|
| 19 |
|
|
@@ -21,18 +33,20 @@ impl Query {
|
|
| 21 |
Entity::find().all(db).await
|
| 22 |
}
|
| 23 |
|
| 24 |
-
pub async fn find_dialog_infos_by_uid(
|
|
|
|
|
|
|
|
|
|
| 25 |
Entity::find()
|
| 26 |
.filter(dialog_info::Column::Uid.eq(uid))
|
| 27 |
.filter(dialog_info::Column::IsDeleted.eq(false))
|
| 28 |
-
.all(db)
|
| 29 |
-
.await
|
| 30 |
}
|
| 31 |
|
| 32 |
pub async fn find_dialog_infos_in_page(
|
| 33 |
db: &DbConn,
|
| 34 |
page: u64,
|
| 35 |
-
posts_per_page: u64
|
| 36 |
) -> Result<(Vec<dialog_info::Model>, u64), DbErr> {
|
| 37 |
// Setup paginator
|
| 38 |
let paginator = Entity::find()
|
|
@@ -54,7 +68,7 @@ impl Mutation {
|
|
| 54 |
kb_id: i64,
|
| 55 |
name: &String
|
| 56 |
) -> Result<dialog_info::ActiveModel, DbErr> {
|
| 57 |
-
dialog_info::ActiveModel {
|
| 58 |
dialog_id: Default::default(),
|
| 59 |
uid: Set(uid),
|
| 60 |
kb_id: Set(kb_id),
|
|
@@ -62,16 +76,14 @@ impl Mutation {
|
|
| 62 |
history: Set("".to_owned()),
|
| 63 |
created_at: Set(now()),
|
| 64 |
updated_at: Set(now()),
|
| 65 |
-
is_deleted: Default::default()
|
| 66 |
-
}
|
| 67 |
-
.save(db)
|
| 68 |
-
.await
|
| 69 |
}
|
| 70 |
|
| 71 |
pub async fn update_dialog_info_by_id(
|
| 72 |
db: &DbConn,
|
| 73 |
dialog_id: i64,
|
| 74 |
-
dialog_name
|
| 75 |
history: &String
|
| 76 |
) -> Result<UpdateResult, DbErr> {
|
| 77 |
Entity::update_many()
|
|
@@ -79,19 +91,17 @@ impl Mutation {
|
|
| 79 |
.col_expr(dialog_info::Column::History, Expr::value(history))
|
| 80 |
.col_expr(dialog_info::Column::UpdatedAt, Expr::value(now()))
|
| 81 |
.filter(dialog_info::Column::DialogId.eq(dialog_id))
|
| 82 |
-
.exec(db)
|
| 83 |
-
.await
|
| 84 |
}
|
| 85 |
|
| 86 |
pub async fn delete_dialog_info(db: &DbConn, dialog_id: i64) -> Result<UpdateResult, DbErr> {
|
| 87 |
Entity::update_many()
|
| 88 |
.col_expr(dialog_info::Column::IsDeleted, Expr::value(true))
|
| 89 |
.filter(dialog_info::Column::DialogId.eq(dialog_id))
|
| 90 |
-
.exec(db)
|
| 91 |
-
.await
|
| 92 |
}
|
| 93 |
|
| 94 |
pub async fn delete_all_dialog_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 95 |
Entity::delete_many().exec(db).await
|
| 96 |
}
|
| 97 |
-
}
|
|
|
|
| 1 |
+
use chrono::{ Local, FixedOffset, Utc };
|
| 2 |
use migration::Expr;
|
| 3 |
+
use sea_orm::{
|
| 4 |
+
ActiveModelTrait,
|
| 5 |
+
DbConn,
|
| 6 |
+
DbErr,
|
| 7 |
+
DeleteResult,
|
| 8 |
+
EntityTrait,
|
| 9 |
+
PaginatorTrait,
|
| 10 |
+
QueryOrder,
|
| 11 |
+
UpdateResult,
|
| 12 |
+
};
|
| 13 |
use sea_orm::ActiveValue::Set;
|
| 14 |
use sea_orm::QueryFilter;
|
| 15 |
use sea_orm::ColumnTrait;
|
| 16 |
use crate::entity::dialog_info;
|
| 17 |
use crate::entity::dialog_info::Entity;
|
| 18 |
|
| 19 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 20 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 21 |
}
|
| 22 |
pub struct Query;
|
| 23 |
|
| 24 |
impl Query {
|
| 25 |
+
pub async fn find_dialog_info_by_id(
|
| 26 |
+
db: &DbConn,
|
| 27 |
+
id: i64
|
| 28 |
+
) -> Result<Option<dialog_info::Model>, DbErr> {
|
| 29 |
Entity::find_by_id(id).one(db).await
|
| 30 |
}
|
| 31 |
|
|
|
|
| 33 |
Entity::find().all(db).await
|
| 34 |
}
|
| 35 |
|
| 36 |
+
pub async fn find_dialog_infos_by_uid(
|
| 37 |
+
db: &DbConn,
|
| 38 |
+
uid: i64
|
| 39 |
+
) -> Result<Vec<dialog_info::Model>, DbErr> {
|
| 40 |
Entity::find()
|
| 41 |
.filter(dialog_info::Column::Uid.eq(uid))
|
| 42 |
.filter(dialog_info::Column::IsDeleted.eq(false))
|
| 43 |
+
.all(db).await
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
pub async fn find_dialog_infos_in_page(
|
| 47 |
db: &DbConn,
|
| 48 |
page: u64,
|
| 49 |
+
posts_per_page: u64
|
| 50 |
) -> Result<(Vec<dialog_info::Model>, u64), DbErr> {
|
| 51 |
// Setup paginator
|
| 52 |
let paginator = Entity::find()
|
|
|
|
| 68 |
kb_id: i64,
|
| 69 |
name: &String
|
| 70 |
) -> Result<dialog_info::ActiveModel, DbErr> {
|
| 71 |
+
(dialog_info::ActiveModel {
|
| 72 |
dialog_id: Default::default(),
|
| 73 |
uid: Set(uid),
|
| 74 |
kb_id: Set(kb_id),
|
|
|
|
| 76 |
history: Set("".to_owned()),
|
| 77 |
created_at: Set(now()),
|
| 78 |
updated_at: Set(now()),
|
| 79 |
+
is_deleted: Default::default(),
|
| 80 |
+
}).save(db).await
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
|
| 83 |
pub async fn update_dialog_info_by_id(
|
| 84 |
db: &DbConn,
|
| 85 |
dialog_id: i64,
|
| 86 |
+
dialog_name: &String,
|
| 87 |
history: &String
|
| 88 |
) -> Result<UpdateResult, DbErr> {
|
| 89 |
Entity::update_many()
|
|
|
|
| 91 |
.col_expr(dialog_info::Column::History, Expr::value(history))
|
| 92 |
.col_expr(dialog_info::Column::UpdatedAt, Expr::value(now()))
|
| 93 |
.filter(dialog_info::Column::DialogId.eq(dialog_id))
|
| 94 |
+
.exec(db).await
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
pub async fn delete_dialog_info(db: &DbConn, dialog_id: i64) -> Result<UpdateResult, DbErr> {
|
| 98 |
Entity::update_many()
|
| 99 |
.col_expr(dialog_info::Column::IsDeleted, Expr::value(true))
|
| 100 |
.filter(dialog_info::Column::DialogId.eq(dialog_id))
|
| 101 |
+
.exec(db).await
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
pub async fn delete_all_dialog_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 105 |
Entity::delete_many().exec(db).await
|
| 106 |
}
|
| 107 |
+
}
|
src/service/doc_info.rs
CHANGED
|
@@ -1,20 +1,41 @@
|
|
| 1 |
-
use chrono::{Utc, FixedOffset};
|
| 2 |
-
use sea_orm::{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
use sea_orm::ActiveValue::Set;
|
| 4 |
use sea_orm::QueryFilter;
|
| 5 |
use crate::api::doc_info::ListParams;
|
| 6 |
-
use crate::entity::{doc2_doc, doc_info};
|
| 7 |
use crate::entity::doc_info::Entity;
|
| 8 |
use crate::service;
|
| 9 |
|
| 10 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 11 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 12 |
}
|
| 13 |
|
| 14 |
pub struct Query;
|
| 15 |
|
| 16 |
impl Query {
|
| 17 |
-
pub async fn find_doc_info_by_id(
|
|
|
|
|
|
|
|
|
|
| 18 |
Entity::find_by_id(id).one(db).await
|
| 19 |
}
|
| 20 |
|
|
@@ -22,57 +43,71 @@ impl Query {
|
|
| 22 |
Entity::find().all(db).await
|
| 23 |
}
|
| 24 |
|
| 25 |
-
pub async fn find_doc_infos_by_uid(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
}
|
| 31 |
|
| 32 |
-
pub async fn find_doc_infos_by_name(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
let mut dids = Vec::<i64>::new();
|
| 34 |
if let Some(pid) = parent_id {
|
| 35 |
-
for d2d in doc2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 36 |
dids.push(d2d.did);
|
| 37 |
}
|
| 38 |
-
}
|
| 39 |
-
else{
|
| 40 |
let doc = Entity::find()
|
| 41 |
.filter(doc_info::Column::DocName.eq(name.clone()))
|
| 42 |
.filter(doc_info::Column::Uid.eq(uid))
|
| 43 |
-
.all(db)
|
| 44 |
-
|
| 45 |
-
if doc.len() == 0{
|
| 46 |
return Ok(vec![]);
|
| 47 |
}
|
| 48 |
-
assert!(doc.len()>0);
|
| 49 |
-
let d2d = doc2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 50 |
assert!(d2d.len() <= 1, "Did: {}->{}", doc[0].did, d2d.len());
|
| 51 |
-
if d2d.len()>0{
|
| 52 |
-
for d2d_ in doc2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 53 |
dids.push(d2d_.did);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
}
|
| 57 |
-
|
| 58 |
Entity::find()
|
| 59 |
.filter(doc_info::Column::DocName.eq(name.clone()))
|
| 60 |
.filter(doc_info::Column::Uid.eq(uid))
|
| 61 |
.filter(doc_info::Column::Did.is_in(dids))
|
| 62 |
.filter(doc_info::Column::IsDeleted.eq(false))
|
| 63 |
-
.all(db)
|
| 64 |
-
.await
|
| 65 |
}
|
| 66 |
|
| 67 |
pub async fn all_descendent_ids(db: &DbConn, doc_ids: &Vec<i64>) -> Result<Vec<i64>, DbErr> {
|
| 68 |
let mut dids = doc_ids.clone();
|
| 69 |
-
let mut i:usize = 0;
|
| 70 |
loop {
|
| 71 |
if dids.len() == i {
|
| 72 |
break;
|
| 73 |
}
|
| 74 |
-
|
| 75 |
-
for d in doc2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 76 |
dids.push(d.did);
|
| 77 |
}
|
| 78 |
i += 1;
|
|
@@ -80,9 +115,13 @@ impl Query {
|
|
| 80 |
Ok(dids)
|
| 81 |
}
|
| 82 |
|
| 83 |
-
pub async fn find_doc_infos_by_params(
|
|
|
|
|
|
|
|
|
|
| 84 |
// Setup paginator
|
| 85 |
-
let mut sql:String
|
|
|
|
| 86 |
select
|
| 87 |
a.did,
|
| 88 |
a.uid,
|
|
@@ -97,21 +136,33 @@ impl Query {
|
|
| 97 |
doc_info as a
|
| 98 |
".to_owned();
|
| 99 |
|
| 100 |
-
let mut cond:String = format!(" a.uid={} and a.is_deleted=False ", params.uid);
|
| 101 |
|
| 102 |
if let Some(kb_id) = params.filter.kb_id {
|
| 103 |
-
sql.push_str(
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
if let Some(folder_id) = params.filter.folder_id {
|
| 106 |
-
sql.push_str(
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
// Fetch paginated posts
|
| 109 |
if let Some(tag_id) = params.filter.tag_id {
|
| 110 |
-
let tag = service::tag_info::Query
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
-
if tag.regx.len()>0{
|
| 115 |
cond.push_str(&format!(" and doc_name ~ '{}'", tag.regx));
|
| 116 |
}
|
| 117 |
}
|
|
@@ -119,7 +170,7 @@ impl Query {
|
|
| 119 |
if let Some(keywords) = params.filter.keywords {
|
| 120 |
cond.push_str(&format!(" and doc_name like '%{}%'", keywords));
|
| 121 |
}
|
| 122 |
-
if cond.len() > 0{
|
| 123 |
sql.push_str(&" where ");
|
| 124 |
sql.push_str(&cond);
|
| 125 |
}
|
|
@@ -128,28 +179,26 @@ impl Query {
|
|
| 128 |
orderby = "updated_at desc".to_owned();
|
| 129 |
}
|
| 130 |
sql.push_str(&format!(" order by {}", orderby));
|
| 131 |
-
let mut page_size:u32 = 30;
|
| 132 |
if let Some(pg_sz) = params.per_page {
|
| 133 |
page_size = pg_sz;
|
| 134 |
}
|
| 135 |
-
let mut page:u32 = 0;
|
| 136 |
if let Some(pg) = params.page {
|
| 137 |
page = pg;
|
| 138 |
}
|
| 139 |
-
sql.push_str(&format!(" limit {} offset {} ;", page_size, page*page_size));
|
| 140 |
-
|
| 141 |
print!("{}", sql);
|
| 142 |
Entity::find()
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
).all(db).await
|
| 146 |
-
|
| 147 |
}
|
| 148 |
|
| 149 |
pub async fn find_doc_infos_in_page(
|
| 150 |
db: &DbConn,
|
| 151 |
page: u64,
|
| 152 |
-
posts_per_page: u64
|
| 153 |
) -> Result<(Vec<doc_info::Model>, u64), DbErr> {
|
| 154 |
// Setup paginator
|
| 155 |
let paginator = Entity::find()
|
|
@@ -165,22 +214,18 @@ impl Query {
|
|
| 165 |
pub struct Mutation;
|
| 166 |
|
| 167 |
impl Mutation {
|
| 168 |
-
|
| 169 |
-
pub async fn mv_doc_info(
|
| 170 |
-
db: &DbConn,
|
| 171 |
-
dest_did: i64,
|
| 172 |
-
dids: &[i64]
|
| 173 |
-
) -> Result<(), DbErr> {
|
| 174 |
for did in dids {
|
| 175 |
-
let d = doc2_doc::Entity
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
id: Set(d[0].id),
|
| 179 |
did: Set(did.to_owned()),
|
| 180 |
-
parent_id: Set(dest_did)
|
| 181 |
-
}
|
| 182 |
-
.update(db)
|
| 183 |
-
.await?;
|
| 184 |
}
|
| 185 |
|
| 186 |
Ok(())
|
|
@@ -190,21 +235,19 @@ impl Mutation {
|
|
| 190 |
db: &DbConn,
|
| 191 |
dest_did: i64,
|
| 192 |
did: i64
|
| 193 |
-
) -> Result<doc2_doc::ActiveModel, DbErr>
|
| 194 |
-
doc2_doc::ActiveModel {
|
| 195 |
id: Default::default(),
|
| 196 |
parent_id: Set(dest_did),
|
| 197 |
did: Set(did),
|
| 198 |
-
}
|
| 199 |
-
.save(db)
|
| 200 |
-
.await
|
| 201 |
}
|
| 202 |
|
| 203 |
pub async fn create_doc_info(
|
| 204 |
db: &DbConn,
|
| 205 |
-
form_data: doc_info::Model
|
| 206 |
) -> Result<doc_info::ActiveModel, DbErr> {
|
| 207 |
-
doc_info::ActiveModel {
|
| 208 |
did: Default::default(),
|
| 209 |
uid: Set(form_data.uid.to_owned()),
|
| 210 |
doc_name: Set(form_data.doc_name.to_owned()),
|
|
@@ -213,24 +256,21 @@ impl Mutation {
|
|
| 213 |
location: Set(form_data.location.to_owned()),
|
| 214 |
created_at: Set(form_data.created_at.to_owned()),
|
| 215 |
updated_at: Set(form_data.updated_at.to_owned()),
|
| 216 |
-
is_deleted:Default::default()
|
| 217 |
-
}
|
| 218 |
-
.save(db)
|
| 219 |
-
.await
|
| 220 |
}
|
| 221 |
|
| 222 |
pub async fn update_doc_info_by_id(
|
| 223 |
db: &DbConn,
|
| 224 |
id: i64,
|
| 225 |
-
form_data: doc_info::Model
|
| 226 |
) -> Result<doc_info::Model, DbErr> {
|
| 227 |
let doc_info: doc_info::ActiveModel = Entity::find_by_id(id)
|
| 228 |
-
.one(db)
|
| 229 |
-
.await?
|
| 230 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 231 |
.map(Into::into)?;
|
| 232 |
|
| 233 |
-
doc_info::ActiveModel {
|
| 234 |
did: doc_info.did,
|
| 235 |
uid: Set(form_data.uid.to_owned()),
|
| 236 |
doc_name: Set(form_data.doc_name.to_owned()),
|
|
@@ -240,46 +280,51 @@ impl Mutation {
|
|
| 240 |
created_at: doc_info.created_at,
|
| 241 |
updated_at: Set(now()),
|
| 242 |
is_deleted: Default::default(),
|
| 243 |
-
}
|
| 244 |
-
.update(db)
|
| 245 |
-
.await
|
| 246 |
}
|
| 247 |
|
| 248 |
pub async fn delete_doc_info(db: &DbConn, doc_ids: &Vec<i64>) -> Result<UpdateResult, DbErr> {
|
| 249 |
let mut dids = doc_ids.clone();
|
| 250 |
-
let mut i:usize = 0;
|
| 251 |
loop {
|
| 252 |
if dids.len() == i {
|
| 253 |
break;
|
| 254 |
}
|
| 255 |
let mut doc: doc_info::ActiveModel = Entity::find_by_id(dids[i])
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
.
|
| 260 |
-
doc.updated_at = Set(now());
|
| 261 |
doc.is_deleted = Set(true);
|
| 262 |
let _ = doc.update(db).await?;
|
| 263 |
-
|
| 264 |
-
for d in doc2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 265 |
dids.push(d.did);
|
| 266 |
}
|
| 267 |
-
let _ = doc2_doc::Entity
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
i += 1;
|
| 270 |
}
|
| 271 |
-
crate::service::kb_info::Mutation::remove_docs(&db, dids,None).await
|
| 272 |
}
|
| 273 |
|
| 274 |
pub async fn rename(db: &DbConn, doc_id: i64, name: &String) -> Result<doc_info::Model, DbErr> {
|
| 275 |
let mut doc: doc_info::ActiveModel = Entity::find_by_id(doc_id)
|
| 276 |
-
.one(db)
|
| 277 |
-
.await?
|
| 278 |
.ok_or(DbErr::Custom(format!("Can't find doc:{}", doc_id)))
|
| 279 |
.map(Into::into)?;
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
}
|
| 284 |
|
| 285 |
pub async fn delete_all_doc_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
|
|
|
| 1 |
+
use chrono::{ Utc, FixedOffset };
|
| 2 |
+
use sea_orm::{
|
| 3 |
+
ActiveModelTrait,
|
| 4 |
+
ColumnTrait,
|
| 5 |
+
DbConn,
|
| 6 |
+
DbErr,
|
| 7 |
+
DeleteResult,
|
| 8 |
+
EntityTrait,
|
| 9 |
+
PaginatorTrait,
|
| 10 |
+
QueryOrder,
|
| 11 |
+
Unset,
|
| 12 |
+
Unchanged,
|
| 13 |
+
ConditionalStatement,
|
| 14 |
+
QuerySelect,
|
| 15 |
+
JoinType,
|
| 16 |
+
RelationTrait,
|
| 17 |
+
DbBackend,
|
| 18 |
+
Statement,
|
| 19 |
+
UpdateResult,
|
| 20 |
+
};
|
| 21 |
use sea_orm::ActiveValue::Set;
|
| 22 |
use sea_orm::QueryFilter;
|
| 23 |
use crate::api::doc_info::ListParams;
|
| 24 |
+
use crate::entity::{ doc2_doc, doc_info };
|
| 25 |
use crate::entity::doc_info::Entity;
|
| 26 |
use crate::service;
|
| 27 |
|
| 28 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 29 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 30 |
}
|
| 31 |
|
| 32 |
pub struct Query;
|
| 33 |
|
| 34 |
impl Query {
|
| 35 |
+
pub async fn find_doc_info_by_id(
|
| 36 |
+
db: &DbConn,
|
| 37 |
+
id: i64
|
| 38 |
+
) -> Result<Option<doc_info::Model>, DbErr> {
|
| 39 |
Entity::find_by_id(id).one(db).await
|
| 40 |
}
|
| 41 |
|
|
|
|
| 43 |
Entity::find().all(db).await
|
| 44 |
}
|
| 45 |
|
| 46 |
+
pub async fn find_doc_infos_by_uid(
|
| 47 |
+
db: &DbConn,
|
| 48 |
+
uid: i64
|
| 49 |
+
) -> Result<Vec<doc_info::Model>, DbErr> {
|
| 50 |
+
Entity::find().filter(doc_info::Column::Uid.eq(uid)).all(db).await
|
| 51 |
}
|
| 52 |
|
| 53 |
+
pub async fn find_doc_infos_by_name(
|
| 54 |
+
db: &DbConn,
|
| 55 |
+
uid: i64,
|
| 56 |
+
name: &String,
|
| 57 |
+
parent_id: Option<i64>
|
| 58 |
+
) -> Result<Vec<doc_info::Model>, DbErr> {
|
| 59 |
let mut dids = Vec::<i64>::new();
|
| 60 |
if let Some(pid) = parent_id {
|
| 61 |
+
for d2d in doc2_doc::Entity
|
| 62 |
+
::find()
|
| 63 |
+
.filter(doc2_doc::Column::ParentId.eq(pid))
|
| 64 |
+
.all(db).await? {
|
| 65 |
dids.push(d2d.did);
|
| 66 |
}
|
| 67 |
+
} else {
|
|
|
|
| 68 |
let doc = Entity::find()
|
| 69 |
.filter(doc_info::Column::DocName.eq(name.clone()))
|
| 70 |
.filter(doc_info::Column::Uid.eq(uid))
|
| 71 |
+
.all(db).await?;
|
| 72 |
+
if doc.len() == 0 {
|
|
|
|
| 73 |
return Ok(vec![]);
|
| 74 |
}
|
| 75 |
+
assert!(doc.len() > 0);
|
| 76 |
+
let d2d = doc2_doc::Entity
|
| 77 |
+
::find()
|
| 78 |
+
.filter(doc2_doc::Column::Did.eq(doc[0].did))
|
| 79 |
+
.all(db).await?;
|
| 80 |
assert!(d2d.len() <= 1, "Did: {}->{}", doc[0].did, d2d.len());
|
| 81 |
+
if d2d.len() > 0 {
|
| 82 |
+
for d2d_ in doc2_doc::Entity
|
| 83 |
+
::find()
|
| 84 |
+
.filter(doc2_doc::Column::ParentId.eq(d2d[0].parent_id))
|
| 85 |
+
.all(db).await? {
|
| 86 |
dids.push(d2d_.did);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
+
|
| 91 |
Entity::find()
|
| 92 |
.filter(doc_info::Column::DocName.eq(name.clone()))
|
| 93 |
.filter(doc_info::Column::Uid.eq(uid))
|
| 94 |
.filter(doc_info::Column::Did.is_in(dids))
|
| 95 |
.filter(doc_info::Column::IsDeleted.eq(false))
|
| 96 |
+
.all(db).await
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
pub async fn all_descendent_ids(db: &DbConn, doc_ids: &Vec<i64>) -> Result<Vec<i64>, DbErr> {
|
| 100 |
let mut dids = doc_ids.clone();
|
| 101 |
+
let mut i: usize = 0;
|
| 102 |
loop {
|
| 103 |
if dids.len() == i {
|
| 104 |
break;
|
| 105 |
}
|
| 106 |
+
|
| 107 |
+
for d in doc2_doc::Entity
|
| 108 |
+
::find()
|
| 109 |
+
.filter(doc2_doc::Column::ParentId.eq(dids[i]))
|
| 110 |
+
.all(db).await? {
|
| 111 |
dids.push(d.did);
|
| 112 |
}
|
| 113 |
i += 1;
|
|
|
|
| 115 |
Ok(dids)
|
| 116 |
}
|
| 117 |
|
| 118 |
+
pub async fn find_doc_infos_by_params(
|
| 119 |
+
db: &DbConn,
|
| 120 |
+
params: ListParams
|
| 121 |
+
) -> Result<Vec<doc_info::Model>, DbErr> {
|
| 122 |
// Setup paginator
|
| 123 |
+
let mut sql: String =
|
| 124 |
+
"
|
| 125 |
select
|
| 126 |
a.did,
|
| 127 |
a.uid,
|
|
|
|
| 136 |
doc_info as a
|
| 137 |
".to_owned();
|
| 138 |
|
| 139 |
+
let mut cond: String = format!(" a.uid={} and a.is_deleted=False ", params.uid);
|
| 140 |
|
| 141 |
if let Some(kb_id) = params.filter.kb_id {
|
| 142 |
+
sql.push_str(
|
| 143 |
+
&format!(" inner join kb2_doc on kb2_doc.did = a.did and kb2_doc.kb_id={}", kb_id)
|
| 144 |
+
);
|
| 145 |
}
|
| 146 |
if let Some(folder_id) = params.filter.folder_id {
|
| 147 |
+
sql.push_str(
|
| 148 |
+
&format!(" inner join doc2_doc on a.did = doc2_doc.did and doc2_doc.parent_id={}", folder_id)
|
| 149 |
+
);
|
| 150 |
}
|
| 151 |
// Fetch paginated posts
|
| 152 |
if let Some(tag_id) = params.filter.tag_id {
|
| 153 |
+
let tag = service::tag_info::Query
|
| 154 |
+
::find_tag_info_by_id(tag_id, &db).await
|
| 155 |
+
.unwrap()
|
| 156 |
+
.unwrap();
|
| 157 |
+
if tag.folder_id > 0 {
|
| 158 |
+
sql.push_str(
|
| 159 |
+
&format!(
|
| 160 |
+
" inner join doc2_doc on a.did = doc2_doc.did and doc2_doc.parent_id={}",
|
| 161 |
+
tag.folder_id
|
| 162 |
+
)
|
| 163 |
+
);
|
| 164 |
}
|
| 165 |
+
if tag.regx.len() > 0 {
|
| 166 |
cond.push_str(&format!(" and doc_name ~ '{}'", tag.regx));
|
| 167 |
}
|
| 168 |
}
|
|
|
|
| 170 |
if let Some(keywords) = params.filter.keywords {
|
| 171 |
cond.push_str(&format!(" and doc_name like '%{}%'", keywords));
|
| 172 |
}
|
| 173 |
+
if cond.len() > 0 {
|
| 174 |
sql.push_str(&" where ");
|
| 175 |
sql.push_str(&cond);
|
| 176 |
}
|
|
|
|
| 179 |
orderby = "updated_at desc".to_owned();
|
| 180 |
}
|
| 181 |
sql.push_str(&format!(" order by {}", orderby));
|
| 182 |
+
let mut page_size: u32 = 30;
|
| 183 |
if let Some(pg_sz) = params.per_page {
|
| 184 |
page_size = pg_sz;
|
| 185 |
}
|
| 186 |
+
let mut page: u32 = 0;
|
| 187 |
if let Some(pg) = params.page {
|
| 188 |
page = pg;
|
| 189 |
}
|
| 190 |
+
sql.push_str(&format!(" limit {} offset {} ;", page_size, page * page_size));
|
| 191 |
+
|
| 192 |
print!("{}", sql);
|
| 193 |
Entity::find()
|
| 194 |
+
.from_raw_sql(Statement::from_sql_and_values(DbBackend::Postgres, sql, vec![]))
|
| 195 |
+
.all(db).await
|
|
|
|
|
|
|
| 196 |
}
|
| 197 |
|
| 198 |
pub async fn find_doc_infos_in_page(
|
| 199 |
db: &DbConn,
|
| 200 |
page: u64,
|
| 201 |
+
posts_per_page: u64
|
| 202 |
) -> Result<(Vec<doc_info::Model>, u64), DbErr> {
|
| 203 |
// Setup paginator
|
| 204 |
let paginator = Entity::find()
|
|
|
|
| 214 |
pub struct Mutation;
|
| 215 |
|
| 216 |
impl Mutation {
|
| 217 |
+
pub async fn mv_doc_info(db: &DbConn, dest_did: i64, dids: &[i64]) -> Result<(), DbErr> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
for did in dids {
|
| 219 |
+
let d = doc2_doc::Entity
|
| 220 |
+
::find()
|
| 221 |
+
.filter(doc2_doc::Column::Did.eq(did.to_owned()))
|
| 222 |
+
.all(db).await?;
|
| 223 |
+
|
| 224 |
+
let _ = (doc2_doc::ActiveModel {
|
| 225 |
id: Set(d[0].id),
|
| 226 |
did: Set(did.to_owned()),
|
| 227 |
+
parent_id: Set(dest_did),
|
| 228 |
+
}).update(db).await?;
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
|
| 231 |
Ok(())
|
|
|
|
| 235 |
db: &DbConn,
|
| 236 |
dest_did: i64,
|
| 237 |
did: i64
|
| 238 |
+
) -> Result<doc2_doc::ActiveModel, DbErr> {
|
| 239 |
+
(doc2_doc::ActiveModel {
|
| 240 |
id: Default::default(),
|
| 241 |
parent_id: Set(dest_did),
|
| 242 |
did: Set(did),
|
| 243 |
+
}).save(db).await
|
|
|
|
|
|
|
| 244 |
}
|
| 245 |
|
| 246 |
pub async fn create_doc_info(
|
| 247 |
db: &DbConn,
|
| 248 |
+
form_data: doc_info::Model
|
| 249 |
) -> Result<doc_info::ActiveModel, DbErr> {
|
| 250 |
+
(doc_info::ActiveModel {
|
| 251 |
did: Default::default(),
|
| 252 |
uid: Set(form_data.uid.to_owned()),
|
| 253 |
doc_name: Set(form_data.doc_name.to_owned()),
|
|
|
|
| 256 |
location: Set(form_data.location.to_owned()),
|
| 257 |
created_at: Set(form_data.created_at.to_owned()),
|
| 258 |
updated_at: Set(form_data.updated_at.to_owned()),
|
| 259 |
+
is_deleted: Default::default(),
|
| 260 |
+
}).save(db).await
|
|
|
|
|
|
|
| 261 |
}
|
| 262 |
|
| 263 |
pub async fn update_doc_info_by_id(
|
| 264 |
db: &DbConn,
|
| 265 |
id: i64,
|
| 266 |
+
form_data: doc_info::Model
|
| 267 |
) -> Result<doc_info::Model, DbErr> {
|
| 268 |
let doc_info: doc_info::ActiveModel = Entity::find_by_id(id)
|
| 269 |
+
.one(db).await?
|
|
|
|
| 270 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 271 |
.map(Into::into)?;
|
| 272 |
|
| 273 |
+
(doc_info::ActiveModel {
|
| 274 |
did: doc_info.did,
|
| 275 |
uid: Set(form_data.uid.to_owned()),
|
| 276 |
doc_name: Set(form_data.doc_name.to_owned()),
|
|
|
|
| 280 |
created_at: doc_info.created_at,
|
| 281 |
updated_at: Set(now()),
|
| 282 |
is_deleted: Default::default(),
|
| 283 |
+
}).update(db).await
|
|
|
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
pub async fn delete_doc_info(db: &DbConn, doc_ids: &Vec<i64>) -> Result<UpdateResult, DbErr> {
|
| 287 |
let mut dids = doc_ids.clone();
|
| 288 |
+
let mut i: usize = 0;
|
| 289 |
loop {
|
| 290 |
if dids.len() == i {
|
| 291 |
break;
|
| 292 |
}
|
| 293 |
let mut doc: doc_info::ActiveModel = Entity::find_by_id(dids[i])
|
| 294 |
+
.one(db).await?
|
| 295 |
+
.ok_or(DbErr::Custom(format!("Can't find doc:{}", dids[i])))
|
| 296 |
+
.map(Into::into)?;
|
| 297 |
+
doc.updated_at = Set(now());
|
|
|
|
| 298 |
doc.is_deleted = Set(true);
|
| 299 |
let _ = doc.update(db).await?;
|
| 300 |
+
|
| 301 |
+
for d in doc2_doc::Entity
|
| 302 |
+
::find()
|
| 303 |
+
.filter(doc2_doc::Column::ParentId.eq(dids[i]))
|
| 304 |
+
.all(db).await? {
|
| 305 |
dids.push(d.did);
|
| 306 |
}
|
| 307 |
+
let _ = doc2_doc::Entity
|
| 308 |
+
::delete_many()
|
| 309 |
+
.filter(doc2_doc::Column::ParentId.eq(dids[i]))
|
| 310 |
+
.exec(db).await?;
|
| 311 |
+
let _ = doc2_doc::Entity
|
| 312 |
+
::delete_many()
|
| 313 |
+
.filter(doc2_doc::Column::Did.eq(dids[i]))
|
| 314 |
+
.exec(db).await?;
|
| 315 |
i += 1;
|
| 316 |
}
|
| 317 |
+
crate::service::kb_info::Mutation::remove_docs(&db, dids, None).await
|
| 318 |
}
|
| 319 |
|
| 320 |
pub async fn rename(db: &DbConn, doc_id: i64, name: &String) -> Result<doc_info::Model, DbErr> {
|
| 321 |
let mut doc: doc_info::ActiveModel = Entity::find_by_id(doc_id)
|
| 322 |
+
.one(db).await?
|
|
|
|
| 323 |
.ok_or(DbErr::Custom(format!("Can't find doc:{}", doc_id)))
|
| 324 |
.map(Into::into)?;
|
| 325 |
+
doc.updated_at = Set(now());
|
| 326 |
+
doc.doc_name = Set(name.clone());
|
| 327 |
+
doc.update(db).await
|
| 328 |
}
|
| 329 |
|
| 330 |
pub async fn delete_all_doc_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
src/service/kb_info.rs
CHANGED
|
@@ -1,13 +1,24 @@
|
|
| 1 |
-
use chrono::{Local, FixedOffset, Utc};
|
| 2 |
use migration::Expr;
|
| 3 |
-
use sea_orm::{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
use sea_orm::ActiveValue::Set;
|
| 5 |
use crate::entity::kb_info;
|
| 6 |
use crate::entity::kb2_doc;
|
| 7 |
use crate::entity::kb_info::Entity;
|
| 8 |
|
| 9 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 10 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 11 |
}
|
| 12 |
pub struct Query;
|
| 13 |
|
|
@@ -21,22 +32,25 @@ impl Query {
|
|
| 21 |
}
|
| 22 |
|
| 23 |
pub async fn find_kb_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<kb_info::Model>, DbErr> {
|
| 24 |
-
Entity::find()
|
| 25 |
-
.filter(kb_info::Column::Uid.eq(uid))
|
| 26 |
-
.all(db)
|
| 27 |
-
.await
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
pub async fn find_kb_infos_by_name(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
}
|
| 36 |
|
| 37 |
-
pub async fn find_kb_by_docs(
|
|
|
|
|
|
|
|
|
|
| 38 |
let mut kbids = Vec::<i64>::new();
|
| 39 |
-
for k in kb2_doc::Entity
|
|
|
|
|
|
|
|
|
|
| 40 |
kbids.push(k.kb_id);
|
| 41 |
}
|
| 42 |
Entity::find().filter(kb_info::Column::KbId.is_in(kbids)).all(db).await
|
|
@@ -45,7 +59,7 @@ impl Query {
|
|
| 45 |
pub async fn find_kb_infos_in_page(
|
| 46 |
db: &DbConn,
|
| 47 |
page: u64,
|
| 48 |
-
posts_per_page: u64
|
| 49 |
) -> Result<(Vec<kb_info::Model>, u64), DbErr> {
|
| 50 |
// Setup paginator
|
| 51 |
let paginator = Entity::find()
|
|
@@ -63,44 +77,38 @@ pub struct Mutation;
|
|
| 63 |
impl Mutation {
|
| 64 |
pub async fn create_kb_info(
|
| 65 |
db: &DbConn,
|
| 66 |
-
form_data: kb_info::Model
|
| 67 |
) -> Result<kb_info::ActiveModel, DbErr> {
|
| 68 |
-
kb_info::ActiveModel {
|
| 69 |
kb_id: Default::default(),
|
| 70 |
uid: Set(form_data.uid.to_owned()),
|
| 71 |
kb_name: Set(form_data.kb_name.to_owned()),
|
| 72 |
icon: Set(form_data.icon.to_owned()),
|
| 73 |
created_at: Set(now()),
|
| 74 |
updated_at: Set(now()),
|
| 75 |
-
is_deleted:Default::default()
|
| 76 |
-
}
|
| 77 |
-
.save(db)
|
| 78 |
-
.await
|
| 79 |
}
|
| 80 |
|
| 81 |
-
pub async fn add_docs(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)-> Result<(), DbErr> {
|
| 86 |
-
for did in doc_ids{
|
| 87 |
-
let res = kb2_doc::Entity::find()
|
| 88 |
.filter(kb2_doc::Column::KbId.eq(kb_id))
|
| 89 |
.filter(kb2_doc::Column::Did.eq(did))
|
| 90 |
-
.all(db)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
id: Default::default(),
|
| 95 |
kb_id: Set(kb_id),
|
| 96 |
did: Set(did),
|
| 97 |
kb_progress: Set(0.0),
|
| 98 |
kb_progress_msg: Set("".to_owned()),
|
| 99 |
updated_at: Set(now()),
|
| 100 |
-
is_deleted:Default::default()
|
| 101 |
-
}
|
| 102 |
-
.save(db)
|
| 103 |
-
.await?;
|
| 104 |
}
|
| 105 |
|
| 106 |
Ok(())
|
|
@@ -110,17 +118,16 @@ impl Mutation {
|
|
| 110 |
db: &DbConn,
|
| 111 |
doc_ids: Vec<i64>,
|
| 112 |
kb_id: Option<i64>
|
| 113 |
-
)-> Result<UpdateResult, DbErr>
|
| 114 |
-
let update = kb2_doc::Entity
|
|
|
|
| 115 |
.col_expr(kb2_doc::Column::IsDeleted, Expr::value(true))
|
| 116 |
.col_expr(kb2_doc::Column::KbProgress, Expr::value(0))
|
| 117 |
.col_expr(kb2_doc::Column::KbProgressMsg, Expr::value(""))
|
| 118 |
.filter(kb2_doc::Column::Did.is_in(doc_ids));
|
| 119 |
-
if let Some(kbid) = kb_id{
|
| 120 |
-
update.filter(kb2_doc::Column::KbId.eq(kbid))
|
| 121 |
-
|
| 122 |
-
.await
|
| 123 |
-
}else{
|
| 124 |
update.exec(db).await
|
| 125 |
}
|
| 126 |
}
|
|
@@ -128,35 +135,31 @@ impl Mutation {
|
|
| 128 |
pub async fn update_kb_info_by_id(
|
| 129 |
db: &DbConn,
|
| 130 |
id: i64,
|
| 131 |
-
form_data: kb_info::Model
|
| 132 |
) -> Result<kb_info::Model, DbErr> {
|
| 133 |
let kb_info: kb_info::ActiveModel = Entity::find_by_id(id)
|
| 134 |
-
.one(db)
|
| 135 |
-
.await?
|
| 136 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 137 |
.map(Into::into)?;
|
| 138 |
|
| 139 |
-
kb_info::ActiveModel {
|
| 140 |
kb_id: kb_info.kb_id,
|
| 141 |
uid: kb_info.uid,
|
| 142 |
kb_name: Set(form_data.kb_name.to_owned()),
|
| 143 |
icon: Set(form_data.icon.to_owned()),
|
| 144 |
created_at: kb_info.created_at,
|
| 145 |
updated_at: Set(now()),
|
| 146 |
-
is_deleted: Default::default()
|
| 147 |
-
}
|
| 148 |
-
.update(db)
|
| 149 |
-
.await
|
| 150 |
}
|
| 151 |
|
| 152 |
pub async fn delete_kb_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
|
| 153 |
let kb: kb_info::ActiveModel = Entity::find_by_id(kb_id)
|
| 154 |
-
.one(db)
|
| 155 |
-
.await?
|
| 156 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 157 |
.map(Into::into)?;
|
| 158 |
|
| 159 |
-
|
| 160 |
}
|
| 161 |
|
| 162 |
pub async fn delete_all_kb_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
|
|
|
| 1 |
+
use chrono::{ Local, FixedOffset, Utc };
|
| 2 |
use migration::Expr;
|
| 3 |
+
use sea_orm::{
|
| 4 |
+
ActiveModelTrait,
|
| 5 |
+
ColumnTrait,
|
| 6 |
+
DbConn,
|
| 7 |
+
DbErr,
|
| 8 |
+
DeleteResult,
|
| 9 |
+
EntityTrait,
|
| 10 |
+
PaginatorTrait,
|
| 11 |
+
QueryFilter,
|
| 12 |
+
QueryOrder,
|
| 13 |
+
UpdateResult,
|
| 14 |
+
};
|
| 15 |
use sea_orm::ActiveValue::Set;
|
| 16 |
use crate::entity::kb_info;
|
| 17 |
use crate::entity::kb2_doc;
|
| 18 |
use crate::entity::kb_info::Entity;
|
| 19 |
|
| 20 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 21 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 22 |
}
|
| 23 |
pub struct Query;
|
| 24 |
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
pub async fn find_kb_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<kb_info::Model>, DbErr> {
|
| 35 |
+
Entity::find().filter(kb_info::Column::Uid.eq(uid)).all(db).await
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
+
|
| 38 |
+
pub async fn find_kb_infos_by_name(
|
| 39 |
+
db: &DbConn,
|
| 40 |
+
name: String
|
| 41 |
+
) -> Result<Vec<kb_info::Model>, DbErr> {
|
| 42 |
+
Entity::find().filter(kb_info::Column::KbName.eq(name)).all(db).await
|
| 43 |
}
|
| 44 |
|
| 45 |
+
pub async fn find_kb_by_docs(
|
| 46 |
+
db: &DbConn,
|
| 47 |
+
doc_ids: Vec<i64>
|
| 48 |
+
) -> Result<Vec<kb_info::Model>, DbErr> {
|
| 49 |
let mut kbids = Vec::<i64>::new();
|
| 50 |
+
for k in kb2_doc::Entity
|
| 51 |
+
::find()
|
| 52 |
+
.filter(kb2_doc::Column::Did.is_in(doc_ids))
|
| 53 |
+
.all(db).await? {
|
| 54 |
kbids.push(k.kb_id);
|
| 55 |
}
|
| 56 |
Entity::find().filter(kb_info::Column::KbId.is_in(kbids)).all(db).await
|
|
|
|
| 59 |
pub async fn find_kb_infos_in_page(
|
| 60 |
db: &DbConn,
|
| 61 |
page: u64,
|
| 62 |
+
posts_per_page: u64
|
| 63 |
) -> Result<(Vec<kb_info::Model>, u64), DbErr> {
|
| 64 |
// Setup paginator
|
| 65 |
let paginator = Entity::find()
|
|
|
|
| 77 |
impl Mutation {
|
| 78 |
pub async fn create_kb_info(
|
| 79 |
db: &DbConn,
|
| 80 |
+
form_data: kb_info::Model
|
| 81 |
) -> Result<kb_info::ActiveModel, DbErr> {
|
| 82 |
+
(kb_info::ActiveModel {
|
| 83 |
kb_id: Default::default(),
|
| 84 |
uid: Set(form_data.uid.to_owned()),
|
| 85 |
kb_name: Set(form_data.kb_name.to_owned()),
|
| 86 |
icon: Set(form_data.icon.to_owned()),
|
| 87 |
created_at: Set(now()),
|
| 88 |
updated_at: Set(now()),
|
| 89 |
+
is_deleted: Default::default(),
|
| 90 |
+
}).save(db).await
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
+
pub async fn add_docs(db: &DbConn, kb_id: i64, doc_ids: Vec<i64>) -> Result<(), DbErr> {
|
| 94 |
+
for did in doc_ids {
|
| 95 |
+
let res = kb2_doc::Entity
|
| 96 |
+
::find()
|
|
|
|
|
|
|
|
|
|
| 97 |
.filter(kb2_doc::Column::KbId.eq(kb_id))
|
| 98 |
.filter(kb2_doc::Column::Did.eq(did))
|
| 99 |
+
.all(db).await?;
|
| 100 |
+
if res.len() > 0 {
|
| 101 |
+
continue;
|
| 102 |
+
}
|
| 103 |
+
let _ = (kb2_doc::ActiveModel {
|
| 104 |
id: Default::default(),
|
| 105 |
kb_id: Set(kb_id),
|
| 106 |
did: Set(did),
|
| 107 |
kb_progress: Set(0.0),
|
| 108 |
kb_progress_msg: Set("".to_owned()),
|
| 109 |
updated_at: Set(now()),
|
| 110 |
+
is_deleted: Default::default(),
|
| 111 |
+
}).save(db).await?;
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
Ok(())
|
|
|
|
| 118 |
db: &DbConn,
|
| 119 |
doc_ids: Vec<i64>,
|
| 120 |
kb_id: Option<i64>
|
| 121 |
+
) -> Result<UpdateResult, DbErr> {
|
| 122 |
+
let update = kb2_doc::Entity
|
| 123 |
+
::update_many()
|
| 124 |
.col_expr(kb2_doc::Column::IsDeleted, Expr::value(true))
|
| 125 |
.col_expr(kb2_doc::Column::KbProgress, Expr::value(0))
|
| 126 |
.col_expr(kb2_doc::Column::KbProgressMsg, Expr::value(""))
|
| 127 |
.filter(kb2_doc::Column::Did.is_in(doc_ids));
|
| 128 |
+
if let Some(kbid) = kb_id {
|
| 129 |
+
update.filter(kb2_doc::Column::KbId.eq(kbid)).exec(db).await
|
| 130 |
+
} else {
|
|
|
|
|
|
|
| 131 |
update.exec(db).await
|
| 132 |
}
|
| 133 |
}
|
|
|
|
| 135 |
pub async fn update_kb_info_by_id(
|
| 136 |
db: &DbConn,
|
| 137 |
id: i64,
|
| 138 |
+
form_data: kb_info::Model
|
| 139 |
) -> Result<kb_info::Model, DbErr> {
|
| 140 |
let kb_info: kb_info::ActiveModel = Entity::find_by_id(id)
|
| 141 |
+
.one(db).await?
|
|
|
|
| 142 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 143 |
.map(Into::into)?;
|
| 144 |
|
| 145 |
+
(kb_info::ActiveModel {
|
| 146 |
kb_id: kb_info.kb_id,
|
| 147 |
uid: kb_info.uid,
|
| 148 |
kb_name: Set(form_data.kb_name.to_owned()),
|
| 149 |
icon: Set(form_data.icon.to_owned()),
|
| 150 |
created_at: kb_info.created_at,
|
| 151 |
updated_at: Set(now()),
|
| 152 |
+
is_deleted: Default::default(),
|
| 153 |
+
}).update(db).await
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
pub async fn delete_kb_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
|
| 157 |
let kb: kb_info::ActiveModel = Entity::find_by_id(kb_id)
|
| 158 |
+
.one(db).await?
|
|
|
|
| 159 |
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
| 160 |
.map(Into::into)?;
|
| 161 |
|
| 162 |
+
kb.delete(db).await
|
| 163 |
}
|
| 164 |
|
| 165 |
pub async fn delete_all_kb_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
src/service/mod.rs
CHANGED
|
@@ -2,4 +2,4 @@ pub(crate) mod dialog_info;
|
|
| 2 |
pub(crate) mod tag_info;
|
| 3 |
pub(crate) mod kb_info;
|
| 4 |
pub(crate) mod doc_info;
|
| 5 |
-
pub(crate) mod user_info;
|
|
|
|
| 2 |
pub(crate) mod tag_info;
|
| 3 |
pub(crate) mod kb_info;
|
| 4 |
pub(crate) mod doc_info;
|
| 5 |
+
pub(crate) mod user_info;
|
src/service/tag_info.rs
CHANGED
|
@@ -1,30 +1,40 @@
|
|
| 1 |
-
use chrono::{FixedOffset, Utc};
|
| 2 |
-
use sea_orm::{
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
use crate::entity::tag_info;
|
| 5 |
use crate::entity::tag_info::Entity;
|
| 6 |
|
| 7 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 8 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 9 |
}
|
| 10 |
pub struct Query;
|
| 11 |
|
| 12 |
impl Query {
|
| 13 |
-
pub async fn find_tag_info_by_id(
|
|
|
|
|
|
|
|
|
|
| 14 |
Entity::find_by_id(id).one(db).await
|
| 15 |
}
|
| 16 |
|
| 17 |
-
pub async fn find_tags_by_uid(uid:i64, db: &DbConn) -> Result<Vec<tag_info::Model>, DbErr> {
|
| 18 |
-
Entity::find()
|
| 19 |
-
.filter(tag_info::Column::Uid.eq(uid))
|
| 20 |
-
.all(db)
|
| 21 |
-
.await
|
| 22 |
}
|
| 23 |
|
| 24 |
pub async fn find_tag_infos_in_page(
|
| 25 |
db: &DbConn,
|
| 26 |
page: u64,
|
| 27 |
-
posts_per_page: u64
|
| 28 |
) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
|
| 29 |
// Setup paginator
|
| 30 |
let paginator = Entity::find()
|
|
@@ -42,9 +52,9 @@ pub struct Mutation;
|
|
| 42 |
impl Mutation {
|
| 43 |
pub async fn create_tag(
|
| 44 |
db: &DbConn,
|
| 45 |
-
form_data: tag_info::Model
|
| 46 |
) -> Result<tag_info::ActiveModel, DbErr> {
|
| 47 |
-
tag_info::ActiveModel {
|
| 48 |
tid: Default::default(),
|
| 49 |
uid: Set(form_data.uid.to_owned()),
|
| 50 |
tag_name: Set(form_data.tag_name.to_owned()),
|
|
@@ -53,27 +63,24 @@ impl Mutation {
|
|
| 53 |
icon: Set(form_data.icon.to_owned()),
|
| 54 |
folder_id: match form_data.folder_id {
|
| 55 |
0 => NotSet,
|
| 56 |
-
_ => Set(form_data.folder_id.to_owned())
|
| 57 |
},
|
| 58 |
created_at: Set(now()),
|
| 59 |
updated_at: Set(now()),
|
| 60 |
-
}
|
| 61 |
-
.save(db)
|
| 62 |
-
.await
|
| 63 |
}
|
| 64 |
|
| 65 |
pub async fn update_tag_by_id(
|
| 66 |
db: &DbConn,
|
| 67 |
id: i64,
|
| 68 |
-
form_data: tag_info::Model
|
| 69 |
) -> Result<tag_info::Model, DbErr> {
|
| 70 |
let tag: tag_info::ActiveModel = Entity::find_by_id(id)
|
| 71 |
-
.one(db)
|
| 72 |
-
.await?
|
| 73 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 74 |
.map(Into::into)?;
|
| 75 |
|
| 76 |
-
tag_info::ActiveModel {
|
| 77 |
tid: tag.tid,
|
| 78 |
uid: tag.uid,
|
| 79 |
tag_name: Set(form_data.tag_name.to_owned()),
|
|
@@ -83,15 +90,12 @@ impl Mutation {
|
|
| 83 |
folder_id: Set(form_data.folder_id.to_owned()),
|
| 84 |
created_at: Default::default(),
|
| 85 |
updated_at: Set(now()),
|
| 86 |
-
}
|
| 87 |
-
.update(db)
|
| 88 |
-
.await
|
| 89 |
}
|
| 90 |
|
| 91 |
pub async fn delete_tag(db: &DbConn, tid: i64) -> Result<DeleteResult, DbErr> {
|
| 92 |
let tag: tag_info::ActiveModel = Entity::find_by_id(tid)
|
| 93 |
-
.one(db)
|
| 94 |
-
.await?
|
| 95 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 96 |
.map(Into::into)?;
|
| 97 |
|
|
@@ -101,4 +105,4 @@ impl Mutation {
|
|
| 101 |
pub async fn delete_all_tags(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 102 |
Entity::delete_many().exec(db).await
|
| 103 |
}
|
| 104 |
-
}
|
|
|
|
| 1 |
+
use chrono::{ FixedOffset, Utc };
|
| 2 |
+
use sea_orm::{
|
| 3 |
+
ActiveModelTrait,
|
| 4 |
+
DbConn,
|
| 5 |
+
DbErr,
|
| 6 |
+
DeleteResult,
|
| 7 |
+
EntityTrait,
|
| 8 |
+
PaginatorTrait,
|
| 9 |
+
QueryOrder,
|
| 10 |
+
ColumnTrait,
|
| 11 |
+
QueryFilter,
|
| 12 |
+
};
|
| 13 |
+
use sea_orm::ActiveValue::{ Set, NotSet };
|
| 14 |
use crate::entity::tag_info;
|
| 15 |
use crate::entity::tag_info::Entity;
|
| 16 |
|
| 17 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 18 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 19 |
}
|
| 20 |
pub struct Query;
|
| 21 |
|
| 22 |
impl Query {
|
| 23 |
+
pub async fn find_tag_info_by_id(
|
| 24 |
+
id: i64,
|
| 25 |
+
db: &DbConn
|
| 26 |
+
) -> Result<Option<tag_info::Model>, DbErr> {
|
| 27 |
Entity::find_by_id(id).one(db).await
|
| 28 |
}
|
| 29 |
|
| 30 |
+
pub async fn find_tags_by_uid(uid: i64, db: &DbConn) -> Result<Vec<tag_info::Model>, DbErr> {
|
| 31 |
+
Entity::find().filter(tag_info::Column::Uid.eq(uid)).all(db).await
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
pub async fn find_tag_infos_in_page(
|
| 35 |
db: &DbConn,
|
| 36 |
page: u64,
|
| 37 |
+
posts_per_page: u64
|
| 38 |
) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
|
| 39 |
// Setup paginator
|
| 40 |
let paginator = Entity::find()
|
|
|
|
| 52 |
impl Mutation {
|
| 53 |
pub async fn create_tag(
|
| 54 |
db: &DbConn,
|
| 55 |
+
form_data: tag_info::Model
|
| 56 |
) -> Result<tag_info::ActiveModel, DbErr> {
|
| 57 |
+
(tag_info::ActiveModel {
|
| 58 |
tid: Default::default(),
|
| 59 |
uid: Set(form_data.uid.to_owned()),
|
| 60 |
tag_name: Set(form_data.tag_name.to_owned()),
|
|
|
|
| 63 |
icon: Set(form_data.icon.to_owned()),
|
| 64 |
folder_id: match form_data.folder_id {
|
| 65 |
0 => NotSet,
|
| 66 |
+
_ => Set(form_data.folder_id.to_owned()),
|
| 67 |
},
|
| 68 |
created_at: Set(now()),
|
| 69 |
updated_at: Set(now()),
|
| 70 |
+
}).save(db).await
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
pub async fn update_tag_by_id(
|
| 74 |
db: &DbConn,
|
| 75 |
id: i64,
|
| 76 |
+
form_data: tag_info::Model
|
| 77 |
) -> Result<tag_info::Model, DbErr> {
|
| 78 |
let tag: tag_info::ActiveModel = Entity::find_by_id(id)
|
| 79 |
+
.one(db).await?
|
|
|
|
| 80 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 81 |
.map(Into::into)?;
|
| 82 |
|
| 83 |
+
(tag_info::ActiveModel {
|
| 84 |
tid: tag.tid,
|
| 85 |
uid: tag.uid,
|
| 86 |
tag_name: Set(form_data.tag_name.to_owned()),
|
|
|
|
| 90 |
folder_id: Set(form_data.folder_id.to_owned()),
|
| 91 |
created_at: Default::default(),
|
| 92 |
updated_at: Set(now()),
|
| 93 |
+
}).update(db).await
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
pub async fn delete_tag(db: &DbConn, tid: i64) -> Result<DeleteResult, DbErr> {
|
| 97 |
let tag: tag_info::ActiveModel = Entity::find_by_id(tid)
|
| 98 |
+
.one(db).await?
|
|
|
|
| 99 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 100 |
.map(Into::into)?;
|
| 101 |
|
|
|
|
| 105 |
pub async fn delete_all_tags(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 106 |
Entity::delete_many().exec(db).await
|
| 107 |
}
|
| 108 |
+
}
|
src/service/user_info.rs
CHANGED
|
@@ -1,39 +1,56 @@
|
|
| 1 |
-
use chrono::{FixedOffset, Utc};
|
| 2 |
use migration::Expr;
|
| 3 |
-
use sea_orm::{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
use sea_orm::ActiveValue::Set;
|
| 5 |
use crate::entity::user_info;
|
| 6 |
use crate::entity::user_info::Entity;
|
| 7 |
|
| 8 |
-
fn now()->chrono::DateTime<FixedOffset>{
|
| 9 |
-
Utc::now().with_timezone(&FixedOffset::east_opt(3600*8).unwrap())
|
| 10 |
}
|
| 11 |
pub struct Query;
|
| 12 |
|
| 13 |
impl Query {
|
| 14 |
-
pub async fn find_user_info_by_id(
|
|
|
|
|
|
|
|
|
|
| 15 |
Entity::find_by_id(id).one(db).await
|
| 16 |
}
|
| 17 |
|
| 18 |
-
pub async fn login(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Entity::find()
|
| 20 |
.filter(user_info::Column::Email.eq(email))
|
| 21 |
.filter(user_info::Column::Password.eq(password))
|
| 22 |
-
.one(db)
|
| 23 |
-
.await
|
| 24 |
}
|
| 25 |
|
| 26 |
-
pub async fn find_user_infos(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
}
|
| 32 |
|
| 33 |
pub async fn find_user_infos_in_page(
|
| 34 |
db: &DbConn,
|
| 35 |
page: u64,
|
| 36 |
-
posts_per_page: u64
|
| 37 |
) -> Result<(Vec<user_info::Model>, u64), DbErr> {
|
| 38 |
// Setup paginator
|
| 39 |
let paginator = Entity::find()
|
|
@@ -51,9 +68,9 @@ pub struct Mutation;
|
|
| 51 |
impl Mutation {
|
| 52 |
pub async fn create_user(
|
| 53 |
db: &DbConn,
|
| 54 |
-
form_data: &user_info::Model
|
| 55 |
) -> Result<user_info::ActiveModel, DbErr> {
|
| 56 |
-
user_info::ActiveModel {
|
| 57 |
uid: Default::default(),
|
| 58 |
email: Set(form_data.email.to_owned()),
|
| 59 |
nickname: Set(form_data.nickname.to_owned()),
|
|
@@ -65,22 +82,19 @@ impl Mutation {
|
|
| 65 |
last_login_at: Set(now()),
|
| 66 |
created_at: Set(now()),
|
| 67 |
updated_at: Set(now()),
|
| 68 |
-
}
|
| 69 |
-
.save(db)
|
| 70 |
-
.await
|
| 71 |
}
|
| 72 |
|
| 73 |
pub async fn update_user_by_id(
|
| 74 |
db: &DbConn,
|
| 75 |
-
form_data: &user_info::Model
|
| 76 |
) -> Result<user_info::Model, DbErr> {
|
| 77 |
let usr: user_info::ActiveModel = Entity::find_by_id(form_data.uid)
|
| 78 |
-
.one(db)
|
| 79 |
-
.await?
|
| 80 |
.ok_or(DbErr::Custom("Cannot find user.".to_owned()))
|
| 81 |
.map(Into::into)?;
|
| 82 |
|
| 83 |
-
user_info::ActiveModel {
|
| 84 |
uid: Set(form_data.uid),
|
| 85 |
email: Set(form_data.email.to_owned()),
|
| 86 |
nickname: Set(form_data.nickname.to_owned()),
|
|
@@ -91,27 +105,20 @@ impl Mutation {
|
|
| 91 |
password: Set(form_data.password.to_owned()),
|
| 92 |
updated_at: Set(now()),
|
| 93 |
last_login_at: usr.last_login_at,
|
| 94 |
-
created_at:usr.created_at,
|
| 95 |
-
}
|
| 96 |
-
.update(db)
|
| 97 |
-
.await
|
| 98 |
}
|
| 99 |
|
| 100 |
-
pub async fn update_login_status(
|
| 101 |
-
uid: i64,
|
| 102 |
-
db: &DbConn
|
| 103 |
-
) -> Result<UpdateResult, DbErr> {
|
| 104 |
Entity::update_many()
|
| 105 |
-
.col_expr(user_info::Column::LastLoginAt,
|
| 106 |
.filter(user_info::Column::Uid.eq(uid))
|
| 107 |
-
.exec(db)
|
| 108 |
-
.await
|
| 109 |
}
|
| 110 |
|
| 111 |
pub async fn delete_user(db: &DbConn, tid: i64) -> Result<DeleteResult, DbErr> {
|
| 112 |
let tag: user_info::ActiveModel = Entity::find_by_id(tid)
|
| 113 |
-
.one(db)
|
| 114 |
-
.await?
|
| 115 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 116 |
.map(Into::into)?;
|
| 117 |
|
|
@@ -121,4 +128,4 @@ impl Mutation {
|
|
| 121 |
pub async fn delete_all(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 122 |
Entity::delete_many().exec(db).await
|
| 123 |
}
|
| 124 |
-
}
|
|
|
|
| 1 |
+
use chrono::{ FixedOffset, Utc };
|
| 2 |
use migration::Expr;
|
| 3 |
+
use sea_orm::{
|
| 4 |
+
ActiveModelTrait,
|
| 5 |
+
ColumnTrait,
|
| 6 |
+
DbConn,
|
| 7 |
+
DbErr,
|
| 8 |
+
DeleteResult,
|
| 9 |
+
EntityTrait,
|
| 10 |
+
PaginatorTrait,
|
| 11 |
+
QueryFilter,
|
| 12 |
+
QueryOrder,
|
| 13 |
+
UpdateResult,
|
| 14 |
+
};
|
| 15 |
use sea_orm::ActiveValue::Set;
|
| 16 |
use crate::entity::user_info;
|
| 17 |
use crate::entity::user_info::Entity;
|
| 18 |
|
| 19 |
+
fn now() -> chrono::DateTime<FixedOffset> {
|
| 20 |
+
Utc::now().with_timezone(&FixedOffset::east_opt(3600 * 8).unwrap())
|
| 21 |
}
|
| 22 |
pub struct Query;
|
| 23 |
|
| 24 |
impl Query {
|
| 25 |
+
pub async fn find_user_info_by_id(
|
| 26 |
+
db: &DbConn,
|
| 27 |
+
id: i64
|
| 28 |
+
) -> Result<Option<user_info::Model>, DbErr> {
|
| 29 |
Entity::find_by_id(id).one(db).await
|
| 30 |
}
|
| 31 |
|
| 32 |
+
pub async fn login(
|
| 33 |
+
db: &DbConn,
|
| 34 |
+
email: &str,
|
| 35 |
+
password: &str
|
| 36 |
+
) -> Result<Option<user_info::Model>, DbErr> {
|
| 37 |
Entity::find()
|
| 38 |
.filter(user_info::Column::Email.eq(email))
|
| 39 |
.filter(user_info::Column::Password.eq(password))
|
| 40 |
+
.one(db).await
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
+
pub async fn find_user_infos(
|
| 44 |
+
db: &DbConn,
|
| 45 |
+
email: &String
|
| 46 |
+
) -> Result<Option<user_info::Model>, DbErr> {
|
| 47 |
+
Entity::find().filter(user_info::Column::Email.eq(email)).one(db).await
|
| 48 |
}
|
| 49 |
|
| 50 |
pub async fn find_user_infos_in_page(
|
| 51 |
db: &DbConn,
|
| 52 |
page: u64,
|
| 53 |
+
posts_per_page: u64
|
| 54 |
) -> Result<(Vec<user_info::Model>, u64), DbErr> {
|
| 55 |
// Setup paginator
|
| 56 |
let paginator = Entity::find()
|
|
|
|
| 68 |
impl Mutation {
|
| 69 |
pub async fn create_user(
|
| 70 |
db: &DbConn,
|
| 71 |
+
form_data: &user_info::Model
|
| 72 |
) -> Result<user_info::ActiveModel, DbErr> {
|
| 73 |
+
(user_info::ActiveModel {
|
| 74 |
uid: Default::default(),
|
| 75 |
email: Set(form_data.email.to_owned()),
|
| 76 |
nickname: Set(form_data.nickname.to_owned()),
|
|
|
|
| 82 |
last_login_at: Set(now()),
|
| 83 |
created_at: Set(now()),
|
| 84 |
updated_at: Set(now()),
|
| 85 |
+
}).save(db).await
|
|
|
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
pub async fn update_user_by_id(
|
| 89 |
db: &DbConn,
|
| 90 |
+
form_data: &user_info::Model
|
| 91 |
) -> Result<user_info::Model, DbErr> {
|
| 92 |
let usr: user_info::ActiveModel = Entity::find_by_id(form_data.uid)
|
| 93 |
+
.one(db).await?
|
|
|
|
| 94 |
.ok_or(DbErr::Custom("Cannot find user.".to_owned()))
|
| 95 |
.map(Into::into)?;
|
| 96 |
|
| 97 |
+
(user_info::ActiveModel {
|
| 98 |
uid: Set(form_data.uid),
|
| 99 |
email: Set(form_data.email.to_owned()),
|
| 100 |
nickname: Set(form_data.nickname.to_owned()),
|
|
|
|
| 105 |
password: Set(form_data.password.to_owned()),
|
| 106 |
updated_at: Set(now()),
|
| 107 |
last_login_at: usr.last_login_at,
|
| 108 |
+
created_at: usr.created_at,
|
| 109 |
+
}).update(db).await
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
+
pub async fn update_login_status(uid: i64, db: &DbConn) -> Result<UpdateResult, DbErr> {
|
|
|
|
|
|
|
|
|
|
| 113 |
Entity::update_many()
|
| 114 |
+
.col_expr(user_info::Column::LastLoginAt, Expr::value(now()))
|
| 115 |
.filter(user_info::Column::Uid.eq(uid))
|
| 116 |
+
.exec(db).await
|
|
|
|
| 117 |
}
|
| 118 |
|
| 119 |
pub async fn delete_user(db: &DbConn, tid: i64) -> Result<DeleteResult, DbErr> {
|
| 120 |
let tag: user_info::ActiveModel = Entity::find_by_id(tid)
|
| 121 |
+
.one(db).await?
|
|
|
|
| 122 |
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
| 123 |
.map(Into::into)?;
|
| 124 |
|
|
|
|
| 128 |
pub async fn delete_all(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
| 129 |
Entity::delete_many().exec(db).await
|
| 130 |
}
|
| 131 |
+
}
|