2021-07-23 06:37:18 +00:00
|
|
|
use crate::{
|
2021-07-24 06:05:49 +00:00
|
|
|
entities::doc::{CreateDocParams, DocData, DocInfo, QueryDocParams, UpdateDocParams},
|
2021-07-23 06:37:18 +00:00
|
|
|
errors::EditorError,
|
|
|
|
module::EditorDatabase,
|
|
|
|
sql_tables::doc::{DocTable, DocTableChangeset, DocTableSql},
|
|
|
|
};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct DocController {
|
|
|
|
sql: Arc<DocTableSql>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocController {
|
|
|
|
pub(crate) fn new(database: Arc<dyn EditorDatabase>) -> Self {
|
|
|
|
let sql = Arc::new(DocTableSql { database });
|
|
|
|
Self { sql }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn create_doc(
|
|
|
|
&self,
|
|
|
|
params: CreateDocParams,
|
|
|
|
path: &str,
|
2021-07-24 06:05:49 +00:00
|
|
|
) -> Result<DocInfo, EditorError> {
|
2021-07-23 06:37:18 +00:00
|
|
|
let doc_table = DocTable::new(params, path);
|
2021-07-24 06:05:49 +00:00
|
|
|
let doc: DocInfo = doc_table.clone().into();
|
2021-07-23 06:37:18 +00:00
|
|
|
let _ = self.sql.create_doc_table(doc_table)?;
|
|
|
|
|
|
|
|
Ok(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn update_doc(&self, params: UpdateDocParams) -> Result<(), EditorError> {
|
|
|
|
let changeset = DocTableChangeset::new(params);
|
|
|
|
let _ = self.sql.update_doc_table(changeset)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-24 06:05:49 +00:00
|
|
|
pub(crate) async fn read_doc(&self, doc_id: &str) -> Result<DocInfo, EditorError> {
|
2021-07-23 06:37:18 +00:00
|
|
|
let doc_table = self.sql.read_doc_table(doc_id)?;
|
2021-07-24 06:05:49 +00:00
|
|
|
let doc_desc: DocInfo = doc_table.into();
|
2021-07-23 06:37:18 +00:00
|
|
|
Ok(doc_desc)
|
|
|
|
}
|
|
|
|
}
|