group.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::{FieldType, RowPB};
  3. use crate::services::group::Group;
  4. use database_model::{FieldTypeRevision, GroupConfigurationRevision};
  5. use flowy_derive::ProtoBuf;
  6. use flowy_error::ErrorCode;
  7. use std::convert::TryInto;
  8. use std::sync::Arc;
  9. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  10. pub struct GroupConfigurationPB {
  11. #[pb(index = 1)]
  12. pub id: String,
  13. #[pb(index = 2)]
  14. pub field_id: String,
  15. }
  16. impl std::convert::From<&GroupConfigurationRevision> for GroupConfigurationPB {
  17. fn from(rev: &GroupConfigurationRevision) -> Self {
  18. GroupConfigurationPB {
  19. id: rev.id.clone(),
  20. field_id: rev.field_id.clone(),
  21. }
  22. }
  23. }
  24. #[derive(ProtoBuf, Debug, Default, Clone)]
  25. pub struct RepeatedGroupPB {
  26. #[pb(index = 1)]
  27. pub items: Vec<GroupPB>,
  28. }
  29. impl std::ops::Deref for RepeatedGroupPB {
  30. type Target = Vec<GroupPB>;
  31. fn deref(&self) -> &Self::Target {
  32. &self.items
  33. }
  34. }
  35. impl std::ops::DerefMut for RepeatedGroupPB {
  36. fn deref_mut(&mut self) -> &mut Self::Target {
  37. &mut self.items
  38. }
  39. }
  40. #[derive(ProtoBuf, Debug, Default, Clone)]
  41. pub struct GroupPB {
  42. #[pb(index = 1)]
  43. pub field_id: String,
  44. #[pb(index = 2)]
  45. pub group_id: String,
  46. #[pb(index = 3)]
  47. pub desc: String,
  48. #[pb(index = 4)]
  49. pub rows: Vec<RowPB>,
  50. #[pb(index = 5)]
  51. pub is_default: bool,
  52. #[pb(index = 6)]
  53. pub is_visible: bool,
  54. }
  55. impl std::convert::From<Group> for GroupPB {
  56. fn from(group: Group) -> Self {
  57. Self {
  58. field_id: group.field_id,
  59. group_id: group.id,
  60. desc: group.name,
  61. rows: group.rows,
  62. is_default: group.is_default,
  63. is_visible: group.is_visible,
  64. }
  65. }
  66. }
  67. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  68. pub struct RepeatedGroupConfigurationPB {
  69. #[pb(index = 1)]
  70. pub items: Vec<GroupConfigurationPB>,
  71. }
  72. impl std::convert::From<Vec<GroupConfigurationPB>> for RepeatedGroupConfigurationPB {
  73. fn from(items: Vec<GroupConfigurationPB>) -> Self {
  74. Self { items }
  75. }
  76. }
  77. impl std::convert::From<Vec<Arc<GroupConfigurationRevision>>> for RepeatedGroupConfigurationPB {
  78. fn from(revs: Vec<Arc<GroupConfigurationRevision>>) -> Self {
  79. RepeatedGroupConfigurationPB {
  80. items: revs.iter().map(|rev| rev.as_ref().into()).collect(),
  81. }
  82. }
  83. }
  84. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  85. pub struct InsertGroupPayloadPB {
  86. #[pb(index = 1)]
  87. pub field_id: String,
  88. #[pb(index = 2)]
  89. pub field_type: FieldType,
  90. #[pb(index = 3)]
  91. pub view_id: String,
  92. }
  93. impl TryInto<InsertGroupParams> for InsertGroupPayloadPB {
  94. type Error = ErrorCode;
  95. fn try_into(self) -> Result<InsertGroupParams, Self::Error> {
  96. let field_id = NotEmptyStr::parse(self.field_id)
  97. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  98. .0;
  99. let view_id = NotEmptyStr::parse(self.view_id)
  100. .map_err(|_| ErrorCode::ViewIdIsInvalid)?
  101. .0;
  102. Ok(InsertGroupParams {
  103. field_id,
  104. field_type_rev: self.field_type.into(),
  105. view_id,
  106. })
  107. }
  108. }
  109. pub struct InsertGroupParams {
  110. pub view_id: String,
  111. pub field_id: String,
  112. pub field_type_rev: FieldTypeRevision,
  113. }
  114. #[derive(ProtoBuf, Debug, Default, Clone)]
  115. pub struct DeleteGroupPayloadPB {
  116. #[pb(index = 1)]
  117. pub field_id: String,
  118. #[pb(index = 2)]
  119. pub group_id: String,
  120. #[pb(index = 3)]
  121. pub field_type: FieldType,
  122. #[pb(index = 4)]
  123. pub view_id: String,
  124. }
  125. impl TryInto<DeleteGroupParams> for DeleteGroupPayloadPB {
  126. type Error = ErrorCode;
  127. fn try_into(self) -> Result<DeleteGroupParams, Self::Error> {
  128. let field_id = NotEmptyStr::parse(self.field_id)
  129. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  130. .0;
  131. let group_id = NotEmptyStr::parse(self.group_id)
  132. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  133. .0;
  134. let view_id = NotEmptyStr::parse(self.view_id)
  135. .map_err(|_| ErrorCode::ViewIdIsInvalid)?
  136. .0;
  137. Ok(DeleteGroupParams {
  138. field_id,
  139. field_type_rev: self.field_type.into(),
  140. group_id,
  141. view_id,
  142. })
  143. }
  144. }
  145. pub struct DeleteGroupParams {
  146. pub view_id: String,
  147. pub field_id: String,
  148. pub group_id: String,
  149. pub field_type_rev: FieldTypeRevision,
  150. }