impl kb_info & dialog_info & doc_info (#9)
* feat: impl kb_info & dialog_info * feat: impl doc_info apis
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
use sea_orm::{DbConn, DbErr, EntityTrait, PaginatorTrait, QueryOrder};
|
||||
use crate::entity::dialog_info;
|
||||
use chrono::Local;
|
||||
use sea_orm::{ActiveModelTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryOrder};
|
||||
use sea_orm::ActiveValue::Set;
|
||||
use sea_orm::QueryFilter;
|
||||
use sea_orm::ColumnTrait;
|
||||
use crate::entity::{dialog_info, kb_info};
|
||||
use crate::entity::dialog_info::Entity;
|
||||
|
||||
pub struct Query;
|
||||
@@ -9,6 +13,17 @@ impl Query {
|
||||
Entity::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_dialog_infos(db: &DbConn) -> Result<Vec<dialog_info::Model>, DbErr> {
|
||||
Entity::find().all(db).await
|
||||
}
|
||||
|
||||
pub async fn find_dialog_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<dialog_info::Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(dialog_info::Column::Uid.eq(uid))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_dialog_infos_in_page(
|
||||
db: &DbConn,
|
||||
page: u64,
|
||||
@@ -23,4 +38,61 @@ impl Query {
|
||||
// Fetch paginated posts
|
||||
paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mutation;
|
||||
|
||||
impl Mutation {
|
||||
pub async fn create_dialog_info(
|
||||
db: &DbConn,
|
||||
form_data: dialog_info::Model,
|
||||
) -> Result<dialog_info::ActiveModel, DbErr> {
|
||||
dialog_info::ActiveModel {
|
||||
dialog_id: Default::default(),
|
||||
uid: Set(form_data.uid.to_owned()),
|
||||
dialog_name: Set(form_data.dialog_name.to_owned()),
|
||||
history: Set(form_data.history.to_owned()),
|
||||
created_at: Set(Local::now().date_naive()),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_dialog_info_by_id(
|
||||
db: &DbConn,
|
||||
id: i64,
|
||||
form_data: dialog_info::Model,
|
||||
) -> Result<dialog_info::Model, DbErr> {
|
||||
let dialog_info: dialog_info::ActiveModel = Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
dialog_info::ActiveModel {
|
||||
dialog_id: dialog_info.dialog_id,
|
||||
uid: dialog_info.uid,
|
||||
dialog_name: Set(form_data.dialog_name.to_owned()),
|
||||
history: Set(form_data.history.to_owned()),
|
||||
created_at: Default::default(),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.update(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_dialog_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
|
||||
let tag: dialog_info::ActiveModel = Entity::find_by_id(kb_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
tag.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_all_dialog_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
||||
Entity::delete_many().exec(db).await
|
||||
}
|
||||
}
|
||||
154
src/service/doc_info.rs
Normal file
154
src/service/doc_info.rs
Normal file
@@ -0,0 +1,154 @@
|
||||
use chrono::Local;
|
||||
use postgres::fallible_iterator::FallibleIterator;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryOrder};
|
||||
use sea_orm::ActiveValue::Set;
|
||||
use sea_orm::QueryFilter;
|
||||
use crate::api::doc_info::{FilterParams, Params};
|
||||
use crate::entity::{doc2_doc, doc_info, kb_info, tag_info};
|
||||
use crate::entity::doc_info::Entity;
|
||||
|
||||
pub struct Query;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_doc_info_by_id(db: &DbConn, id: i64) -> Result<Option<doc_info::Model>, DbErr> {
|
||||
Entity::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_doc_infos(db: &DbConn) -> Result<Vec<doc_info::Model>, DbErr> {
|
||||
Entity::find().all(db).await
|
||||
}
|
||||
|
||||
pub async fn find_doc_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<doc_info::Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(doc_info::Column::Uid.eq(uid))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_doc_infos_by_params(db: &DbConn, params: Params) -> Result<Vec<doc_info::Model>, DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find();
|
||||
|
||||
// Fetch paginated posts
|
||||
let mut query = paginator
|
||||
.find_with_related(kb_info::Entity);
|
||||
if let Some(kb_id) = params.filter.kb_id {
|
||||
query = query.filter(kb_info::Column::KbId.eq(kb_id));
|
||||
}
|
||||
if let Some(folder_id) = params.filter.folder_id {
|
||||
|
||||
}
|
||||
if let Some(tag_id) = params.filter.tag_id {
|
||||
query = query.filter(tag_info::Column::Tid.eq(tag_id));
|
||||
}
|
||||
if let Some(keywords) = params.filter.keywords {
|
||||
|
||||
}
|
||||
Ok(query.order_by_asc(doc_info::Column::Did)
|
||||
.all(db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|(mut doc_info, kb_infos)| {
|
||||
doc_info.kb_infos = kb_infos;
|
||||
doc_info
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn find_doc_infos_in_page(
|
||||
db: &DbConn,
|
||||
page: u64,
|
||||
posts_per_page: u64,
|
||||
) -> Result<(Vec<doc_info::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find()
|
||||
.order_by_asc(doc_info::Column::Did)
|
||||
.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 mv_doc_info(
|
||||
db: &DbConn,
|
||||
dest_did: i64,
|
||||
dids: &[i64]
|
||||
) -> Result<(), DbErr> {
|
||||
for did in dids {
|
||||
let _ = doc2_doc::ActiveModel {
|
||||
parent_id: Set(dest_did),
|
||||
did: Set(*did),
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_doc_info(
|
||||
db: &DbConn,
|
||||
form_data: doc_info::Model,
|
||||
) -> Result<doc_info::ActiveModel, DbErr> {
|
||||
doc_info::ActiveModel {
|
||||
did: Default::default(),
|
||||
uid: Set(form_data.uid.to_owned()),
|
||||
doc_name: Set(form_data.doc_name.to_owned()),
|
||||
size: Set(form_data.size.to_owned()),
|
||||
r#type: Set(form_data.r#type.to_owned()),
|
||||
kb_progress: Set(form_data.kb_progress.to_owned()),
|
||||
location: Set(form_data.location.to_owned()),
|
||||
created_at: Set(Local::now().date_naive()),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_doc_info_by_id(
|
||||
db: &DbConn,
|
||||
id: i64,
|
||||
form_data: doc_info::Model,
|
||||
) -> Result<doc_info::Model, DbErr> {
|
||||
let doc_info: doc_info::ActiveModel = Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
doc_info::ActiveModel {
|
||||
did: doc_info.did,
|
||||
uid: Set(form_data.uid.to_owned()),
|
||||
doc_name: Set(form_data.doc_name.to_owned()),
|
||||
size: Set(form_data.size.to_owned()),
|
||||
r#type: Set(form_data.r#type.to_owned()),
|
||||
kb_progress: Set(form_data.kb_progress.to_owned()),
|
||||
location: Set(form_data.location.to_owned()),
|
||||
created_at: Default::default(),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.update(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_doc_info(db: &DbConn, doc_id: i64) -> Result<DeleteResult, DbErr> {
|
||||
let tag: doc_info::ActiveModel = Entity::find_by_id(doc_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
tag.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_all_doc_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
||||
Entity::delete_many().exec(db).await
|
||||
}
|
||||
}
|
||||
96
src/service/kb_info.rs
Normal file
96
src/service/kb_info.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use chrono::{Local, NaiveDate};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, DbConn, DbErr, DeleteResult, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder};
|
||||
use sea_orm::ActiveValue::Set;
|
||||
use crate::entity::kb_info;
|
||||
use crate::entity::kb_info::Entity;
|
||||
|
||||
pub struct Query;
|
||||
|
||||
impl Query {
|
||||
pub async fn find_kb_info_by_id(db: &DbConn, id: i64) -> Result<Option<kb_info::Model>, DbErr> {
|
||||
Entity::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_kb_infos(db: &DbConn) -> Result<Vec<kb_info::Model>, DbErr> {
|
||||
Entity::find().all(db).await
|
||||
}
|
||||
|
||||
pub async fn find_kb_infos_by_uid(db: &DbConn, uid: i64) -> Result<Vec<kb_info::Model>, DbErr> {
|
||||
Entity::find()
|
||||
.filter(kb_info::Column::Uid.eq(uid))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn find_kb_infos_in_page(
|
||||
db: &DbConn,
|
||||
page: u64,
|
||||
posts_per_page: u64,
|
||||
) -> Result<(Vec<kb_info::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find()
|
||||
.order_by_asc(kb_info::Column::KbId)
|
||||
.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_kb_info(
|
||||
db: &DbConn,
|
||||
form_data: kb_info::Model,
|
||||
) -> Result<kb_info::ActiveModel, DbErr> {
|
||||
kb_info::ActiveModel {
|
||||
kb_id: Default::default(),
|
||||
uid: Set(form_data.uid.to_owned()),
|
||||
kn_name: Set(form_data.kn_name.to_owned()),
|
||||
icon: Set(form_data.icon.to_owned()),
|
||||
created_at: Set(Local::now().date_naive()),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_kb_info_by_id(
|
||||
db: &DbConn,
|
||||
id: i64,
|
||||
form_data: kb_info::Model,
|
||||
) -> Result<kb_info::Model, DbErr> {
|
||||
let kb_info: kb_info::ActiveModel = Entity::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
kb_info::ActiveModel {
|
||||
kb_id: kb_info.kb_id,
|
||||
uid: kb_info.uid,
|
||||
kn_name: Set(form_data.kn_name.to_owned()),
|
||||
icon: Set(form_data.icon.to_owned()),
|
||||
created_at: Default::default(),
|
||||
updated_at: Set(Local::now().date_naive()),
|
||||
}
|
||||
.update(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_kb_info(db: &DbConn, kb_id: i64) -> Result<DeleteResult, DbErr> {
|
||||
let tag: kb_info::ActiveModel = Entity::find_by_id(kb_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
tag.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_all_kb_infos(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
||||
Entity::delete_many().exec(db).await
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
pub(crate) mod dialog_info;
|
||||
pub(crate) mod tag_info;
|
||||
pub(crate) mod tag_info;
|
||||
pub(crate) mod kb_info;
|
||||
pub(crate) mod doc_info;
|
||||
@@ -22,7 +22,7 @@ impl Query {
|
||||
) -> Result<(Vec<tag_info::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Entity::find()
|
||||
.order_by_asc(tag_info::Column::Uid)
|
||||
.order_by_asc(tag_info::Column::Tid)
|
||||
.paginate(db, posts_per_page);
|
||||
let num_pages = paginator.num_pages().await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user