148 lines
4.9 KiB
Rust
Raw Normal View History

use crate::{
2021-07-31 10:50:56 +08:00
entities::app::{App, CreateAppParams, *},
errors::*,
2021-07-20 14:03:21 +08:00
module::{WorkspaceDatabase, WorkspaceUser},
2021-07-21 15:43:05 +08:00
observable::*,
2021-09-02 19:57:19 +08:00
services::{helper::spawn, server::Server, ViewController},
2021-07-20 14:03:21 +08:00
sql_tables::app::{AppTable, AppTableChangeset, AppTableSql},
};
2021-09-02 17:43:10 +08:00
2021-09-07 17:12:03 +08:00
use crate::entities::view::RepeatedView;
use std::sync::Arc;
2021-09-01 22:50:22 +08:00
pub(crate) struct AppController {
user: Arc<dyn WorkspaceUser>,
2021-07-20 14:03:21 +08:00
sql: Arc<AppTableSql>,
#[allow(dead_code)]
2021-07-20 14:03:21 +08:00
view_controller: Arc<ViewController>,
2021-09-01 22:50:22 +08:00
server: Server,
}
impl AppController {
2021-09-01 22:50:22 +08:00
pub(crate) fn new(
2021-07-20 14:03:21 +08:00
user: Arc<dyn WorkspaceUser>,
database: Arc<dyn WorkspaceDatabase>,
view_controller: Arc<ViewController>,
2021-09-01 22:50:22 +08:00
server: Server,
2021-07-20 14:03:21 +08:00
) -> Self {
2021-09-07 17:12:03 +08:00
let sql = Arc::new(AppTableSql::new(database));
2021-07-20 14:03:21 +08:00
Self {
user,
sql,
view_controller,
2021-09-01 22:50:22 +08:00
server,
2021-07-20 14:03:21 +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());
let _ = self.sql.create_app(app_table)?;
2021-09-07 17:12:03 +08:00
// Opti: transaction
let apps = self.read_local_apps(&app.workspace_id)?;
ObservableBuilder::new(&app.workspace_id, WorkspaceObservable::WorkspaceCreateApp)
.payload(apps)
.build();
2021-07-19 22:44:37 +08:00
Ok(app)
}
2021-09-02 19:57:19 +08:00
pub(crate) async fn read_app(&self, params: QueryAppParams) -> Result<App, WorkspaceError> {
let app_table = self.sql.read_app(&params.app_id, params.is_trash)?;
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> {
let app = self.sql.delete_app(app_id)?;
2021-09-02 19:57:19 +08:00
let _ = self.delete_app_on_server(app_id).await?;
2021-09-07 17:12:03 +08:00
// Opti: transaction
let apps = self.read_local_apps(&app.workspace_id)?;
ObservableBuilder::new(&app.workspace_id, WorkspaceObservable::WorkspaceDeleteApp)
.payload(apps)
.build();
Ok(())
}
2021-09-07 17:12:03 +08:00
fn read_local_apps(&self, workspace_id: &str) -> Result<RepeatedApp, WorkspaceError> {
let app_tables = self.sql.read_apps(workspace_id, false)?;
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-02 19:57:19 +08:00
let changeset = AppTableChangeset::new(params.clone());
2021-07-21 15:43:05 +08:00
let app_id = changeset.id.clone();
let _ = self.sql.update_app(changeset)?;
2021-09-02 19:57:19 +08:00
let _ = self.update_app_on_server(params).await?;
2021-09-07 17:12:03 +08:00
let app: App = self.sql.read_app(&app_id, false)?.into();
ObservableBuilder::new(&app_id, WorkspaceObservable::AppUpdated)
.payload(app)
.build();
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
}
}