fix: dao bugs
This commit is contained in:
parent
e46a834bef
commit
36574b6d68
19 changed files with 149 additions and 77 deletions
3
.env
Normal file
3
.env
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
HOST=127.0.0.1
|
||||
PORT=8000
|
||||
DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt"
|
||||
3
.env.template
Normal file
3
.env.template
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
HOST=127.0.0.1
|
||||
PORT=8000
|
||||
DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt"
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -14,3 +14,4 @@ Cargo.lock
|
|||
*.pdb
|
||||
|
||||
.idea/
|
||||
.env
|
||||
|
|
|
|||
|
|
@ -8,9 +8,14 @@ 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"] }
|
||||
tracing-subscriber = "0.3.18"
|
||||
dotenvy = "0.15.7"
|
||||
listenfd = "1.0.1"
|
||||
migration = { path = "./migration" }
|
||||
|
||||
[[bin]]
|
||||
name = "doc_gpt"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# Running Migrator CLI
|
||||
# Running Migrator CLI
|
||||
|
||||
- Generate a new migration file
|
||||
```sh
|
||||
|
|
|
|||
|
|
@ -61,8 +61,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?;
|
||||
|
|
@ -73,8 +72,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?;
|
||||
|
|
@ -86,7 +84,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?;
|
||||
|
|
@ -96,9 +93,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?;
|
||||
|
|
@ -108,15 +104,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?;
|
||||
|
|
@ -126,7 +123,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())
|
||||
|
|
@ -135,8 +135,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?;
|
||||
|
|
@ -146,15 +144,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?;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -6,15 +6,13 @@ 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)]
|
||||
|
|
@ -22,11 +20,11 @@ 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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -6,7 +6,7 @@ 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,
|
||||
|
|
@ -14,8 +14,8 @@ pub struct Model {
|
|||
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,
|
||||
}
|
||||
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -6,15 +6,13 @@ use serde::{Deserialize, Serialize};
|
|||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub kb_id: i64,
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
#[sea_orm(index)]
|
||||
pub uid: i64,
|
||||
pub kn_name: String,
|
||||
pub icon: i64,
|
||||
|
||||
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)]
|
||||
|
|
@ -22,21 +20,21 @@ pub enum Relation {}
|
|||
|
||||
impl Related<super::doc_info::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::kb_2_doc::Relation::DocInfo.def()
|
||||
super::kb2_doc::Relation::DocInfo.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(super::kb_2_doc::Relation::KbInfo.def().rev())
|
||||
Some(super::kb2_doc::Relation::KbInfo.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::dialog_info::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::dialog_2_kb::Relation::DialogInfo.def()
|
||||
super::dialog2_kb::Relation::DialogInfo.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(super::dialog_2_kb::Relation::KbInfo.def().rev())
|
||||
Some(super::dialog2_kb::Relation::KbInfo.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
mod user_info;
|
||||
mod tag_info;
|
||||
mod tag_2_doc;
|
||||
mod kb_2_doc;
|
||||
mod dialog_2_kb;
|
||||
mod doc_2_doc;
|
||||
mod kb_info;
|
||||
mod doc_info;
|
||||
mod dialog_info;
|
||||
pub(crate) mod user_info;
|
||||
pub(crate) mod tag_info;
|
||||
mod tag2_doc;
|
||||
mod kb2_doc;
|
||||
mod dialog2_kb;
|
||||
mod doc2_doc;
|
||||
pub(crate) mod kb_info;
|
||||
pub(crate) mod doc_info;
|
||||
pub(crate) mod dialog_info;
|
||||
|
|
@ -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 = "tag_2_doc")]
|
||||
#[sea_orm(table_name = "tag2_doc")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
#[sea_orm(index)]
|
||||
pub tag_id: i64,
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub uid: i64,
|
||||
|
|
@ -13,10 +13,8 @@ pub struct Model {
|
|||
pub icon: i64,
|
||||
pub dir: 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)]
|
||||
|
|
@ -24,11 +22,11 @@ pub enum Relation {}
|
|||
|
||||
impl Related<super::doc_info::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::tag_2_doc::Relation::DocInfo.def()
|
||||
super::tag2_doc::Relation::DocInfo.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(super::tag_2_doc::Relation::Tag.def().rev())
|
||||
Some(super::tag2_doc::Relation::Tag.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,6 @@ pub struct Model {
|
|||
|
||||
pub created_at: DateTimeWithTimeZone,
|
||||
pub updated_at: DateTimeWithTimeZone,
|
||||
#[sea_orm(soft_delete_column)]
|
||||
pub is_deleted: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
|
|
|
|||
63
src/main.rs
63
src/main.rs
|
|
@ -2,22 +2,59 @@ mod api;
|
|||
mod entity;
|
||||
mod service;
|
||||
|
||||
use actix_web::{get, web, App, HttpServer, Responder};
|
||||
use std::env;
|
||||
use actix_files::Files;
|
||||
use actix_web::{web, App, HttpServer, Responder, middleware};
|
||||
use listenfd::ListenFd;
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
"Hello, World!"
|
||||
}
|
||||
|
||||
#[get("/{name}")]
|
||||
async fn hello(name: web::Path<String>) -> impl Responder {
|
||||
format!("Hello {}!", &name)
|
||||
#[derive(Debug, Clone)]
|
||||
struct AppState {
|
||||
conn: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index).service(hello))
|
||||
.bind(("127.0.0.1", 9090))?
|
||||
.run()
|
||||
.await
|
||||
std::env::set_var("RUST_LOG", "debug");
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// get env vars
|
||||
dotenvy::dotenv().ok();
|
||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
|
||||
let host = env::var("HOST").expect("HOST is not set in .env file");
|
||||
let port = env::var("PORT").expect("PORT is not set in .env file");
|
||||
let server_url = format!("{host}:{port}");
|
||||
|
||||
// establish connection to database and apply migrations
|
||||
// -> create post table if not exists
|
||||
let conn = Database::connect(&db_url).await.unwrap();
|
||||
Migrator::up(&conn, None).await.unwrap();
|
||||
|
||||
let state = AppState { conn };
|
||||
|
||||
// create server and try to serve over socket if possible
|
||||
let mut listenfd = ListenFd::from_env();
|
||||
let mut server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.service(Files::new("/static", "./static"))
|
||||
.app_data(web::Data::new(state.clone()))
|
||||
.wrap(middleware::Logger::default())
|
||||
.configure(init)
|
||||
});
|
||||
|
||||
server = match listenfd.take_tcp_listener(0)? {
|
||||
Some(listener) => server.listen(listener)?,
|
||||
None => server.bind(&server_url)?,
|
||||
};
|
||||
|
||||
println!("Starting server at {server_url}");
|
||||
server.run().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init(cfg: &mut web::ServiceConfig) {
|
||||
// cfg.service(index);
|
||||
// cfg.service(hello);
|
||||
}
|
||||
26
src/service/dialog_info.rs
Normal file
26
src/service/dialog_info.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use sea_orm::{DbConn, DbErr, EntityTrait, PaginatorTrait, QueryOrder};
|
||||
use crate::entity::dialog_info;
|
||||
use crate::entity::dialog_info::Entity;
|
||||
|
||||
pub struct Query;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_dialog_info_by_id(db: &DbConn, id: i64) -> Result<Option<dialog_info::Model>, DbErr> {
|
||||
Entity::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_dialog_infos_in_page(
|
||||
db: &DbConn,
|
||||
page: u64,
|
||||
posts_per_page: u64,
|
||||
) -> Result<(Vec<dialog_info::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find()
|
||||
.order_by_asc(dialog_info::Column::DialogId)
|
||||
.paginate(db, posts_per_page);
|
||||
let num_pages = paginator.num_pages().await?;
|
||||
|
||||
// Fetch paginated posts
|
||||
paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
pub(crate) mod dialog_info;
|
||||
Loading…
Add table
Reference in a new issue