group.rs 4.6 KB

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