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-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-07-14 21:12:52 +08:00
|
|
|
};
|
2021-09-02 17:43:10 +08:00
|
|
|
|
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-08-30 16:18:58 +08:00
|
|
|
#[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,
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let sql = Arc::new(AppTableSql { database });
|
|
|
|
Self {
|
|
|
|
user,
|
|
|
|
sql,
|
|
|
|
view_controller,
|
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-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-07-23 14:37:18 +08:00
|
|
|
let _ = self.sql.create_app(app_table)?;
|
2021-07-29 22:22:35 +08:00
|
|
|
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceCreateApp);
|
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> {
|
|
|
|
let app_table = self.sql.read_app(¶ms.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> {
|
2021-07-29 22:22:35 +08:00
|
|
|
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-07-29 22:22:35 +08:00
|
|
|
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceDeleteApp);
|
2021-07-29 17:27:59 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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();
|
2021-07-23 14:37:18 +08:00
|
|
|
let _ = self.sql.update_app(changeset)?;
|
2021-09-02 19:57:19 +08:00
|
|
|
let _ = self.update_app_on_server(params).await?;
|
2021-07-29 22:22:35 +08:00
|
|
|
send_observable(&app_id, WorkspaceObservable::AppUpdated);
|
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 {
|
|
|
|
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?;
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_app_on_server(&self, params: QueryAppParams) -> Result<(), WorkspaceError> {
|
|
|
|
let token = self.user.token()?;
|
|
|
|
let server = self.server.clone();
|
|
|
|
spawn(async move {
|
|
|
|
match server.read_app(&token, params).await {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => {
|
|
|
|
// TODO: retry?
|
|
|
|
log::error!("Read app failed: {:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(())
|
2021-07-20 14:03:21 +08:00
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|