122 lines
3.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
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 {
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-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)?;
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceCreateApp);
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?;
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceDeleteApp);
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();
let _ = self.sql.update_app(changeset)?;
2021-09-02 19:57:19 +08:00
let _ = self.update_app_on_server(params).await?;
send_observable(&app_id, WorkspaceObservable::AppUpdated);
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
}
}