2021-07-14 21:12:52 +08:00
|
|
|
use crate::{
|
2021-07-31 10:50:56 +08:00
|
|
|
entities::app::{App, CreateAppParams, *},
|
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-09-07 21:31:04 +08:00
|
|
|
services::{helper::spawn, server::Server},
|
2021-07-20 14:03:21 +08:00
|
|
|
sql_tables::app::{AppTable, AppTableChangeset, AppTableSql},
|
2021-07-14 21:12:52 +08:00
|
|
|
};
|
2021-09-02 17:43:10 +08:00
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
use flowy_database::SqliteConnection;
|
2021-07-14 21:12:52 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-09-01 22:50:22 +08:00
|
|
|
pub(crate) struct AppController {
|
2021-07-14 21:12:52 +08:00
|
|
|
user: Arc<dyn WorkspaceUser>,
|
2021-07-20 14:03:21 +08:00
|
|
|
sql: Arc<AppTableSql>,
|
2021-09-07 21:31:04 +08:00
|
|
|
database: Arc<dyn WorkspaceDatabase>,
|
2021-09-01 22:50:22 +08:00
|
|
|
server: Server,
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppController {
|
2021-09-07 21:31:04 +08:00
|
|
|
pub(crate) fn new(user: Arc<dyn WorkspaceUser>, database: Arc<dyn WorkspaceDatabase>, server: Server) -> Self {
|
|
|
|
let sql = Arc::new(AppTableSql {});
|
2021-07-20 14:03:21 +08:00
|
|
|
Self {
|
|
|
|
user,
|
|
|
|
sql,
|
2021-09-07 21:31:04 +08:00
|
|
|
database,
|
2021-09-01 22:50:22 +08:00
|
|
|
server,
|
2021-07-20 14:03:21 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-02 19:57:19 +08:00
|
|
|
pub(crate) async fn create_app(&self, params: CreateAppParams) -> Result<App, WorkspaceError> {
|
|
|
|
let app = self.create_app_on_server(params).await?;
|
|
|
|
let app_table = AppTable::new(app.clone());
|
2021-09-07 21:31:04 +08:00
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let _ = self.sql.create_app(app_table, &*conn)?;
|
2021-09-07 17:12:03 +08:00
|
|
|
|
|
|
|
// Opti: transaction
|
2021-09-07 21:31:04 +08:00
|
|
|
let apps = self.read_local_apps(&app.workspace_id, &*conn)?;
|
2021-09-07 23:30:43 +08:00
|
|
|
observable(&app.workspace_id, WorkspaceObservable::WorkspaceCreateApp)
|
2021-09-07 17:12:03 +08:00
|
|
|
.payload(apps)
|
|
|
|
.build();
|
2021-07-19 22:44:37 +08:00
|
|
|
Ok(app)
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
pub(crate) async fn read_app(&self, params: QueryAppParams) -> Result<App, WorkspaceError> {
|
2021-09-07 21:31:04 +08:00
|
|
|
let app_table = self
|
|
|
|
.sql
|
|
|
|
.read_app(¶ms.app_id, params.is_trash, &*self.database.db_connection()?)?;
|
2021-09-02 19:57:19 +08:00
|
|
|
let _ = self.read_app_on_server(params).await?;
|
2021-07-20 15:51:49 +08:00
|
|
|
Ok(app_table.into())
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:50:22 +08:00
|
|
|
pub(crate) async fn delete_app(&self, app_id: &str) -> Result<(), WorkspaceError> {
|
2021-09-07 21:31:04 +08:00
|
|
|
let _ = self.delete_app_on_server(app_id);
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let app = self.sql.delete_app(app_id, &*conn)?;
|
2021-09-07 17:12:03 +08:00
|
|
|
// Opti: transaction
|
2021-09-07 21:31:04 +08:00
|
|
|
let apps = self.read_local_apps(&app.workspace_id, &*conn)?;
|
2021-09-07 23:30:43 +08:00
|
|
|
observable(&app.workspace_id, WorkspaceObservable::WorkspaceDeleteApp)
|
2021-09-07 17:12:03 +08:00
|
|
|
.payload(apps)
|
|
|
|
.build();
|
2021-07-29 17:27:59 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
fn read_local_apps(&self, workspace_id: &str, conn: &SqliteConnection) -> Result<RepeatedApp, WorkspaceError> {
|
|
|
|
let app_tables = self.sql.read_apps(workspace_id, false, conn)?;
|
2021-09-07 17:12:03 +08:00
|
|
|
let apps = app_tables.into_iter().map(|table| table.into()).collect::<Vec<App>>();
|
|
|
|
Ok(RepeatedApp { items: apps })
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:50:22 +08:00
|
|
|
pub(crate) async fn update_app(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> {
|
2021-09-07 21:31:04 +08:00
|
|
|
let _ = self.update_app_on_server(params.clone()).await?;
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
let changeset = AppTableChangeset::new(params);
|
|
|
|
let app_id = changeset.id.clone();
|
|
|
|
let conn = self.database.db_connection()?;
|
|
|
|
let _ = self.sql.update_app(changeset, &*conn)?;
|
|
|
|
let app: App = self.sql.read_app(&app_id, false, &*conn)?.into();
|
2021-09-07 23:30:43 +08:00
|
|
|
observable(&app_id, WorkspaceObservable::AppUpdated).payload(app).build();
|
2021-07-14 21:12:52 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
2021-07-20 14:03:21 +08:00
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
impl AppController {
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-02 19:57:19 +08:00
|
|
|
async fn create_app_on_server(&self, params: CreateAppParams) -> Result<App, WorkspaceError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let app = self.server.create_app(&token, params).await?;
|
2021-09-05 13:50:23 +08:00
|
|
|
log::info!("😁 {:?}", app);
|
2021-09-02 19:57:19 +08:00
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-02 19:57:19 +08:00
|
|
|
async fn update_app_on_server(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
spawn(async move {
|
|
|
|
match server.update_app(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Update app failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-02 19:57:19 +08:00
|
|
|
async fn delete_app_on_server(&self, app_id: &str) -> Result<(), WorkspaceError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
let params = DeleteAppParams {
|
|
|
|
app_id: app_id.to_string(),
|
|
|
|
};
|
|
|
|
spawn(async move {
|
|
|
|
match server.delete_app(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Delete app failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-09-02 19:57:19 +08:00
|
|
|
async fn read_app_on_server(&self, params: QueryAppParams) -> Result<(), WorkspaceError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
spawn(async move {
|
2021-09-07 17:12:03 +08:00
|
|
|
// Opti: retry?
|
|
|
|
let app = server.read_app(&token, params).await.unwrap();
|
|
|
|
match app {
|
|
|
|
None => {},
|
|
|
|
Some(_) => {},
|
2021-09-02 19:57:19 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
2021-07-20 14:03:21 +08:00
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|