persistence.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use crate::util::sqlx_ext::SqlBuilder;
  2. use backend_service::errors::{invalid_params, ServerError};
  3. use chrono::{DateTime, NaiveDateTime, Utc};
  4. use flowy_core_data_model::{
  5. parser::view::ViewIdentify,
  6. protobuf::{RepeatedView as RepeatedViewPB, View as ViewPB, ViewType as ViewTypePB},
  7. };
  8. use protobuf::ProtobufEnum;
  9. use sqlx::postgres::PgArguments;
  10. use uuid::Uuid;
  11. pub(crate) const VIEW_TABLE: &str = "view_table";
  12. pub struct NewViewSqlBuilder {
  13. table: ViewTable,
  14. }
  15. impl NewViewSqlBuilder {
  16. pub fn new(view_id: Uuid, belong_to_id: &str) -> Self {
  17. let time = Utc::now();
  18. let table = ViewTable {
  19. id: view_id,
  20. belong_to_id: belong_to_id.to_string(),
  21. name: "".to_string(),
  22. description: "".to_string(),
  23. modified_time: time,
  24. create_time: time,
  25. thumbnail: "".to_string(),
  26. view_type: ViewTypePB::Doc.value(),
  27. };
  28. Self { table }
  29. }
  30. pub fn from_view(view: ViewPB) -> Result<Self, ServerError> {
  31. let view_id = ViewIdentify::parse(view.id).map_err(invalid_params)?;
  32. let view_id = Uuid::parse_str(view_id.as_ref())?;
  33. let create_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(view.create_time, 0), Utc);
  34. let modified_time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(view.modified_time, 0), Utc);
  35. let table = ViewTable {
  36. id: view_id,
  37. belong_to_id: view.belong_to_id,
  38. name: view.name,
  39. description: view.desc,
  40. modified_time,
  41. create_time,
  42. thumbnail: "".to_string(),
  43. view_type: view.view_type.value(),
  44. };
  45. Ok(Self { table })
  46. }
  47. pub fn name(mut self, name: &str) -> Self {
  48. self.table.name = name.to_string();
  49. self
  50. }
  51. pub fn desc(mut self, desc: &str) -> Self {
  52. self.table.description = desc.to_owned();
  53. self
  54. }
  55. pub fn thumbnail(mut self, thumbnail: &str) -> Self {
  56. self.table.thumbnail = thumbnail.to_owned();
  57. self
  58. }
  59. pub fn view_type(mut self, view_type: ViewTypePB) -> Self {
  60. self.table.view_type = view_type.value();
  61. self
  62. }
  63. pub fn build(self) -> Result<(String, PgArguments, ViewPB), ServerError> {
  64. let view: ViewPB = self.table.clone().into();
  65. let (sql, args) = SqlBuilder::create(VIEW_TABLE)
  66. .add_field_with_arg("id", self.table.id)
  67. .add_field_with_arg("belong_to_id", self.table.belong_to_id)
  68. .add_field_with_arg("name", self.table.name)
  69. .add_field_with_arg("description", self.table.description)
  70. .add_field_with_arg("modified_time", self.table.modified_time)
  71. .add_field_with_arg("create_time", self.table.create_time)
  72. .add_field_with_arg("thumbnail", self.table.thumbnail)
  73. .add_field_with_arg("view_type", self.table.view_type)
  74. .build()?;
  75. Ok((sql, args, view))
  76. }
  77. }
  78. pub(crate) fn check_view_ids(ids: Vec<String>) -> Result<Vec<Uuid>, ServerError> {
  79. let mut view_ids = vec![];
  80. for id in ids {
  81. view_ids.push(check_view_id(id)?);
  82. }
  83. Ok(view_ids)
  84. }
  85. pub(crate) fn check_view_id(id: String) -> Result<Uuid, ServerError> {
  86. let view_id = ViewIdentify::parse(id).map_err(invalid_params)?;
  87. let view_id = Uuid::parse_str(view_id.as_ref())?;
  88. Ok(view_id)
  89. }
  90. #[derive(Debug, Clone, sqlx::FromRow)]
  91. pub struct ViewTable {
  92. pub(crate) id: uuid::Uuid,
  93. pub(crate) belong_to_id: String,
  94. pub(crate) name: String,
  95. pub(crate) description: String,
  96. pub(crate) modified_time: chrono::DateTime<Utc>,
  97. pub(crate) create_time: chrono::DateTime<Utc>,
  98. pub(crate) thumbnail: String,
  99. pub(crate) view_type: i32,
  100. }
  101. impl std::convert::From<ViewTable> for ViewPB {
  102. fn from(table: ViewTable) -> Self {
  103. let view_type = ViewTypePB::from_i32(table.view_type).unwrap_or(ViewTypePB::Doc);
  104. let mut view = ViewPB::default();
  105. view.set_id(table.id.to_string());
  106. view.set_belong_to_id(table.belong_to_id);
  107. view.set_name(table.name);
  108. view.set_desc(table.description);
  109. view.set_view_type(view_type);
  110. view.set_belongings(RepeatedViewPB::default());
  111. view.set_create_time(table.create_time.timestamp());
  112. view.set_modified_time(table.modified_time.timestamp());
  113. view
  114. }
  115. }