Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl tags api #2

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HOST=127.0.0.1
PORT=8000
DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt"
3 changes: 3 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HOST=127.0.0.1
PORT=8000
DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Cargo.lock
*.pdb

.idea/
.env
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ edition = "2021"
[dependencies]
actix-web = "4.3.1"
actix-rt = "2.8.0"
actix-files = "0.6.2"
postgres = "0.19.7"
sea-orm = {version = "0.12.9", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros"]}
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
tracing-subscriber = "0.3.18"
dotenvy = "0.15.7"
listenfd = "1.0.1"
migration = { path = "./migration" }

[[bin]]
name = "doc_gpt"
Expand Down
2 changes: 1 addition & 1 deletion migration/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Running Migrator CLI
# Running Migrator CLI

- Generate a new migration file
```sh
Expand Down
38 changes: 20 additions & 18 deletions migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ impl MigrationTrait for Migration {
.table(TagInfo::Table)
.if_not_exists()
.col(
ColumnDef::new(TagInfo::Uid)
ColumnDef::new(TagInfo::Tid)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(TagInfo::Uid).big_integer().not_null())
.col(ColumnDef::new(TagInfo::TagName).string().not_null())
.col(ColumnDef::new(TagInfo::Regx).string())
.col(ColumnDef::new(TagInfo::Color).big_integer().default(1))
Expand All @@ -61,8 +62,7 @@ impl MigrationTrait for Migration {
.table(Tag2Doc::Table)
.if_not_exists()
.col(ColumnDef::new(Tag2Doc::TagId).big_integer())
.col(ColumnDef::new(Tag2Doc::Did).big_integer().comment("doc id, did in docinfo"))
.index(Index::create().col(Tag2Doc::TagId))
.col(ColumnDef::new(Tag2Doc::Did).big_integer())
.to_owned(),
)
.await?;
Expand All @@ -73,8 +73,7 @@ impl MigrationTrait for Migration {
.table(Kb2Doc::Table)
.if_not_exists()
.col(ColumnDef::new(Kb2Doc::KbId).big_integer())
.col(ColumnDef::new(Kb2Doc::Did).big_integer().comment("doc id, did in docinfo"))
.index(Index::create().col(Kb2Doc::KbId))
.col(ColumnDef::new(Kb2Doc::Did).big_integer())
.to_owned(),
)
.await?;
Expand All @@ -86,7 +85,6 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(ColumnDef::new(Dialog2Kb::DialogId).big_integer())
.col(ColumnDef::new(Dialog2Kb::KbId).big_integer())
.index(Index::create().col(Dialog2Kb::DialogId))
.to_owned(),
)
.await?;
Expand All @@ -96,9 +94,8 @@ impl MigrationTrait for Migration {
Table::create()
.table(Doc2Doc::Table)
.if_not_exists()
.col(ColumnDef::new(Doc2Doc::ParentId).big_integer().comment("doc id, did in docinfo"))
.col(ColumnDef::new(Doc2Doc::Did).big_integer().comment("doc id, did in docinfo"))
.index(Index::create().col(Doc2Doc::ParentId))
.col(ColumnDef::new(Doc2Doc::ParentId).big_integer())
.col(ColumnDef::new(Doc2Doc::Did).big_integer())
.to_owned(),
)
.await?;
Expand All @@ -108,15 +105,16 @@ impl MigrationTrait for Migration {
Table::create()
.table(KbInfo::Table)
.if_not_exists()
.col(ColumnDef::new(KbInfo::KbId).big_integer().auto_increment().not_null())
.col(ColumnDef::new(KbInfo::KbId).big_integer()
.auto_increment()
.not_null()
.primary_key())
.col(ColumnDef::new(KbInfo::Uid).big_integer().not_null())
.col(ColumnDef::new(KbInfo::KbName).string().not_null())
.col(ColumnDef::new(KbInfo::Icon).big_integer().default(1))
.col(ColumnDef::new(KbInfo::CreatedAt).date().not_null())
.col(ColumnDef::new(KbInfo::UpdatedAt).date().not_null())
.col(ColumnDef::new(KbInfo::IsDeleted).boolean().default(false))
.index(Index::create().col(KbInfo::KbId))
.index(Index::create().col(KbInfo::Uid))
.to_owned(),
)
.await?;
Expand All @@ -126,7 +124,10 @@ impl MigrationTrait for Migration {
Table::create()
.table(DocInfo::Table)
.if_not_exists()
.col(ColumnDef::new(DocInfo::Did).big_integer().auto_increment().not_null())
.col(ColumnDef::new(DocInfo::Did).big_integer()
.not_null()
.auto_increment()
.primary_key())
.col(ColumnDef::new(DocInfo::Uid).big_integer().not_null())
.col(ColumnDef::new(DocInfo::DocName).string().not_null())
.col(ColumnDef::new(DocInfo::Size).big_integer().not_null())
Expand All @@ -135,8 +136,6 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(DocInfo::CreatedAt).date().not_null())
.col(ColumnDef::new(DocInfo::UpdatedAt).date().not_null())
.col(ColumnDef::new(DocInfo::IsDeleted).boolean().default(false))
.index(Index::create().col(DocInfo::Did))
.index(Index::create().col(DocInfo::Uid))
.to_owned(),
)
.await?;
Expand All @@ -146,15 +145,17 @@ impl MigrationTrait for Migration {
Table::create()
.table(DialogInfo::Table)
.if_not_exists()
.col(ColumnDef::new(DialogInfo::DialogId).big_integer().auto_increment().not_null())
.col(ColumnDef::new(DialogInfo::DialogId)
.big_integer()
.not_null()
.auto_increment()
.primary_key())
.col(ColumnDef::new(DialogInfo::Uid).big_integer().not_null())
.col(ColumnDef::new(DialogInfo::DialogName).string().not_null())
.col(ColumnDef::new(DialogInfo::History).string().comment("json"))
.col(ColumnDef::new(DialogInfo::CreatedAt).date().not_null())
.col(ColumnDef::new(DialogInfo::UpdatedAt).date().not_null())
.col(ColumnDef::new(DialogInfo::IsDeleted).boolean().default(false))
.index(Index::create().col(DialogInfo::DialogId))
.index(Index::create().col(DialogInfo::Uid))
.to_owned(),
)
.await?;
Expand Down Expand Up @@ -221,6 +222,7 @@ enum UserInfo {
#[derive(DeriveIden)]
enum TagInfo {
Table,
Tid,
Uid,
TagName,
Regx,
Expand Down
10 changes: 10 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

pub(crate) mod tag;

#[derive(Debug, Deserialize, Serialize)]
struct JsonResponse<T> {
code: u32,
err: String,
data: T,
}
58 changes: 58 additions & 0 deletions src/api/tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::HashMap;
use actix_web::{get, HttpResponse, post, web};
use actix_web::http::Error;
use crate::api::JsonResponse;
use crate::AppState;
use crate::entity::tag_info;
use crate::service::tag_info::{Mutation, Query};

#[post("/v1.0/create_tag")]
async fn create(model: web::Json<tag_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
let model = Mutation::create_tag(&data.conn, model.into_inner()).await.unwrap();

let mut result = HashMap::new();
result.insert("tid", model.uid.unwrap());

let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: result,
};

Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response).unwrap()))
}

#[post("/v1.0/delete_tag")]
async fn delete(model: web::Json<tag_info::Model>, data: web::Data<AppState>) -> Result<HttpResponse, Error> {
let _ = Mutation::delete_tag(&data.conn, model.tid).await.unwrap();

let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: (),
};

Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response).unwrap()))
}

#[get("/v1.0/tags")]
async fn list(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
let tags = Query::find_tag_infos(&data.conn).await.unwrap();

let mut result = HashMap::new();
result.insert("tags", tags);

let json_response = JsonResponse {
code: 200,
err: "".to_owned(),
data: result,
};

Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&json_response).unwrap()))
}
3 changes: 2 additions & 1 deletion src/entity/dialog_2_kb.rs → src/entity/dialog2_kb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "dialog_2_kb")]
#[sea_orm(table_name = "dialog2_kb")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[sea_orm(index)]
pub dialog_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub kb_id: i64,
Expand Down
12 changes: 5 additions & 7 deletions src/entity/dialog_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub dialog_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
#[sea_orm(index)]
pub uid: i64,
pub dialog_name: String,
pub history: String,

pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(soft_delete_column)]
pub is_deleted: bool,
pub created_at: Date,
pub updated_at: Date,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl Related<super::kb_info::Entity> for Entity {
fn to() -> RelationDef {
super::dialog_2_kb::Relation::KbInfo.def()
super::dialog2_kb::Relation::KbInfo.def()
}

fn via() -> Option<RelationDef> {
Some(super::dialog_2_kb::Relation::DialogInfo.def().rev())
Some(super::dialog2_kb::Relation::DialogInfo.def().rev())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/entity/doc_2_doc.rs → src/entity/doc2_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "doc_2_doc")]
#[sea_orm(table_name = "doc2_doc")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[sea_orm(index)]
pub parent_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub did: i64,
Expand Down
18 changes: 9 additions & 9 deletions src/entity/doc_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub did: i64,
#[sea_orm(primary_key, auto_increment = false)]
#[sea_orm(index)]
pub uid: i64,
pub doc_name: String,
pub size: i64,
#[sea_orm(column_name = "type")]
pub r#type: String,
pub kb_progress: f64,

pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
pub created_at: Date,
pub updated_at: Date,
#[sea_orm(soft_delete_column)]
pub is_deleted: bool,
}
Expand All @@ -25,31 +25,31 @@ pub enum Relation {}

impl Related<super::tag_info::Entity> for Entity {
fn to() -> RelationDef {
super::tag_2_doc::Relation::Tag.def()
super::tag2_doc::Relation::Tag.def()
}

fn via() -> Option<RelationDef> {
Some(super::tag_2_doc::Relation::DocInfo.def().rev())
Some(super::tag2_doc::Relation::DocInfo.def().rev())
}
}

impl Related<super::kb_info::Entity> for Entity {
fn to() -> RelationDef {
super::kb_2_doc::Relation::KbInfo.def()
super::kb2_doc::Relation::KbInfo.def()
}

fn via() -> Option<RelationDef> {
Some(super::kb_2_doc::Relation::DocInfo.def().rev())
Some(super::kb2_doc::Relation::DocInfo.def().rev())
}
}

impl Related<Entity> for Entity {
fn to() -> RelationDef {
super::doc_2_doc::Relation::Parent.def()
super::doc2_doc::Relation::Parent.def()
}

fn via() -> Option<RelationDef> {
Some(super::doc_2_doc::Relation::Child.def().rev())
Some(super::doc2_doc::Relation::Child.def().rev())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/entity/kb_2_doc.rs → src/entity/kb2_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(table_name = "kb_2_doc")]
#[sea_orm(table_name = "kb2_doc")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
#[sea_orm(index)]
pub kb_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub uid: i64,
Expand Down
Loading