2021-07-14 13:12:52 +00:00
|
|
|
use crate::{
|
2021-09-11 12:09:46 +00:00
|
|
|
errors::{ErrorBuilder, ErrorCode, WorkspaceError},
|
2021-07-14 13:12:52 +00:00
|
|
|
event::WorkspaceEvent,
|
2021-09-01 14:50:22 +00:00
|
|
|
handlers::*,
|
2021-09-11 12:09:46 +00:00
|
|
|
services::{server::construct_workspace_server, AppController, ViewController, WorkspaceController},
|
2021-09-01 14:50:22 +00:00
|
|
|
};
|
2021-09-11 12:09:46 +00:00
|
|
|
use flowy_database::DBConnection;
|
|
|
|
use flowy_dispatch::prelude::*;
|
2021-09-13 15:09:57 +00:00
|
|
|
use flowy_document::module::FlowyDocument;
|
2021-09-11 12:09:46 +00:00
|
|
|
use flowy_sqlite::ConnectionPool;
|
2021-07-14 00:07:25 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-07-20 06:03:21 +00:00
|
|
|
pub trait WorkspaceDeps: WorkspaceUser + WorkspaceDatabase {}
|
|
|
|
|
2021-07-14 13:12:52 +00:00
|
|
|
pub trait WorkspaceUser: Send + Sync {
|
2021-07-26 05:51:41 +00:00
|
|
|
fn user_id(&self) -> Result<String, WorkspaceError>;
|
2021-09-01 14:50:22 +00:00
|
|
|
fn token(&self) -> Result<String, WorkspaceError>;
|
2021-07-20 06:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait WorkspaceDatabase: Send + Sync {
|
2021-09-11 12:09:46 +00:00
|
|
|
fn db_pool(&self) -> Result<Arc<ConnectionPool>, WorkspaceError>;
|
|
|
|
|
|
|
|
fn db_connection(&self) -> Result<DBConnection, WorkspaceError> {
|
|
|
|
let pool = self.db_pool()?;
|
|
|
|
let conn = pool
|
|
|
|
.get()
|
|
|
|
.map_err(|e| ErrorBuilder::new(ErrorCode::InternalError).error(e).build())?;
|
|
|
|
Ok(conn)
|
|
|
|
}
|
2021-07-14 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:09:57 +00:00
|
|
|
pub fn create(user: Arc<dyn WorkspaceUser>, database: Arc<dyn WorkspaceDatabase>, document: Arc<FlowyDocument>) -> Module {
|
2021-09-01 14:50:22 +00:00
|
|
|
let server = construct_workspace_server();
|
2021-09-11 06:26:30 +00:00
|
|
|
let view_controller = Arc::new(ViewController::new(user.clone(), database.clone(), server.clone(), document));
|
2021-09-07 13:31:04 +00:00
|
|
|
let app_controller = Arc::new(AppController::new(user.clone(), database.clone(), server.clone()));
|
2021-07-20 06:03:21 +00:00
|
|
|
|
|
|
|
let workspace_controller = Arc::new(WorkspaceController::new(
|
|
|
|
user.clone(),
|
|
|
|
database.clone(),
|
|
|
|
app_controller.clone(),
|
2021-09-11 12:09:46 +00:00
|
|
|
view_controller.clone(),
|
2021-09-01 14:50:22 +00:00
|
|
|
server.clone(),
|
2021-07-20 06:03:21 +00:00
|
|
|
));
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-07-29 09:27:59 +00:00
|
|
|
let mut module = Module::new()
|
2021-07-14 00:07:25 +00:00
|
|
|
.name("Flowy-Workspace")
|
2021-07-14 13:12:52 +00:00
|
|
|
.data(workspace_controller)
|
|
|
|
.data(app_controller)
|
2021-07-29 09:27:59 +00:00
|
|
|
.data(view_controller);
|
|
|
|
|
|
|
|
module = module
|
2021-09-05 05:50:23 +00:00
|
|
|
.event(WorkspaceEvent::CreateWorkspace, create_workspace_handler)
|
|
|
|
.event(WorkspaceEvent::ReadCurWorkspace, read_cur_workspace_handler)
|
|
|
|
.event(WorkspaceEvent::ReadWorkspaces, read_workspaces_handler)
|
2021-09-07 09:12:03 +00:00
|
|
|
.event(WorkspaceEvent::OpenWorkspace, open_workspace_handler)
|
|
|
|
.event(WorkspaceEvent::ReadWorkspaceApps, read_workspace_apps_handler);
|
2021-07-29 09:27:59 +00:00
|
|
|
|
|
|
|
module = module
|
2021-09-05 05:50:23 +00:00
|
|
|
.event(WorkspaceEvent::CreateApp, create_app_handler)
|
|
|
|
.event(WorkspaceEvent::ReadApp, read_app_handler)
|
|
|
|
.event(WorkspaceEvent::UpdateApp, update_app_handler)
|
|
|
|
.event(WorkspaceEvent::DeleteApp, delete_app_handler);
|
2021-07-29 09:27:59 +00:00
|
|
|
|
|
|
|
module = module
|
2021-09-05 05:50:23 +00:00
|
|
|
.event(WorkspaceEvent::CreateView, create_view_handler)
|
|
|
|
.event(WorkspaceEvent::ReadView, read_view_handler)
|
|
|
|
.event(WorkspaceEvent::UpdateView, update_view_handler)
|
2021-09-11 06:26:30 +00:00
|
|
|
.event(WorkspaceEvent::DeleteView, delete_view_handler)
|
|
|
|
.event(WorkspaceEvent::OpenView, open_view_handler)
|
2021-09-14 08:22:44 +00:00
|
|
|
.event(WorkspaceEvent::SaveViewData, update_view_data_handler)
|
|
|
|
.event(WorkspaceEvent::ApplyChangeset, apply_changeset_handler);
|
2021-07-29 09:27:59 +00:00
|
|
|
|
|
|
|
module
|
2021-07-14 00:07:25 +00:00
|
|
|
}
|