workspace.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. use super::builder::Builder;
  2. use crate::{
  3. entities::workspace::WorkspaceTable,
  4. sqlx_ext::*,
  5. workspace_service::app::app::read_apps_belong_to_workspace,
  6. };
  7. use anyhow::Context;
  8. use flowy_net::{
  9. errors::{invalid_params, ServerError},
  10. response::FlowyResponse,
  11. };
  12. use flowy_user::entities::parser::UserId;
  13. use crate::{
  14. user_service::LoggedUser,
  15. workspace_service::workspace::{check_workspace_id, make_workspace_from_table},
  16. };
  17. use flowy_workspace::{
  18. entities::workspace::parser::{WorkspaceDesc, WorkspaceName},
  19. protobuf::{CreateWorkspaceParams, RepeatedApp, RepeatedWorkspace, UpdateWorkspaceParams},
  20. };
  21. use sqlx::{postgres::PgArguments, PgPool, Postgres};
  22. pub(crate) async fn create_workspace(
  23. pool: &PgPool,
  24. params: CreateWorkspaceParams,
  25. logged_user: LoggedUser,
  26. ) -> Result<FlowyResponse, ServerError> {
  27. let name = WorkspaceName::parse(params.get_name().to_owned()).map_err(invalid_params)?;
  28. let desc = WorkspaceDesc::parse(params.get_desc().to_owned()).map_err(invalid_params)?;
  29. let user_id = logged_user.get_user_id()?.to_string();
  30. let mut transaction = pool
  31. .begin()
  32. .await
  33. .context("Failed to acquire a Postgres connection to create workspace")?;
  34. let (sql, args, workspace) = Builder::new(&user_id)
  35. .name(name.as_ref())
  36. .desc(desc.as_ref())
  37. .build()?;
  38. let _ = sqlx::query_with(&sql, args)
  39. .execute(&mut transaction)
  40. .await
  41. .map_err(map_sqlx_error)?;
  42. transaction
  43. .commit()
  44. .await
  45. .context("Failed to commit SQL transaction to create workspace.")?;
  46. FlowyResponse::success().pb(workspace)
  47. }
  48. pub(crate) async fn update_workspace(
  49. pool: &PgPool,
  50. params: UpdateWorkspaceParams,
  51. ) -> Result<FlowyResponse, ServerError> {
  52. let workspace_id = check_workspace_id(params.get_id().to_owned())?;
  53. let name = match params.has_name() {
  54. false => None,
  55. true => {
  56. let name = WorkspaceName::parse(params.get_name().to_owned())
  57. .map_err(invalid_params)?
  58. .0;
  59. Some(name)
  60. },
  61. };
  62. let desc = match params.has_desc() {
  63. false => None,
  64. true => {
  65. let desc = WorkspaceDesc::parse(params.get_desc().to_owned())
  66. .map_err(invalid_params)?
  67. .0;
  68. Some(desc)
  69. },
  70. };
  71. let mut transaction = pool
  72. .begin()
  73. .await
  74. .context("Failed to acquire a Postgres connection to update workspace")?;
  75. let (sql, args) = SqlBuilder::update("workspace_table")
  76. .add_some_arg("name", name)
  77. .add_some_arg("description", desc)
  78. .and_where_eq("id", workspace_id)
  79. .build()?;
  80. sqlx::query_with(&sql, args)
  81. .execute(&mut transaction)
  82. .await
  83. .map_err(map_sqlx_error)?;
  84. transaction
  85. .commit()
  86. .await
  87. .context("Failed to commit SQL transaction to update workspace.")?;
  88. Ok(FlowyResponse::success())
  89. }
  90. pub(crate) async fn delete_workspace(
  91. pool: &PgPool,
  92. workspace_id: &str,
  93. ) -> Result<FlowyResponse, ServerError> {
  94. let workspace_id = check_workspace_id(workspace_id.to_owned())?;
  95. let mut transaction = pool
  96. .begin()
  97. .await
  98. .context("Failed to acquire a Postgres connection to delete workspace")?;
  99. let (sql, args) = SqlBuilder::delete("workspace_table")
  100. .and_where_eq("id", workspace_id)
  101. .build()?;
  102. let _ = sqlx::query_with(&sql, args)
  103. .execute(&mut transaction)
  104. .await
  105. .map_err(map_sqlx_error)?;
  106. transaction
  107. .commit()
  108. .await
  109. .context("Failed to commit SQL transaction to delete workspace.")?;
  110. Ok(FlowyResponse::success())
  111. }
  112. pub async fn read_workspaces(
  113. pool: &PgPool,
  114. workspace_id: Option<String>,
  115. logged_user: LoggedUser,
  116. ) -> Result<FlowyResponse, ServerError> {
  117. let user_id = logged_user.get_user_id()?.to_string();
  118. let mut transaction = pool
  119. .begin()
  120. .await
  121. .context("Failed to acquire a Postgres connection to read workspace")?;
  122. let mut builder = SqlBuilder::select("workspace_table")
  123. .add_field("*")
  124. .and_where_eq("user_id", &user_id);
  125. if let Some(workspace_id) = workspace_id {
  126. let workspace_id = check_workspace_id(workspace_id)?;
  127. builder = builder.and_where_eq("id", workspace_id);
  128. }
  129. let (sql, args) = builder.build()?;
  130. let tables = sqlx::query_as_with::<Postgres, WorkspaceTable, PgArguments>(&sql, args)
  131. .fetch_all(&mut transaction)
  132. .await
  133. .map_err(map_sqlx_error)?;
  134. let mut repeated_workspace = RepeatedWorkspace::default();
  135. let mut workspaces = vec![];
  136. for table in tables {
  137. let apps = read_apps_belong_to_workspace(&mut transaction, &table.id.to_string())
  138. .await
  139. .context("Get workspace app")
  140. .unwrap_or(RepeatedApp::default());
  141. let workspace = make_workspace_from_table(table, Some(apps));
  142. workspaces.push(workspace);
  143. }
  144. transaction
  145. .commit()
  146. .await
  147. .context("Failed to commit SQL transaction to read workspace.")?;
  148. repeated_workspace.set_items(workspaces.into());
  149. FlowyResponse::success().pb(repeated_workspace)
  150. }