2021-07-20 14:03:21 +08:00
|
|
|
use crate::{
|
|
|
|
|
entities::{app::App, workspace::*},
|
|
|
|
|
errors::*,
|
|
|
|
|
module::{WorkspaceDatabase, WorkspaceUser},
|
2021-07-21 22:41:44 +08:00
|
|
|
observable::{send_observable, WorkspaceObservable},
|
2021-07-20 14:03:21 +08:00
|
|
|
services::AppController,
|
|
|
|
|
sql_tables::workspace::{WorkspaceSql, WorkspaceTable, WorkspaceTableChangeset},
|
|
|
|
|
};
|
2021-07-19 16:15:20 +08:00
|
|
|
use flowy_dispatch::prelude::DispatchFuture;
|
2021-08-24 21:38:53 +08:00
|
|
|
use flowy_net::request::HttpRequestBuilder;
|
2021-07-13 23:08:20 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
pub struct WorkspaceController {
|
2021-07-18 23:56:36 +08:00
|
|
|
pub user: Arc<dyn WorkspaceUser>,
|
2021-07-20 14:03:21 +08:00
|
|
|
pub sql: Arc<WorkspaceSql>,
|
|
|
|
|
pub app_controller: Arc<AppController>,
|
2021-07-13 23:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WorkspaceController {
|
2021-07-20 14:03:21 +08:00
|
|
|
pub fn new(
|
|
|
|
|
user: Arc<dyn WorkspaceUser>,
|
|
|
|
|
database: Arc<dyn WorkspaceDatabase>,
|
|
|
|
|
app_controller: Arc<AppController>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let sql = Arc::new(WorkspaceSql { database });
|
|
|
|
|
Self {
|
|
|
|
|
user,
|
|
|
|
|
sql,
|
|
|
|
|
app_controller,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-13 23:08:20 +08:00
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
pub async fn create_workspace(
|
2021-07-14 08:07:25 +08:00
|
|
|
&self,
|
2021-08-24 21:38:53 +08:00
|
|
|
mut params: CreateWorkspaceParams,
|
2021-07-19 22:44:37 +08:00
|
|
|
) -> Result<Workspace, WorkspaceError> {
|
2021-07-26 13:51:41 +08:00
|
|
|
let user_id = self.user.user_id()?;
|
2021-08-25 17:34:20 +08:00
|
|
|
params.user_id = user_id.clone();
|
|
|
|
|
|
|
|
|
|
// TODO: server
|
2021-08-24 21:38:53 +08:00
|
|
|
|
2021-07-26 13:51:41 +08:00
|
|
|
let workspace_table = WorkspaceTable::new(params, &user_id);
|
2021-07-29 22:22:35 +08:00
|
|
|
let workspace: Workspace = workspace_table.clone().into();
|
2021-07-23 14:37:18 +08:00
|
|
|
let _ = self.sql.create_workspace(workspace_table)?;
|
2021-07-29 22:22:35 +08:00
|
|
|
send_observable(&user_id, WorkspaceObservable::UserCreateWorkspace);
|
|
|
|
|
Ok(workspace)
|
2021-07-13 23:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-20 14:03:21 +08:00
|
|
|
pub fn update_workspace(&self, params: UpdateWorkspaceParams) -> Result<(), WorkspaceError> {
|
|
|
|
|
let changeset = WorkspaceTableChangeset::new(params);
|
2021-07-21 22:41:44 +08:00
|
|
|
let workspace_id = changeset.id.clone();
|
2021-07-20 14:03:21 +08:00
|
|
|
let _ = self.sql.update_workspace(changeset)?;
|
|
|
|
|
|
2021-07-29 22:22:35 +08:00
|
|
|
send_observable(&workspace_id, WorkspaceObservable::WorkspaceUpdated);
|
2021-07-20 14:03:21 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 22:22:35 +08:00
|
|
|
pub fn delete_workspace(&self, workspace_id: &str) -> Result<(), WorkspaceError> {
|
|
|
|
|
let user_id = self.user.user_id()?;
|
|
|
|
|
let _ = self.sql.delete_workspace(workspace_id)?;
|
|
|
|
|
send_observable(&user_id, WorkspaceObservable::UserDeleteWorkspace);
|
|
|
|
|
Ok(())
|
2021-07-20 14:03:21 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
pub async fn read_cur_workspace(&self) -> Result<Workspace, WorkspaceError> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let user_workspace = self.user.get_cur_workspace().await?;
|
2021-07-23 14:37:18 +08:00
|
|
|
let workspace = self.read_workspace(&user_workspace.workspace_id).await?;
|
2021-07-20 14:03:21 +08:00
|
|
|
Ok(workspace)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
pub async fn read_cur_apps(&self) -> Result<Vec<App>, WorkspaceError> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let user_workspace = self.user.get_cur_workspace().await?;
|
2021-07-23 14:37:18 +08:00
|
|
|
let apps = self.read_apps(&user_workspace.workspace_id).await?;
|
2021-07-20 14:03:21 +08:00
|
|
|
Ok(apps)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
pub async fn read_workspace(&self, workspace_id: &str) -> Result<Workspace, WorkspaceError> {
|
|
|
|
|
let workspace_table = self.read_workspace_table(workspace_id).await?;
|
2021-07-20 14:03:21 +08:00
|
|
|
Ok(workspace_table.into())
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 13:51:41 +08:00
|
|
|
pub async fn read_workspaces_belong_to_user(&self) -> Result<Vec<Workspace>, WorkspaceError> {
|
|
|
|
|
let user_id = self.user.user_id()?;
|
|
|
|
|
let workspace = self
|
|
|
|
|
.sql
|
|
|
|
|
.read_workspaces_belong_to_user(&user_id)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|workspace_table| workspace_table.into())
|
|
|
|
|
.collect::<Vec<Workspace>>();
|
|
|
|
|
|
|
|
|
|
Ok(workspace)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
pub async fn read_apps(&self, workspace_id: &str) -> Result<Vec<App>, WorkspaceError> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let apps = self
|
|
|
|
|
.sql
|
|
|
|
|
.read_apps_belong_to_workspace(workspace_id)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|app_table| app_table.into())
|
|
|
|
|
.collect::<Vec<App>>();
|
|
|
|
|
|
|
|
|
|
Ok(apps)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 14:37:18 +08:00
|
|
|
fn read_workspace_table(
|
2021-07-19 16:15:20 +08:00
|
|
|
&self,
|
|
|
|
|
workspace_id: &str,
|
2021-07-19 17:37:58 +08:00
|
|
|
) -> DispatchFuture<Result<WorkspaceTable, WorkspaceError>> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let sql = self.sql.clone();
|
2021-07-19 16:15:20 +08:00
|
|
|
let workspace_id = workspace_id.to_owned();
|
|
|
|
|
DispatchFuture {
|
|
|
|
|
fut: Box::pin(async move {
|
2021-07-20 14:03:21 +08:00
|
|
|
let workspace = sql.read_workspace(&workspace_id)?;
|
2021-07-19 16:15:20 +08:00
|
|
|
// TODO: fetch workspace from remote server
|
|
|
|
|
Ok(workspace)
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-13 23:08:20 +08:00
|
|
|
}
|
2021-08-24 21:38:53 +08:00
|
|
|
|
|
|
|
|
pub async fn create_workspace_request(
|
|
|
|
|
params: CreateWorkspaceParams,
|
|
|
|
|
url: &str,
|
|
|
|
|
) -> Result<Workspace, WorkspaceError> {
|
|
|
|
|
let workspace = HttpRequestBuilder::post(&url.to_owned())
|
|
|
|
|
.protobuf(params)?
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
2021-08-25 17:34:20 +08:00
|
|
|
.response()
|
|
|
|
|
.await?;
|
2021-08-24 21:38:53 +08:00
|
|
|
Ok(workspace)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn read_workspace_request(
|
|
|
|
|
params: QueryWorkspaceParams,
|
|
|
|
|
url: &str,
|
2021-08-25 17:34:20 +08:00
|
|
|
) -> Result<Option<Workspace>, WorkspaceError> {
|
|
|
|
|
let result = HttpRequestBuilder::get(&url.to_owned())
|
2021-08-24 21:38:53 +08:00
|
|
|
.protobuf(params)?
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
2021-08-25 17:34:20 +08:00
|
|
|
.response::<Workspace>()
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(workspace) => Ok(Some(workspace)),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
if e.is_not_found() {
|
|
|
|
|
Ok(None)
|
|
|
|
|
} else {
|
|
|
|
|
Err(e.into())
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_workspace_request(
|
|
|
|
|
params: UpdateWorkspaceParams,
|
|
|
|
|
url: &str,
|
|
|
|
|
) -> Result<(), WorkspaceError> {
|
|
|
|
|
let _ = HttpRequestBuilder::patch(&url.to_owned())
|
|
|
|
|
.protobuf(params)?
|
|
|
|
|
.send()
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn delete_workspace_request(
|
|
|
|
|
params: DeleteWorkspaceParams,
|
|
|
|
|
url: &str,
|
|
|
|
|
) -> Result<(), WorkspaceError> {
|
|
|
|
|
let _ = HttpRequestBuilder::delete(&url.to_owned())
|
|
|
|
|
.protobuf(params)?
|
|
|
|
|
.send()
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
2021-08-24 21:38:53 +08:00
|
|
|
}
|