From 464d5396e6d1cdd0cee1bb4daed56e72395066ae Mon Sep 17 00:00:00 2001 From: appflowy Date: Fri, 10 Sep 2021 15:53:24 +0800 Subject: [PATCH] config doc server api --- backend/Cargo.toml | 1 + backend/migrations/20210909115140_doc.sql | 6 + backend/src/application.rs | 7 + backend/src/doc_service/doc.rs | 124 ++++++++++++++++++ backend/src/doc_service/mod.rs | 6 + backend/src/doc_service/router.rs | 37 ++++++ backend/src/doc_service/sql_builder.rs | 37 ++++++ backend/src/entities/doc.rs | 18 +++ backend/src/entities/mod.rs | 1 + backend/src/entities/workspace.rs | 51 +++++-- backend/src/lib.rs | 7 +- backend/src/workspace_service/app/app.rs | 20 ++- backend/src/workspace_service/app/mod.rs | 3 +- .../app/{builder.rs => sql_builder.rs} | 24 +--- .../user_default/user_default.rs | 6 +- backend/src/workspace_service/view/mod.rs | 3 +- .../view/{builder.rs => sql_builder.rs} | 27 +--- backend/src/workspace_service/view/view.rs | 16 ++- .../src/workspace_service/workspace/mod.rs | 3 +- .../workspace/{builder.rs => sql_builder.rs} | 7 +- .../workspace_service/workspace/workspace.rs | 27 ++-- .../flowy-document/src/entities/doc/mod.rs | 2 +- rust-lib/flowy-document/src/event.rs | 2 +- .../src/handlers/doc_handler.rs | 6 +- rust-lib/flowy-document/src/lib.rs | 2 +- rust-lib/flowy-document/src/observable/mod.rs | 2 - .../src/services/doc_controller.rs | 20 +-- .../flowy-document/src/services/server/mod.rs | 2 +- .../src/services/server/server_api.rs | 10 +- .../src/services/server/server_api_mock.rs | 9 +- 30 files changed, 354 insertions(+), 132 deletions(-) create mode 100644 backend/migrations/20210909115140_doc.sql create mode 100644 backend/src/doc_service/doc.rs create mode 100644 backend/src/doc_service/mod.rs create mode 100644 backend/src/doc_service/router.rs create mode 100644 backend/src/doc_service/sql_builder.rs create mode 100644 backend/src/entities/doc.rs rename backend/src/workspace_service/app/{builder.rs => sql_builder.rs} (77%) rename backend/src/workspace_service/view/{builder.rs => sql_builder.rs} (71%) rename backend/src/workspace_service/workspace/{builder.rs => sql_builder.rs} (93%) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 107b720132..0a7a2ac6ed 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -45,6 +45,7 @@ tokio = { version = "1", features = ["full"] } flowy-log = { path = "../rust-lib/flowy-log" } flowy-user = { path = "../rust-lib/flowy-user" } flowy-workspace = { path = "../rust-lib/flowy-workspace" } +flowy-document = { path = "../rust-lib/flowy-document" } flowy-net = { path = "../rust-lib/flowy-net", features = ["http_server"] } ormx = { version = "0.7", features = ["postgres"]} diff --git a/backend/migrations/20210909115140_doc.sql b/backend/migrations/20210909115140_doc.sql new file mode 100644 index 0000000000..3c30bed6b4 --- /dev/null +++ b/backend/migrations/20210909115140_doc.sql @@ -0,0 +1,6 @@ +-- Add migration script here +CREATE TABLE IF NOT EXISTS doc_table( + id uuid NOT NULL, + PRIMARY KEY (id), + data TEXT NOT NULL +); \ No newline at end of file diff --git a/backend/src/application.rs b/backend/src/application.rs index d28fe311ee..a0f9211221 100644 --- a/backend/src/application.rs +++ b/backend/src/application.rs @@ -5,6 +5,7 @@ use crate::{ Settings, }, context::AppContext, + doc_service::router as doc, user_service::router as user, workspace_service::{app::router as app, view::router as view, workspace::router as workspace}, ws_service, @@ -112,6 +113,12 @@ fn user_scope() -> Scope { .route(web::get().to(view::read_handler)) .route(web::patch().to(view::update_handler)) ) + .service(web::resource("/doc") + .route(web::post().to(doc::create_handler)) + .route(web::delete().to(doc::delete_handler)) + .route(web::get().to(doc::read_handler)) + .route(web::patch().to(doc::update_handler)) + ) // password .service(web::resource("/password_change") .route(web::post().to(user::change_password)) diff --git a/backend/src/doc_service/doc.rs b/backend/src/doc_service/doc.rs new file mode 100644 index 0000000000..e586cabfd3 --- /dev/null +++ b/backend/src/doc_service/doc.rs @@ -0,0 +1,124 @@ +use super::sql_builder::*; +use crate::{ + entities::doc::{DocTable, DOC_TABLE}, + sqlx_ext::{map_sqlx_error, SqlBuilder}, +}; +use anyhow::Context; +use flowy_document::protobuf::{CreateDocParams, Doc, QueryDocParams, UpdateDocParams}; +use flowy_net::{errors::ServerError, response::FlowyResponse}; +use sqlx::{postgres::PgArguments, PgPool, Postgres}; +use uuid::Uuid; + +pub(crate) async fn create_doc( + pool: &PgPool, + params: CreateDocParams, +) -> Result { + let uuid = Uuid::parse_str(¶ms.id)?; + let mut transaction = pool + .begin() + .await + .context("Failed to acquire a Postgres connection to create doc")?; + let (sql, args) = Builder::new(uuid).data(params.data).build()?; + + let _ = sqlx::query_with(&sql, args) + .execute(&mut transaction) + .await + .map_err(map_sqlx_error)?; + + transaction + .commit() + .await + .context("Failed to commit SQL transaction to create doc.")?; + + Ok(FlowyResponse::success()) +} + +pub(crate) async fn read_doc( + pool: &PgPool, + params: QueryDocParams, +) -> Result { + let doc_id = Uuid::parse_str(¶ms.doc_id)?; + let mut transaction = pool + .begin() + .await + .context("Failed to acquire a Postgres connection to read doc")?; + + let builder = SqlBuilder::select(DOC_TABLE) + .add_field("*") + .and_where_eq("id", &doc_id); + + let (sql, args) = builder.build()?; + // TODO: benchmark the speed of different documents with different size + let doc: Doc = sqlx::query_as_with::(&sql, args) + .fetch_one(&mut transaction) + .await + .map_err(map_sqlx_error)? + .into(); + + transaction + .commit() + .await + .context("Failed to commit SQL transaction to read doc.")?; + + FlowyResponse::success().pb(doc) +} + +pub(crate) async fn update_doc( + pool: &PgPool, + mut params: UpdateDocParams, +) -> Result { + let doc_id = Uuid::parse_str(¶ms.id)?; + let mut transaction = pool + .begin() + .await + .context("Failed to acquire a Postgres connection to update doc")?; + + let data = match params.has_data() { + true => Some(params.take_data()), + false => None, + }; + + let (sql, args) = SqlBuilder::update(DOC_TABLE) + .add_some_arg("data", data) + .and_where_eq("id", doc_id) + .build()?; + + sqlx::query_with(&sql, args) + .execute(&mut transaction) + .await + .map_err(map_sqlx_error)?; + + transaction + .commit() + .await + .context("Failed to commit SQL transaction to update doc.")?; + + Ok(FlowyResponse::success()) +} + +pub(crate) async fn delete_doc( + pool: &PgPool, + params: QueryDocParams, +) -> Result { + let doc_id = Uuid::parse_str(¶ms.doc_id)?; + let mut transaction = pool + .begin() + .await + .context("Failed to acquire a Postgres connection to delete doc")?; + + let (sql, args) = SqlBuilder::delete(DOC_TABLE) + .and_where_eq("id", doc_id) + .build()?; + + let _ = sqlx::query_with(&sql, args) + .execute(&mut transaction) + .await + .map_err(map_sqlx_error)?; + + transaction + .commit() + .await + .context("Failed to commit SQL transaction to delete doc.")?; + + Ok(FlowyResponse::success()) +} diff --git a/backend/src/doc_service/mod.rs b/backend/src/doc_service/mod.rs new file mode 100644 index 0000000000..372c295e14 --- /dev/null +++ b/backend/src/doc_service/mod.rs @@ -0,0 +1,6 @@ +mod doc; +pub mod router; +mod sql_builder; + +pub use doc::*; +pub use router::*; diff --git a/backend/src/doc_service/router.rs b/backend/src/doc_service/router.rs new file mode 100644 index 0000000000..5926fcba0d --- /dev/null +++ b/backend/src/doc_service/router.rs @@ -0,0 +1,37 @@ +use crate::routers::utils::parse_from_payload; +use actix_web::{ + web::{Data, Payload}, + HttpResponse, +}; +use flowy_document::protobuf::CreateDocParams; +use flowy_net::errors::ServerError; +use sqlx::PgPool; + +pub async fn create_handler( + payload: Payload, + _pool: Data, +) -> Result { + let _params: CreateDocParams = parse_from_payload(payload).await?; + unimplemented!() +} + +pub async fn read_handler( + _payload: Payload, + _pool: Data, +) -> Result { + unimplemented!() +} + +pub async fn update_handler( + _payload: Payload, + _pool: Data, +) -> Result { + unimplemented!() +} + +pub async fn delete_handler( + _payload: Payload, + _pool: Data, +) -> Result { + unimplemented!() +} diff --git a/backend/src/doc_service/sql_builder.rs b/backend/src/doc_service/sql_builder.rs new file mode 100644 index 0000000000..1b58f8ed1e --- /dev/null +++ b/backend/src/doc_service/sql_builder.rs @@ -0,0 +1,37 @@ +use crate::{ + entities::doc::{DocTable, DOC_TABLE}, + sqlx_ext::SqlBuilder, +}; + +use flowy_net::errors::ServerError; + +use sqlx::postgres::PgArguments; +use uuid::Uuid; + +pub struct Builder { + table: DocTable, +} + +impl Builder { + pub fn new(id: Uuid) -> Self { + let table = DocTable { + id, + data: "".to_owned(), + }; + Self { table } + } + + pub fn data(mut self, data: String) -> Self { + self.table.data = data; + self + } + + pub fn build(self) -> Result<(String, PgArguments), ServerError> { + let (sql, args) = SqlBuilder::create(DOC_TABLE) + .add_arg("id", self.table.id) + .add_arg("data", self.table.data) + .build()?; + + Ok((sql, args)) + } +} diff --git a/backend/src/entities/doc.rs b/backend/src/entities/doc.rs new file mode 100644 index 0000000000..c9712a8bbb --- /dev/null +++ b/backend/src/entities/doc.rs @@ -0,0 +1,18 @@ +use flowy_document::protobuf::Doc; + +pub(crate) const DOC_TABLE: &'static str = "doc_table"; + +#[derive(Debug, Clone, sqlx::FromRow)] +pub struct DocTable { + pub(crate) id: uuid::Uuid, + pub(crate) data: String, +} + +impl std::convert::Into for DocTable { + fn into(self) -> Doc { + let mut doc = Doc::new(); + doc.set_id(self.id.to_string()); + doc.set_data(self.data); + doc + } +} diff --git a/backend/src/entities/mod.rs b/backend/src/entities/mod.rs index 8a0ee3a365..749cb640ef 100644 --- a/backend/src/entities/mod.rs +++ b/backend/src/entities/mod.rs @@ -1,3 +1,4 @@ +pub mod doc; pub mod token; pub mod user; pub mod workspace; diff --git a/backend/src/entities/workspace.rs b/backend/src/entities/workspace.rs index 2c4f1ae54e..fa85fceeaa 100644 --- a/backend/src/entities/workspace.rs +++ b/backend/src/entities/workspace.rs @@ -1,4 +1,10 @@ use chrono::Utc; +use flowy_workspace::protobuf::{App, RepeatedView, View, ViewType}; +use protobuf::ProtobufEnum; + +pub(crate) const WORKSPACE_TABLE: &'static str = "workspace_table"; +pub(crate) const APP_TABLE: &'static str = "app_table"; +pub(crate) const VIEW_TABLE: &'static str = "view_table"; #[derive(Debug, Clone, sqlx::FromRow)] pub struct WorkspaceTable { @@ -24,6 +30,21 @@ pub struct AppTable { pub(crate) is_trash: bool, } +impl std::convert::Into for AppTable { + fn into(self) -> App { + let mut app = App::default(); + app.set_id(self.id.to_string()); + app.set_workspace_id(self.workspace_id.to_string()); + app.set_name(self.name.clone()); + app.set_desc(self.description.clone()); + app.set_belongings(RepeatedView::default()); + app.set_modified_time(self.modified_time.timestamp()); + app.set_create_time(self.create_time.timestamp()); + + app + } +} + #[derive(Debug, Clone, sqlx::FromRow)] pub struct ViewTable { pub(crate) id: uuid::Uuid, @@ -36,16 +57,20 @@ pub struct ViewTable { pub(crate) view_type: i32, pub(crate) is_trash: bool, } -// impl std::convert::Into for ViewTable { -// fn into(self) -> View { -// View { -// id: self.id.to_string(), -// belong_to_id: self.belong_to_id, -// name: self.name, -// desc: self.description, -// view_type: ViewType::from(self.view_type), -// version: 0, -// belongings: RepeatedView::default(), -// } -// } -// } +impl std::convert::Into for ViewTable { + fn into(self) -> View { + let view_type = ViewType::from_i32(self.view_type).unwrap_or(ViewType::Doc); + + let mut view = View::default(); + view.set_id(self.id.to_string()); + view.set_belong_to_id(self.belong_to_id); + view.set_name(self.name); + view.set_desc(self.description); + view.set_view_type(view_type); + view.set_belongings(RepeatedView::default()); + view.set_create_time(self.create_time.timestamp()); + view.set_modified_time(self.modified_time.timestamp()); + + view + } +} diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 2253932c00..cd3a3bfdf3 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -1,10 +1,11 @@ pub mod application; pub mod config; mod context; +mod doc_service; mod entities; mod middleware; mod routers; mod sqlx_ext; -pub mod user_service; -pub mod workspace_service; -pub mod ws_service; +mod user_service; +mod workspace_service; +mod ws_service; diff --git a/backend/src/workspace_service/app/app.rs b/backend/src/workspace_service/app/app.rs index f5bfa57d62..39c8d80ede 100644 --- a/backend/src/workspace_service/app/app.rs +++ b/backend/src/workspace_service/app/app.rs @@ -1,24 +1,20 @@ use flowy_net::{errors::ServerError, response::FlowyResponse}; use crate::{ - entities::workspace::AppTable, + entities::workspace::{AppTable, APP_TABLE}, sqlx_ext::{map_sqlx_error, SqlBuilder}, user_service::LoggedUser, - workspace_service::{ - app::{check_app_id, make_app_from_table, Builder}, - view::read_views_belong_to_id, - }, + workspace_service::{app::sql_builder::*, view::read_views_belong_to_id}, }; use anyhow::Context; use chrono::Utc; use flowy_net::errors::invalid_params; - use flowy_workspace::{ entities::{ app::parser::{AppDesc, AppName}, workspace::parser::WorkspaceId, }, - protobuf::{CreateAppParams, QueryAppParams, RepeatedView, UpdateAppParams}, + protobuf::{App, CreateAppParams, QueryAppParams, RepeatedView, UpdateAppParams}, }; use protobuf::Message; use sqlx::{postgres::PgArguments, PgPool, Postgres}; @@ -67,7 +63,7 @@ pub(crate) async fn read_app( .await .context("Failed to acquire a Postgres connection to read app")?; - let (sql, args) = SqlBuilder::select("app_table") + let (sql, args) = SqlBuilder::select(APP_TABLE) .add_field("*") .and_where_eq("id", app_id) .build()?; @@ -91,7 +87,9 @@ pub(crate) async fn read_app( .await .context("Failed to commit SQL transaction to read app.")?; - let app = make_app_from_table(table, views); + let mut app: App = table.into(); + app.set_belongings(views); + FlowyResponse::success().pb(app) } @@ -131,7 +129,7 @@ pub(crate) async fn update_app( .await .context("Failed to acquire a Postgres connection to update app")?; - let (sql, args) = SqlBuilder::update("app_table") + let (sql, args) = SqlBuilder::update(APP_TABLE) .add_some_arg("name", name) .add_some_arg("color_style", color_style) .add_some_arg("description", desc) @@ -160,7 +158,7 @@ pub(crate) async fn delete_app(pool: &PgPool, app_id: &str) -> Result Result<(String, PgArguments, App), ServerError> { - let app = make_app_from_table(self.table.clone(), RepeatedView::default()); + let app: App = self.table.clone().into(); - let (sql, args) = SqlBuilder::create("app_table") + let (sql, args) = SqlBuilder::create(APP_TABLE) .add_arg("id", self.table.id) .add_arg("workspace_id", self.table.workspace_id) .add_arg("name", self.table.name) @@ -85,19 +88,6 @@ fn default_color_style() -> Vec { } } -pub(crate) fn make_app_from_table(table: AppTable, views: RepeatedView) -> App { - let mut app = App::default(); - app.set_id(table.id.to_string()); - app.set_workspace_id(table.workspace_id.to_string()); - app.set_name(table.name.clone()); - app.set_desc(table.description.clone()); - app.set_belongings(views); - app.set_modified_time(table.modified_time.timestamp()); - app.set_create_time(table.create_time.timestamp()); - - app -} - pub(crate) fn check_app_id(id: String) -> Result { let app_id = AppId::parse(id).map_err(invalid_params)?; let app_id = Uuid::parse_str(app_id.as_ref())?; diff --git a/backend/src/workspace_service/user_default/user_default.rs b/backend/src/workspace_service/user_default/user_default.rs index d9fdef97ac..799c7a6527 100644 --- a/backend/src/workspace_service/user_default/user_default.rs +++ b/backend/src/workspace_service/user_default/user_default.rs @@ -1,9 +1,9 @@ use crate::{ sqlx_ext::{map_sqlx_error, DBTransaction}, workspace_service::{ - app::Builder as AppBuilder, - view::Builder as ViewBuilder, - workspace::Builder as WorkspaceBuilder, + app::sql_builder::Builder as AppBuilder, + view::sql_builder::Builder as ViewBuilder, + workspace::sql_builder::Builder as WorkspaceBuilder, }, }; diff --git a/backend/src/workspace_service/view/mod.rs b/backend/src/workspace_service/view/mod.rs index 340d5bf4ff..bcaea84d46 100644 --- a/backend/src/workspace_service/view/mod.rs +++ b/backend/src/workspace_service/view/mod.rs @@ -1,6 +1,5 @@ -mod builder; pub mod router; +pub mod sql_builder; mod view; -pub use builder::*; pub(crate) use view::*; diff --git a/backend/src/workspace_service/view/builder.rs b/backend/src/workspace_service/view/sql_builder.rs similarity index 71% rename from backend/src/workspace_service/view/builder.rs rename to backend/src/workspace_service/view/sql_builder.rs index 10a4f5f9ed..e7def4c564 100644 --- a/backend/src/workspace_service/view/builder.rs +++ b/backend/src/workspace_service/view/sql_builder.rs @@ -1,9 +1,12 @@ -use crate::{entities::workspace::ViewTable, sqlx_ext::SqlBuilder}; +use crate::{ + entities::workspace::{ViewTable, VIEW_TABLE}, + sqlx_ext::SqlBuilder, +}; use chrono::Utc; use flowy_net::errors::{invalid_params, ServerError}; use flowy_workspace::{ entities::view::parser::ViewId, - protobuf::{RepeatedView, View, ViewType}, + protobuf::{View, ViewType}, }; use protobuf::ProtobufEnum; use sqlx::postgres::PgArguments; @@ -54,9 +57,9 @@ impl Builder { } pub fn build(self) -> Result<(String, PgArguments, View), ServerError> { - let view = make_view_from_table(self.table.clone(), RepeatedView::default()); + let view: View = self.table.clone().into(); - let (sql, args) = SqlBuilder::create("view_table") + let (sql, args) = SqlBuilder::create(VIEW_TABLE) .add_arg("id", self.table.id) .add_arg("belong_to_id", self.table.belong_to_id) .add_arg("name", self.table.name) @@ -71,22 +74,6 @@ impl Builder { } } -pub(crate) fn make_view_from_table(table: ViewTable, views: RepeatedView) -> View { - let view_type = ViewType::from_i32(table.view_type).unwrap_or(ViewType::Doc); - - let mut view = View::default(); - view.set_id(table.id.to_string()); - view.set_belong_to_id(table.belong_to_id); - view.set_name(table.name); - view.set_desc(table.description); - view.set_view_type(view_type); - view.set_belongings(views); - view.set_create_time(table.create_time.timestamp()); - view.set_modified_time(table.modified_time.timestamp()); - - view -} - pub(crate) fn check_view_id(id: String) -> Result { let view_id = ViewId::parse(id).map_err(invalid_params)?; let view_id = Uuid::parse_str(view_id.as_ref())?; diff --git a/backend/src/workspace_service/view/view.rs b/backend/src/workspace_service/view/view.rs index d8d7c425f8..e88774c6eb 100644 --- a/backend/src/workspace_service/view/view.rs +++ b/backend/src/workspace_service/view/view.rs @@ -1,7 +1,7 @@ use crate::{ entities::workspace::ViewTable, sqlx_ext::{map_sqlx_error, DBTransaction, SqlBuilder}, - workspace_service::view::{check_view_id, make_view_from_table, Builder}, + workspace_service::view::sql_builder::*, }; use anyhow::Context; use chrono::Utc; @@ -17,6 +17,7 @@ use flowy_workspace::{ protobuf::{CreateViewParams, QueryViewParams, RepeatedView, UpdateViewParams, View}, }; +use crate::entities::workspace::VIEW_TABLE; use sqlx::{postgres::PgArguments, PgPool, Postgres}; pub(crate) async fn create_view( @@ -63,7 +64,7 @@ pub(crate) async fn read_view( .await .context("Failed to acquire a Postgres connection to read view")?; - let (sql, args) = SqlBuilder::select("view_table") + let (sql, args) = SqlBuilder::select(VIEW_TABLE) .add_field("*") .and_where_eq("id", view_id) .build()?; @@ -87,7 +88,8 @@ pub(crate) async fn read_view( .await .context("Failed to commit SQL transaction to read view.")?; - let view = make_view_from_table(table, views); + let mut view: View = table.into(); + view.set_belongings(views); FlowyResponse::success().pb(view) } @@ -130,7 +132,7 @@ pub(crate) async fn update_view( .await .context("Failed to acquire a Postgres connection to update app")?; - let (sql, args) = SqlBuilder::update("view_table") + let (sql, args) = SqlBuilder::update(VIEW_TABLE) .add_some_arg("name", name) .add_some_arg("description", desc) .add_some_arg("thumbnail", thumbnail) @@ -162,7 +164,7 @@ pub(crate) async fn delete_view( .await .context("Failed to acquire a Postgres connection to delete view")?; - let (sql, args) = SqlBuilder::delete("view_table") + let (sql, args) = SqlBuilder::delete(VIEW_TABLE) .and_where_eq("id", view_id) .build()?; @@ -185,7 +187,7 @@ pub(crate) async fn read_views_belong_to_id<'c>( id: &str, ) -> Result, ServerError> { // TODO: add index for app_table - let (sql, args) = SqlBuilder::select("view_table") + let (sql, args) = SqlBuilder::select(VIEW_TABLE) .add_field("*") .and_where_eq("belong_to_id", id) .and_where_eq("is_trash", false) @@ -198,7 +200,7 @@ pub(crate) async fn read_views_belong_to_id<'c>( let views = tables .into_iter() - .map(|table| make_view_from_table(table, RepeatedView::default())) + .map(|table| table.into()) .collect::>(); Ok(views) diff --git a/backend/src/workspace_service/workspace/mod.rs b/backend/src/workspace_service/workspace/mod.rs index aecbf72766..55563e74eb 100644 --- a/backend/src/workspace_service/workspace/mod.rs +++ b/backend/src/workspace_service/workspace/mod.rs @@ -1,6 +1,5 @@ -pub mod builder; pub mod router; +pub mod sql_builder; mod workspace; -pub use builder::*; pub use workspace::*; diff --git a/backend/src/workspace_service/workspace/builder.rs b/backend/src/workspace_service/workspace/sql_builder.rs similarity index 93% rename from backend/src/workspace_service/workspace/builder.rs rename to backend/src/workspace_service/workspace/sql_builder.rs index 389a919597..37fea1fda1 100644 --- a/backend/src/workspace_service/workspace/builder.rs +++ b/backend/src/workspace_service/workspace/sql_builder.rs @@ -1,4 +1,7 @@ -use crate::{entities::workspace::WorkspaceTable, sqlx_ext::SqlBuilder}; +use crate::{ + entities::workspace::{WorkspaceTable, WORKSPACE_TABLE}, + sqlx_ext::SqlBuilder, +}; use chrono::Utc; use flowy_net::errors::{invalid_params, ServerError}; use flowy_workspace::{ @@ -42,7 +45,7 @@ impl Builder { let workspace = make_workspace_from_table(self.table.clone(), None); // TODO: use macro to fetch each field from struct - let (sql, args) = SqlBuilder::create("workspace_table") + let (sql, args) = SqlBuilder::create(WORKSPACE_TABLE) .add_arg("id", self.table.id) .add_arg("name", self.table.name) .add_arg("description", self.table.description) diff --git a/backend/src/workspace_service/workspace/workspace.rs b/backend/src/workspace_service/workspace/workspace.rs index 823358e2e9..4088e702d1 100644 --- a/backend/src/workspace_service/workspace/workspace.rs +++ b/backend/src/workspace_service/workspace/workspace.rs @@ -1,4 +1,4 @@ -use super::builder::Builder; +use super::sql_builder::Builder; use crate::{entities::workspace::WorkspaceTable, sqlx_ext::*}; use anyhow::Context; @@ -8,24 +8,13 @@ use flowy_net::{ }; use crate::{ - entities::workspace::AppTable, + entities::workspace::{AppTable, WORKSPACE_TABLE}, user_service::LoggedUser, - workspace_service::{ - app::make_app_from_table, - view::read_views_belong_to_id, - workspace::{check_workspace_id, make_workspace_from_table}, - }, + workspace_service::{view::read_views_belong_to_id, workspace::sql_builder::*}, }; use flowy_workspace::{ entities::workspace::parser::{WorkspaceDesc, WorkspaceId, WorkspaceName}, - protobuf::{ - App, - CreateWorkspaceParams, - RepeatedApp, - RepeatedView, - RepeatedWorkspace, - UpdateWorkspaceParams, - }, + protobuf::{App, CreateWorkspaceParams, RepeatedApp, RepeatedWorkspace, UpdateWorkspaceParams}, }; use sqlx::{postgres::PgArguments, PgPool, Postgres}; @@ -91,7 +80,7 @@ pub(crate) async fn update_workspace( .await .context("Failed to acquire a Postgres connection to update workspace")?; - let (sql, args) = SqlBuilder::update("workspace_table") + let (sql, args) = SqlBuilder::update(WORKSPACE_TABLE) .add_some_arg("name", name) .add_some_arg("description", desc) .and_where_eq("id", workspace_id) @@ -120,7 +109,7 @@ pub(crate) async fn delete_workspace( .await .context("Failed to acquire a Postgres connection to delete workspace")?; - let (sql, args) = SqlBuilder::delete("workspace_table") + let (sql, args) = SqlBuilder::delete(WORKSPACE_TABLE) .and_where_eq("id", workspace_id) .build()?; @@ -148,7 +137,7 @@ pub async fn read_workspaces( .await .context("Failed to acquire a Postgres connection to read workspace")?; - let mut builder = SqlBuilder::select("workspace_table") + let mut builder = SqlBuilder::select(WORKSPACE_TABLE) .add_field("*") .and_where_eq("user_id", &user_id); @@ -208,7 +197,7 @@ async fn read_apps_belong_to_workspace<'c>( let apps = tables .into_iter() - .map(|table| make_app_from_table(table, RepeatedView::default())) + .map(|table| table.into()) .collect::>(); let mut repeated_app = RepeatedApp::default(); diff --git a/rust-lib/flowy-document/src/entities/doc/mod.rs b/rust-lib/flowy-document/src/entities/doc/mod.rs index a12126ac61..91a3ad876c 100644 --- a/rust-lib/flowy-document/src/entities/doc/mod.rs +++ b/rust-lib/flowy-document/src/entities/doc/mod.rs @@ -1,7 +1,7 @@ mod doc_create; mod doc_modify; mod doc_query; -mod parser; +pub mod parser; pub use doc_create::*; pub use doc_modify::*; diff --git a/rust-lib/flowy-document/src/event.rs b/rust-lib/flowy-document/src/event.rs index e7f1fb71d7..eafec08fff 100644 --- a/rust-lib/flowy-document/src/event.rs +++ b/rust-lib/flowy-document/src/event.rs @@ -4,7 +4,7 @@ use strum_macros::Display; #[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)] #[event_err = "DocError"] pub enum EditorEvent { - #[event(input = "CreateDocRequest", output = "Doc")] + #[event(input = "CreateDocRequest")] CreateDoc = 0, #[event(input = "UpdateDocRequest")] diff --git a/rust-lib/flowy-document/src/handlers/doc_handler.rs b/rust-lib/flowy-document/src/handlers/doc_handler.rs index bf40fcc821..aee5853470 100644 --- a/rust-lib/flowy-document/src/handlers/doc_handler.rs +++ b/rust-lib/flowy-document/src/handlers/doc_handler.rs @@ -3,10 +3,10 @@ use flowy_dispatch::prelude::*; use std::convert::TryInto; #[tracing::instrument(skip(data, controller))] -pub async fn create_doc_handler(data: Data, controller: Unit) -> DataResult { +pub async fn create_doc_handler(data: Data, controller: Unit) -> Result<(), DocError> { let params: CreateDocParams = data.into_inner().try_into()?; - let doc_desc = controller.create_doc(params).await?; - data_result(doc_desc) + let _ = controller.create_doc(params).await?; + Ok(()) } #[tracing::instrument(skip(data, controller))] diff --git a/rust-lib/flowy-document/src/lib.rs b/rust-lib/flowy-document/src/lib.rs index aaff5840a6..83a88792f1 100644 --- a/rust-lib/flowy-document/src/lib.rs +++ b/rust-lib/flowy-document/src/lib.rs @@ -4,7 +4,7 @@ pub mod event; mod handlers; pub mod module; mod observable; -mod protobuf; +pub mod protobuf; mod services; mod sql_tables; diff --git a/rust-lib/flowy-document/src/observable/mod.rs b/rust-lib/flowy-document/src/observable/mod.rs index 73171fda02..a01ea122c4 100644 --- a/rust-lib/flowy-document/src/observable/mod.rs +++ b/rust-lib/flowy-document/src/observable/mod.rs @@ -1,3 +1 @@ mod observable; - -pub(crate) use observable::*; diff --git a/rust-lib/flowy-document/src/services/doc_controller.rs b/rust-lib/flowy-document/src/services/doc_controller.rs index 7cc95b2d41..287ec4cdad 100644 --- a/rust-lib/flowy-document/src/services/doc_controller.rs +++ b/rust-lib/flowy-document/src/services/doc_controller.rs @@ -25,14 +25,16 @@ impl DocController { } } - pub(crate) async fn create_doc(&self, params: CreateDocParams) -> Result { - let doc = self.create_doc_on_server(params).await?; - let doc_table = DocTable::new(doc.clone()); - + pub(crate) async fn create_doc(&self, params: CreateDocParams) -> Result<(), DocError> { + let _ = self.create_doc_on_server(params.clone()).await?; + let doc = Doc { + id: params.id, + data: params.data, + }; let conn = self.database.db_connection()?; - let _ = self.sql.create_doc_table(doc_table, &*conn)?; + let _ = self.sql.create_doc_table(DocTable::new(doc), &*conn)?; - Ok(doc) + Ok(()) } pub(crate) async fn update_doc(&self, params: UpdateDocParams) -> Result<(), DocError> { @@ -63,10 +65,10 @@ impl DocController { impl DocController { #[tracing::instrument(skip(self), err)] - async fn create_doc_on_server(&self, params: CreateDocParams) -> Result { + async fn create_doc_on_server(&self, params: CreateDocParams) -> Result<(), DocError> { let token = self.user.token()?; - let doc = self.server.create_doc(&token, params).await?; - Ok(doc) + let _ = self.server.create_doc(&token, params).await?; + Ok(()) } #[tracing::instrument(level = "debug", skip(self), err)] diff --git a/rust-lib/flowy-document/src/services/server/mod.rs b/rust-lib/flowy-document/src/services/server/mod.rs index 3076676060..02d1e46d9a 100644 --- a/rust-lib/flowy-document/src/services/server/mod.rs +++ b/rust-lib/flowy-document/src/services/server/mod.rs @@ -14,7 +14,7 @@ use std::sync::Arc; pub(crate) type Server = Arc; pub trait DocumentServerAPI { - fn create_doc(&self, token: &str, params: CreateDocParams) -> ResultFuture; + fn create_doc(&self, token: &str, params: CreateDocParams) -> ResultFuture<(), DocError>; fn read_doc(&self, token: &str, params: QueryDocParams) -> ResultFuture, DocError>; diff --git a/rust-lib/flowy-document/src/services/server/server_api.rs b/rust-lib/flowy-document/src/services/server/server_api.rs index e22b6902a2..2bfbe6c50f 100644 --- a/rust-lib/flowy-document/src/services/server/server_api.rs +++ b/rust-lib/flowy-document/src/services/server/server_api.rs @@ -9,7 +9,7 @@ use flowy_net::{config::*, request::HttpRequestBuilder}; pub struct DocServer {} impl DocumentServerAPI for DocServer { - fn create_doc(&self, token: &str, params: CreateDocParams) -> ResultFuture { + fn create_doc(&self, token: &str, params: CreateDocParams) -> ResultFuture<(), DocError> { let token = token.to_owned(); ResultFuture::new(async move { create_doc_request(&token, params, DOC_URL.as_ref()).await }) } @@ -32,14 +32,14 @@ impl DocumentServerAPI for DocServer { pub(crate) fn request_builder() -> HttpRequestBuilder { HttpRequestBuilder::new().middleware(super::middleware::MIDDLEWARE.clone()) } -pub async fn create_doc_request(token: &str, params: CreateDocParams, url: &str) -> Result { - let doc = request_builder() +pub async fn create_doc_request(token: &str, params: CreateDocParams, url: &str) -> Result<(), DocError> { + let _ = request_builder() .post(&url.to_owned()) .header(HEADER_TOKEN, token) .protobuf(params)? - .response() + .send() .await?; - Ok(doc) + Ok(()) } pub async fn read_doc_request(token: &str, params: QueryDocParams, url: &str) -> Result, DocError> { diff --git a/rust-lib/flowy-document/src/services/server/server_api_mock.rs b/rust-lib/flowy-document/src/services/server/server_api_mock.rs index 4463901c17..880d5c68e2 100644 --- a/rust-lib/flowy-document/src/services/server/server_api_mock.rs +++ b/rust-lib/flowy-document/src/services/server/server_api_mock.rs @@ -7,14 +7,7 @@ use flowy_infra::future::ResultFuture; pub struct DocServerMock {} impl DocumentServerAPI for DocServerMock { - fn create_doc(&self, _token: &str, params: CreateDocParams) -> ResultFuture { - let doc = Doc { - id: params.id, - data: params.data, - }; - - ResultFuture::new(async { Ok(doc) }) - } + fn create_doc(&self, _token: &str, _params: CreateDocParams) -> ResultFuture<(), DocError> { ResultFuture::new(async { Ok(()) }) } fn read_doc(&self, _token: &str, _params: QueryDocParams) -> ResultFuture, DocError> { ResultFuture::new(async { Ok(None) })