2021-07-14 21:12:52 +08:00
|
|
|
use crate::{
|
2021-07-20 14:03:21 +08:00
|
|
|
entities::{
|
|
|
|
app::{App, CreateAppParams, *},
|
|
|
|
view::View,
|
|
|
|
},
|
2021-07-14 21:12:52 +08:00
|
|
|
errors::*,
|
2021-07-20 14:03:21 +08:00
|
|
|
module::{WorkspaceDatabase, WorkspaceUser},
|
2021-07-21 15:43:05 +08:00
|
|
|
observable::*,
|
2021-07-20 14:03:21 +08:00
|
|
|
services::ViewController,
|
|
|
|
sql_tables::app::{AppTable, AppTableChangeset, AppTableSql},
|
2021-07-14 21:12:52 +08:00
|
|
|
};
|
2021-07-20 14:03:21 +08:00
|
|
|
use flowy_dispatch::prelude::DispatchFuture;
|
2021-07-14 21:12:52 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct AppController {
|
|
|
|
user: Arc<dyn WorkspaceUser>,
|
2021-07-20 14:03:21 +08:00
|
|
|
sql: Arc<AppTableSql>,
|
|
|
|
view_controller: Arc<ViewController>,
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppController {
|
2021-07-20 14:03:21 +08:00
|
|
|
pub fn new(
|
|
|
|
user: Arc<dyn WorkspaceUser>,
|
|
|
|
database: Arc<dyn WorkspaceDatabase>,
|
|
|
|
view_controller: Arc<ViewController>,
|
|
|
|
) -> Self {
|
|
|
|
let sql = Arc::new(AppTableSql { database });
|
|
|
|
Self {
|
|
|
|
user,
|
|
|
|
sql,
|
|
|
|
view_controller,
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
|
2021-07-19 22:44:37 +08:00
|
|
|
pub fn save_app(&self, params: CreateAppParams) -> Result<App, WorkspaceError> {
|
|
|
|
let app_table = AppTable::new(params);
|
|
|
|
let app: App = app_table.clone().into();
|
2021-07-20 14:03:21 +08:00
|
|
|
let _ = self.sql.write_app_table(app_table)?;
|
2021-07-21 22:41:44 +08:00
|
|
|
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceAddApp);
|
2021-07-19 22:44:37 +08:00
|
|
|
Ok(app)
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
2021-07-20 15:51:49 +08:00
|
|
|
pub async fn get_app(&self, app_id: &str) -> Result<App, WorkspaceError> {
|
|
|
|
let app_table = self.get_app_table(app_id).await?;
|
|
|
|
Ok(app_table.into())
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:12:52 +08:00
|
|
|
pub fn update_app(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> {
|
2021-07-19 17:37:58 +08:00
|
|
|
let changeset = AppTableChangeset::new(params);
|
2021-07-21 15:43:05 +08:00
|
|
|
let app_id = changeset.id.clone();
|
2021-07-20 14:03:21 +08:00
|
|
|
let _ = self.sql.update_app_table(changeset)?;
|
2021-07-21 22:41:44 +08:00
|
|
|
send_observable(&app_id, WorkspaceObservable::AppUpdateDesc);
|
2021-07-14 21:12:52 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-20 14:03:21 +08:00
|
|
|
|
2021-07-20 15:51:49 +08:00
|
|
|
pub async fn get_views(&self, app_id: &str) -> Result<Vec<View>, WorkspaceError> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let views = self
|
|
|
|
.sql
|
2021-07-20 15:51:49 +08:00
|
|
|
.read_views_belong_to_app(app_id)?
|
2021-07-20 14:03:21 +08:00
|
|
|
.into_iter()
|
|
|
|
.map(|view_table| view_table.into())
|
|
|
|
.collect::<Vec<View>>();
|
|
|
|
|
|
|
|
Ok(views)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_app_table(&self, app_id: &str) -> DispatchFuture<Result<AppTable, WorkspaceError>> {
|
|
|
|
let sql = self.sql.clone();
|
|
|
|
let app_id = app_id.to_owned();
|
|
|
|
DispatchFuture {
|
|
|
|
fut: Box::pin(async move {
|
|
|
|
let app_table = sql.read_app_table(&app_id)?;
|
|
|
|
// TODO: fetch app from remote server
|
|
|
|
Ok(app_table)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|