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-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-08-25 17:34:20 +08:00
|
|
|
use flowy_net::request::HttpRequestBuilder;
|
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>,
|
2021-08-30 16:18:58 +08:00
|
|
|
#[allow(dead_code)]
|
2021-07-20 14:03:21 +08:00
|
|
|
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-08-25 17:34:20 +08:00
|
|
|
pub fn create_app(&self, mut params: CreateAppParams) -> Result<App, WorkspaceError> {
|
|
|
|
let user_id = self.user.user_id()?;
|
|
|
|
params.user_id = user_id;
|
|
|
|
|
|
|
|
// TODO: server
|
|
|
|
|
2021-07-19 22:44:37 +08:00
|
|
|
let app_table = AppTable::new(params);
|
|
|
|
let app: App = app_table.clone().into();
|
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-07-29 17:27:59 +08:00
|
|
|
pub async fn read_app(&self, app_id: &str, is_trash: bool) -> Result<App, WorkspaceError> {
|
|
|
|
let app_table = self.async_read_app(&app_id, is_trash).await?;
|
2021-07-20 15:51:49 +08:00
|
|
|
Ok(app_table.into())
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:27:59 +08:00
|
|
|
pub 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)?;
|
|
|
|
send_observable(&app.workspace_id, WorkspaceObservable::WorkspaceDeleteApp);
|
2021-07-29 17:27:59 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async 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-23 14:37:18 +08:00
|
|
|
let _ = self.sql.update_app(changeset)?;
|
2021-07-29 22:22:35 +08:00
|
|
|
send_observable(&app_id, WorkspaceObservable::AppUpdated);
|
2021-07-14 21:12:52 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-20 14:03:21 +08:00
|
|
|
|
2021-07-29 17:27:59 +08:00
|
|
|
fn async_read_app(
|
|
|
|
&self,
|
|
|
|
app_id: &str,
|
|
|
|
is_trash: bool,
|
|
|
|
) -> DispatchFuture<Result<AppTable, WorkspaceError>> {
|
2021-07-20 14:03:21 +08:00
|
|
|
let sql = self.sql.clone();
|
|
|
|
let app_id = app_id.to_owned();
|
|
|
|
DispatchFuture {
|
|
|
|
fut: Box::pin(async move {
|
2021-07-29 17:27:59 +08:00
|
|
|
let app_table = sql.read_app(&app_id, is_trash)?;
|
2021-07-20 14:03:21 +08:00
|
|
|
// TODO: fetch app from remote server
|
|
|
|
Ok(app_table)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 21:12:52 +08:00
|
|
|
}
|
2021-08-25 17:34:20 +08:00
|
|
|
|
|
|
|
pub async fn create_app_request(params: CreateAppParams, url: &str) -> Result<App, WorkspaceError> {
|
|
|
|
let app = HttpRequestBuilder::post(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.response()
|
|
|
|
.await?;
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn read_app_request(
|
|
|
|
params: QueryAppParams,
|
|
|
|
url: &str,
|
|
|
|
) -> Result<Option<App>, WorkspaceError> {
|
|
|
|
let result = HttpRequestBuilder::get(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
match result {
|
2021-09-01 16:08:32 +08:00
|
|
|
Ok(builder) => {
|
|
|
|
let app = builder.response::<App>().await?;
|
|
|
|
Ok(Some(app))
|
|
|
|
},
|
2021-08-25 17:34:20 +08:00
|
|
|
Err(e) => {
|
|
|
|
if e.is_not_found() {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Err(e.into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_app_request(params: UpdateAppParams, url: &str) -> Result<(), WorkspaceError> {
|
|
|
|
let _ = HttpRequestBuilder::patch(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_app_request(params: DeleteAppParams, url: &str) -> Result<(), WorkspaceError> {
|
|
|
|
let _ = HttpRequestBuilder::delete(&url.to_owned())
|
|
|
|
.protobuf(params)?
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|