group.rs 5.0 KB

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