56 lines
1.7 KiB
Rust
Raw Normal View History

2021-07-23 08:14:45 +08:00
use crate::{
2021-07-31 10:50:56 +08:00
errors::DocError,
2021-09-13 23:09:57 +08:00
services::{doc_controller::DocController, doc_manager::DocManager, server::construct_doc_server},
2021-07-23 08:14:45 +08:00
};
2021-09-13 23:09:57 +08:00
use crate::entities::doc::{CreateDocParams, Doc, QueryDocParams, UpdateDocParams};
use diesel::SqliteConnection;
use flowy_database::ConnectionPool;
2021-07-23 15:00:13 +08:00
use std::sync::Arc;
use tokio::sync::RwLock;
2021-07-22 22:26:38 +08:00
2021-07-31 10:50:56 +08:00
pub trait DocumentUser: Send + Sync {
fn user_doc_dir(&self) -> Result<String, DocError>;
fn user_id(&self) -> Result<String, DocError>;
fn token(&self) -> Result<String, DocError>;
}
2021-09-11 14:26:30 +08:00
pub enum DocumentType {
Doc,
}
2021-09-13 23:09:57 +08:00
pub struct FlowyDocument {
controller: Arc<DocController>,
manager: Arc<DocManager>,
2021-09-11 14:26:30 +08:00
}
2021-07-22 22:26:38 +08:00
2021-09-13 23:09:57 +08:00
impl FlowyDocument {
pub fn new(user: Arc<dyn DocumentUser>) -> FlowyDocument {
2021-09-11 14:26:30 +08:00
let server = construct_doc_server();
2021-09-13 23:09:57 +08:00
let manager = Arc::new(DocManager::new());
let controller = Arc::new(DocController::new(server.clone(), user.clone()));
Self { controller, manager }
}
pub fn create(&self, params: CreateDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
let _ = self.controller.create(params, conn)?;
Ok(())
}
pub fn delete(&self, params: QueryDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
let _ = self.controller.delete(params.into(), conn)?;
Ok(())
}
pub async fn open(&self, params: QueryDocParams, pool: Arc<ConnectionPool>) -> Result<Doc, DocError> {
let doc = self.controller.open(params, pool).await?;
Ok(doc)
}
pub fn update(&self, params: UpdateDocParams, conn: &SqliteConnection) -> Result<(), DocError> {
let _ = self.controller.update(params, conn)?;
Ok(())
2021-09-11 14:26:30 +08:00
}
2021-07-22 22:26:38 +08:00
}