persistence.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use crate::util::sqlx_ext::SqlBuilder;
  2. use backend_service::errors::{invalid_params, ServerError};
  3. use chrono::{DateTime, NaiveDateTime, Utc};
  4. use flowy_folder_data_model::{parser::workspace::WorkspaceIdentify, protobuf::Workspace as WorkspacePB};
  5. use sqlx::postgres::PgArguments;
  6. use uuid::Uuid;
  7. pub struct NewWorkspaceBuilder {
  8. table: WorkspaceTable,
  9. }
  10. impl NewWorkspaceBuilder {
  11. pub fn new(user_id: &str) -> Self {
  12. let uuid = uuid::Uuid::new_v4();
  13. let time = Utc::now();
  14. let table = WorkspaceTable {
  15. id: uuid,
  16. name: "".to_string(),
  17. description: "".to_string(),
  18. modified_time: time,
  19. create_time: time,
  20. user_id: user_id.to_string(),
  21. };
  22. Self { table }
  23. }
  24. pub fn from_workspace(user_id: &str, workspace: WorkspacePB) -> Result<Self, ServerError> {
  25. let workspace_id = check_workspace_id(workspace.id)?;
  26. let create_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(workspace.create_time, 0), Utc);
  27. let modified_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(workspace.modified_time, 0), Utc);
  28. let table = WorkspaceTable {
  29. id: workspace_id,
  30. name: workspace.name,
  31. description: workspace.desc,
  32. modified_time,
  33. create_time,
  34. user_id: user_id.to_string(),
  35. };
  36. Ok(Self { table })
  37. }
  38. pub fn name(mut self, name: &str) -> Self {
  39. self.table.name = name.to_string();
  40. self
  41. }
  42. pub fn desc(mut self, desc: &str) -> Self {
  43. self.table.description = desc.to_owned();
  44. self
  45. }
  46. pub fn build(self) -> Result<(String, PgArguments, WorkspacePB), ServerError> {
  47. let workspace: WorkspacePB = self.table.clone().into();
  48. // TODO: use macro to fetch each field from struct
  49. let (sql, args) = SqlBuilder::create(WORKSPACE_TABLE)
  50. .add_field_with_arg("id", self.table.id)
  51. .add_field_with_arg("name", self.table.name)
  52. .add_field_with_arg("description", self.table.description)
  53. .add_field_with_arg("modified_time", self.table.modified_time)
  54. .add_field_with_arg("create_time", self.table.create_time)
  55. .add_field_with_arg("user_id", self.table.user_id)
  56. .build()?;
  57. Ok((sql, args, workspace))
  58. }
  59. }
  60. pub(crate) fn check_workspace_id(id: String) -> Result<Uuid, ServerError> {
  61. let workspace_id = WorkspaceIdentify::parse(id).map_err(invalid_params)?;
  62. let workspace_id = Uuid::parse_str(workspace_id.as_ref())?;
  63. Ok(workspace_id)
  64. }
  65. pub(crate) const WORKSPACE_TABLE: &str = "workspace_table";
  66. #[derive(Debug, Clone, sqlx::FromRow)]
  67. pub struct WorkspaceTable {
  68. pub(crate) id: uuid::Uuid,
  69. pub(crate) name: String,
  70. pub(crate) description: String,
  71. pub(crate) modified_time: chrono::DateTime<Utc>,
  72. pub(crate) create_time: chrono::DateTime<Utc>,
  73. pub(crate) user_id: String,
  74. }
  75. impl std::convert::From<WorkspaceTable> for WorkspacePB {
  76. fn from(table: WorkspaceTable) -> Self {
  77. let mut workspace = WorkspacePB::default();
  78. workspace.set_id(table.id.to_string());
  79. workspace.set_name(table.name.clone());
  80. workspace.set_desc(table.description.clone());
  81. workspace.set_modified_time(table.modified_time.timestamp());
  82. workspace.set_create_time(table.create_time.timestamp());
  83. workspace
  84. }
  85. }