builder.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use crate::{entities::workspace::AppTable, sqlx_ext::SqlBuilder};
  2. use chrono::Utc;
  3. use flowy_net::errors::{invalid_params, ServerError};
  4. use flowy_workspace::{
  5. entities::app::parser::AppId,
  6. protobuf::{App, ColorStyle, RepeatedView},
  7. };
  8. use protobuf::Message;
  9. use sqlx::postgres::PgArguments;
  10. use uuid::Uuid;
  11. pub struct Builder {
  12. table: AppTable,
  13. }
  14. impl Builder {
  15. pub fn new(user_id: &str, workspace_id: &str) -> Self {
  16. let uuid = uuid::Uuid::new_v4();
  17. let time = Utc::now();
  18. let table = AppTable {
  19. id: uuid,
  20. workspace_id: workspace_id.to_string(),
  21. name: "".to_string(),
  22. description: "".to_string(),
  23. color_style: default_color_style(),
  24. last_view_id: "".to_string(),
  25. modified_time: time,
  26. create_time: time,
  27. user_id: user_id.to_string(),
  28. is_trash: false,
  29. };
  30. Self { table }
  31. }
  32. pub fn name(mut self, name: &str) -> Self {
  33. self.table.name = name.to_string();
  34. self
  35. }
  36. pub fn last_view_id(mut self, view_id: &str) -> Self {
  37. self.table.last_view_id = view_id.to_string();
  38. self
  39. }
  40. pub fn desc(mut self, desc: &str) -> Self {
  41. self.table.description = desc.to_owned();
  42. self
  43. }
  44. pub fn color_style(mut self, color_style: ColorStyle) -> Self {
  45. self.table.color_style = color_style
  46. .write_to_bytes()
  47. .unwrap_or(default_color_style());
  48. self
  49. }
  50. pub fn build(self) -> Result<(String, PgArguments, App), ServerError> {
  51. let app = make_app_from_table(self.table.clone(), RepeatedView::default());
  52. let (sql, args) = SqlBuilder::create("app_table")
  53. .add_arg("id", self.table.id)
  54. .add_arg("workspace_id", self.table.workspace_id)
  55. .add_arg("name", self.table.name)
  56. .add_arg("description", self.table.description)
  57. .add_arg("color_style", self.table.color_style)
  58. .add_arg("modified_time", self.table.modified_time)
  59. .add_arg("create_time", self.table.create_time)
  60. .add_arg("user_id", self.table.user_id)
  61. .build()?;
  62. Ok((sql, args, app))
  63. }
  64. }
  65. fn default_color_style() -> Vec<u8> {
  66. let style = ColorStyle::default();
  67. match style.write_to_bytes() {
  68. Ok(bytes) => bytes,
  69. Err(e) => {
  70. log::error!("Serialize color style failed: {:?}", e);
  71. vec![]
  72. },
  73. }
  74. }
  75. pub(crate) fn make_app_from_table(table: AppTable, views: RepeatedView) -> App {
  76. let mut app = App::default();
  77. app.set_id(table.id.to_string());
  78. app.set_workspace_id(table.workspace_id.to_string());
  79. app.set_name(table.name.clone());
  80. app.set_desc(table.description.clone());
  81. app.set_belongings(views);
  82. app
  83. }
  84. pub(crate) fn check_app_id(id: String) -> Result<Uuid, ServerError> {
  85. let app_id = AppId::parse(id).map_err(invalid_params)?;
  86. let app_id = Uuid::parse_str(app_id.as_ref())?;
  87. Ok(app_id)
  88. }