sql_builder.rs 3.4 KB

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