2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-09-09 07:43:05 +00:00
|
|
|
entities::doc::{CreateDocParams, Doc, QueryDocParams, UpdateDocParams},
|
2021-07-31 02:50:56 +00:00
|
|
|
errors::DocError,
|
2021-09-09 07:43:05 +00:00
|
|
|
module::{DocumentDatabase, DocumentUser},
|
|
|
|
services::server::Server,
|
2021-07-23 06:37:18 +00:00
|
|
|
sql_tables::doc::{DocTable, DocTableChangeset, DocTableSql},
|
|
|
|
};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct DocController {
|
2021-09-09 07:43:05 +00:00
|
|
|
server: Server,
|
2021-07-23 06:37:18 +00:00
|
|
|
sql: Arc<DocTableSql>,
|
2021-09-09 07:43:05 +00:00
|
|
|
user: Arc<dyn DocumentUser>,
|
|
|
|
database: Arc<dyn DocumentDatabase>,
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocController {
|
2021-09-09 07:43:05 +00:00
|
|
|
pub(crate) fn new(database: Arc<dyn DocumentDatabase>, server: Server, user: Arc<dyn DocumentUser>) -> Self {
|
|
|
|
let sql = Arc::new(DocTableSql {});
|
|
|
|
Self {
|
|
|
|
sql,
|
|
|
|
server,
|
|
|
|
user,
|
|
|
|
database,
|
|
|
|
}
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 07:43:05 +00:00
|
|
|
pub(crate) async fn create_doc(&self, params: CreateDocParams) -> Result<Doc, DocError> {
|
|
|
|
let doc = self.create_doc_on_server(params).await?;
|
|
|
|
let doc_table = DocTable::new(doc.clone());
|
|
|
|
|
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let _ = self.sql.create_doc_table(doc_table, &*conn)?;
|
2021-07-23 06:37:18 +00:00
|
|
|
|
|
|
|
Ok(doc)
|
|
|
|
}
|
|
|
|
|
2021-07-31 02:50:56 +00:00
|
|
|
pub(crate) async fn update_doc(&self, params: UpdateDocParams) -> Result<(), DocError> {
|
2021-09-09 07:43:05 +00:00
|
|
|
let changeset = DocTableChangeset::new(params.clone());
|
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let _ = self.sql.update_doc_table(changeset, &*conn)?;
|
|
|
|
let _ = self.update_doc_on_server(params)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn read_doc(&self, params: QueryDocParams) -> Result<Doc, DocError> {
|
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let doc: Doc = self.sql.read_doc_table(¶ms.doc_id, &*conn)?.into();
|
|
|
|
|
|
|
|
let _ = self.read_doc_on_server(params)?;
|
|
|
|
Ok(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn delete_doc(&self, params: QueryDocParams) -> Result<(), DocError> {
|
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let _ = self.sql.delete_doc(¶ms.doc_id, &*conn)?;
|
|
|
|
|
|
|
|
let _ = self.delete_doc_on_server(params)?;
|
2021-07-23 06:37:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-09 07:43:05 +00:00
|
|
|
}
|
2021-07-23 06:37:18 +00:00
|
|
|
|
2021-09-09 07:43:05 +00:00
|
|
|
impl DocController {
|
2021-09-09 09:34:01 +00:00
|
|
|
#[tracing::instrument(skip(self), err)]
|
2021-09-09 07:43:05 +00:00
|
|
|
async fn create_doc_on_server(&self, params: CreateDocParams) -> Result<Doc, DocError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let doc = self.server.create_doc(&token, params).await?;
|
|
|
|
Ok(doc)
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:34:01 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-09 07:43:05 +00:00
|
|
|
fn update_doc_on_server(&self, params: UpdateDocParams) -> Result<(), DocError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match server.update_doc(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Update doc failed: {}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:34:01 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-09 07:43:05 +00:00
|
|
|
fn read_doc_on_server(&self, params: QueryDocParams) -> Result<(), DocError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
// Opti: handle the error and retry?
|
2021-09-09 09:34:01 +00:00
|
|
|
let _doc = server.read_doc(&token, params).await?;
|
2021-09-09 07:43:05 +00:00
|
|
|
// save to disk
|
|
|
|
// notify
|
|
|
|
|
|
|
|
Result::<(), DocError>::Ok(())
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:34:01 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-09 07:43:05 +00:00
|
|
|
fn delete_doc_on_server(&self, params: QueryDocParams) -> Result<(), DocError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match server.delete_doc(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Delete doc failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
2021-07-23 06:37:18 +00:00
|
|
|
}
|
|
|
|
}
|