mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-04-19 12:39:59 +08:00
impl tags api (#2)
This commit is contained in:
parent
c6d0d85ab5
commit
f102c608bb
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,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"
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Running Migrator CLI
|
||||
# Running Migrator CLI
|
||||
|
||||
- Generate a new migration file
|
||||
```sh
|
||||
|
@ -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))
|
||||
@ -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?;
|
||||
@ -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?;
|
||||
@ -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?;
|
||||
@ -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?;
|
||||
@ -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?;
|
||||
@ -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())
|
||||
@ -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?;
|
||||
@ -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?;
|
||||
@ -221,6 +222,7 @@ enum UserInfo {
|
||||
#[derive(DeriveIden)]
|
||||
enum TagInfo {
|
||||
Table,
|
||||
Tid,
|
||||
Uid,
|
||||
TagName,
|
||||
Regx,
|
||||
|
@ -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
src/api/tag.rs
Normal file
58
src/api/tag.rs
Normal file
@ -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()))
|
||||
}
|
@ -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,
|
||||
@ -25,7 +26,7 @@ impl RelationTrait for Relation {
|
||||
.into(),
|
||||
Self::Tag => Entity::belongs_to(super::tag_info::Entity)
|
||||
.from(Column::TagId)
|
||||
.to(super::tag_info::Column::Uid)
|
||||
.to(super::tag_info::Column::Tid)
|
||||
.into(),
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub tid: i64,
|
||||
pub uid: i64,
|
||||
pub tag_name: String,
|
||||
pub regx: String,
|
||||
@ -13,10 +14,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 +23,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)]
|
||||
|
64
src/main.rs
64
src/main.rs
@ -2,22 +2,60 @@ 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, 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(api::tag::create);
|
||||
cfg.service(api::tag::delete);
|
||||
cfg.service(api::tag::list);
|
||||
}
|
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,2 @@
|
||||
pub(crate) mod dialog_info;
|
||||
pub(crate) mod tag_info;
|
94
src/service/tag_info.rs
Normal file
94
src/service/tag_info.rs
Normal file
@ -0,0 +1,94 @@
|
||||
use sea_orm::{ActiveModelTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryOrder};
|
||||
use sea_orm::ActiveValue::Set;
|
||||
use crate::entity::tag_info;
|
||||
use crate::entity::tag_info::Entity;
|
||||
|
||||
pub struct Query;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_tag_info_by_id(db: &DbConn, id: i64) -> Result<Option<tag_info::Model>, DbErr> {
|
||||
Entity::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_tag_infos(db: &DbConn) -> Result<Vec<tag_info::Model>, DbErr> {
|
||||
Entity::find().all(db).await
|
||||
}
|
||||
|
||||
pub async fn find_tag_infos_in_page(
|
||||
db: &DbConn,
|
||||
page: u64,
|
||||
posts_per_page: u64,
|
||||
) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find()
|
||||
.order_by_asc(tag_info::Column::Uid)
|
||||
.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))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mutation;
|
||||
|
||||
impl Mutation {
|
||||
pub async fn create_tag(
|
||||
db: &DbConn,
|
||||
form_data: tag_info::Model,
|
||||
) -> Result<tag_info::ActiveModel, DbErr> {
|
||||
tag_info::ActiveModel {
|
||||
tid: Set(form_data.tid.to_owned()),
|
||||
uid: Set(form_data.uid.to_owned()),
|
||||
tag_name: Set(form_data.tag_name.to_owned()),
|
||||
regx: Set(form_data.regx.to_owned()),
|
||||
color: Set(form_data.color.to_owned()),
|
||||
icon: Set(form_data.icon.to_owned()),
|
||||
dir: Set(form_data.dir.to_owned()),
|
||||
created_at: Default::default(),
|
||||
updated_at: Default::default(),
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_tag_by_id(
|
||||
db: &DbConn,
|
||||
id: i64,
|
||||
form_data: tag_info::Model,
|
||||
) -> Result<tag_info::Model, DbErr> {
|
||||
let tag: tag_info::ActiveModel = Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find post.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
tag_info::ActiveModel {
|
||||
tid: tag.tid,
|
||||
uid: tag.uid,
|
||||
tag_name: Set(form_data.tag_name.to_owned()),
|
||||
regx: Set(form_data.regx.to_owned()),
|
||||
color: Set(form_data.color.to_owned()),
|
||||
icon: Set(form_data.icon.to_owned()),
|
||||
dir: Set(form_data.dir.to_owned()),
|
||||
created_at: Default::default(),
|
||||
updated_at: Default::default(),
|
||||
}
|
||||
.update(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_tag(db: &DbConn, tid: i64) -> Result<DeleteResult, DbErr> {
|
||||
let tag: tag_info::ActiveModel = Entity::find_by_id(tid)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find tag.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
tag.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_all_tags(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
||||
Entity::delete_many().exec(db).await
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user