sort_entities.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::FieldType;
  3. use crate::services::sort::SortType;
  4. use std::sync::Arc;
  5. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  6. use flowy_error::ErrorCode;
  7. use grid_model::{FieldTypeRevision, SortCondition, SortRevision};
  8. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  9. pub struct SortPB {
  10. #[pb(index = 1)]
  11. pub id: String,
  12. #[pb(index = 2)]
  13. pub field_id: String,
  14. #[pb(index = 3)]
  15. pub field_type: FieldType,
  16. #[pb(index = 4)]
  17. pub condition: GridSortConditionPB,
  18. }
  19. impl std::convert::From<&SortRevision> for SortPB {
  20. fn from(sort_rev: &SortRevision) -> Self {
  21. Self {
  22. id: sort_rev.id.clone(),
  23. field_id: sort_rev.field_id.clone(),
  24. field_type: sort_rev.field_type.into(),
  25. condition: sort_rev.condition.clone().into(),
  26. }
  27. }
  28. }
  29. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  30. pub struct RepeatedSortPB {
  31. #[pb(index = 1)]
  32. pub items: Vec<SortPB>,
  33. }
  34. impl std::convert::From<Vec<Arc<SortRevision>>> for RepeatedSortPB {
  35. fn from(revs: Vec<Arc<SortRevision>>) -> Self {
  36. RepeatedSortPB {
  37. items: revs.into_iter().map(|rev| rev.as_ref().into()).collect(),
  38. }
  39. }
  40. }
  41. impl std::convert::From<Vec<SortPB>> for RepeatedSortPB {
  42. fn from(items: Vec<SortPB>) -> Self {
  43. Self { items }
  44. }
  45. }
  46. #[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum)]
  47. #[repr(u8)]
  48. pub enum GridSortConditionPB {
  49. Ascending = 0,
  50. Descending = 1,
  51. }
  52. impl std::default::Default for GridSortConditionPB {
  53. fn default() -> Self {
  54. Self::Ascending
  55. }
  56. }
  57. impl std::convert::From<SortCondition> for GridSortConditionPB {
  58. fn from(condition: SortCondition) -> Self {
  59. match condition {
  60. SortCondition::Ascending => GridSortConditionPB::Ascending,
  61. SortCondition::Descending => GridSortConditionPB::Descending,
  62. }
  63. }
  64. }
  65. #[derive(ProtoBuf, Debug, Default, Clone)]
  66. pub struct AlterSortPayloadPB {
  67. #[pb(index = 1)]
  68. pub view_id: String,
  69. #[pb(index = 2)]
  70. pub field_id: String,
  71. #[pb(index = 3)]
  72. pub field_type: FieldType,
  73. /// Create a new sort if the sort_id is None
  74. #[pb(index = 4, one_of)]
  75. pub sort_id: Option<String>,
  76. #[pb(index = 5)]
  77. pub condition: GridSortConditionPB,
  78. }
  79. impl TryInto<AlterSortParams> for AlterSortPayloadPB {
  80. type Error = ErrorCode;
  81. fn try_into(self) -> Result<AlterSortParams, Self::Error> {
  82. let view_id = NotEmptyStr::parse(self.view_id)
  83. .map_err(|_| ErrorCode::GridViewIdIsEmpty)?
  84. .0;
  85. let field_id = NotEmptyStr::parse(self.field_id)
  86. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  87. .0;
  88. let sort_id = match self.sort_id {
  89. None => None,
  90. Some(sort_id) => Some(NotEmptyStr::parse(sort_id).map_err(|_| ErrorCode::SortIdIsEmpty)?.0),
  91. };
  92. Ok(AlterSortParams {
  93. view_id,
  94. field_id,
  95. sort_id,
  96. field_type: self.field_type.into(),
  97. condition: self.condition as u8,
  98. })
  99. }
  100. }
  101. #[derive(Debug)]
  102. pub struct AlterSortParams {
  103. pub view_id: String,
  104. pub field_id: String,
  105. /// Create a new sort if the sort is None
  106. pub sort_id: Option<String>,
  107. pub field_type: FieldTypeRevision,
  108. pub condition: u8,
  109. }
  110. #[derive(ProtoBuf, Debug, Default, Clone)]
  111. pub struct DeleteSortPayloadPB {
  112. #[pb(index = 1)]
  113. pub view_id: String,
  114. #[pb(index = 2)]
  115. pub field_id: String,
  116. #[pb(index = 3)]
  117. pub field_type: FieldType,
  118. #[pb(index = 4)]
  119. pub sort_id: String,
  120. }
  121. impl TryInto<DeleteSortParams> for DeleteSortPayloadPB {
  122. type Error = ErrorCode;
  123. fn try_into(self) -> Result<DeleteSortParams, Self::Error> {
  124. let view_id = NotEmptyStr::parse(self.view_id)
  125. .map_err(|_| ErrorCode::GridViewIdIsEmpty)?
  126. .0;
  127. let field_id = NotEmptyStr::parse(self.field_id)
  128. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  129. .0;
  130. let sort_id = NotEmptyStr::parse(self.sort_id)
  131. .map_err(|_| ErrorCode::UnexpectedEmptyString)?
  132. .0;
  133. let sort_type = SortType {
  134. field_id,
  135. field_type: self.field_type,
  136. };
  137. Ok(DeleteSortParams {
  138. view_id,
  139. sort_type,
  140. sort_id,
  141. })
  142. }
  143. }
  144. #[derive(Debug, Clone)]
  145. pub struct DeleteSortParams {
  146. pub view_id: String,
  147. pub sort_type: SortType,
  148. pub sort_id: String,
  149. }
  150. #[derive(Debug, Default, ProtoBuf)]
  151. pub struct SortChangesetNotificationPB {
  152. #[pb(index = 1)]
  153. pub view_id: String,
  154. #[pb(index = 2)]
  155. pub insert_sorts: Vec<SortPB>,
  156. #[pb(index = 3)]
  157. pub delete_sorts: Vec<SortPB>,
  158. #[pb(index = 4)]
  159. pub update_sorts: Vec<SortPB>,
  160. }
  161. impl SortChangesetNotificationPB {
  162. pub fn new(view_id: String) -> Self {
  163. Self {
  164. view_id,
  165. insert_sorts: vec![],
  166. delete_sorts: vec![],
  167. update_sorts: vec![],
  168. }
  169. }
  170. pub fn extend(&mut self, other: SortChangesetNotificationPB) {
  171. self.insert_sorts.extend(other.insert_sorts);
  172. self.delete_sorts.extend(other.delete_sorts);
  173. self.update_sorts.extend(other.update_sorts);
  174. }
  175. pub fn is_empty(&self) -> bool {
  176. self.insert_sorts.is_empty() && self.delete_sorts.is_empty() && self.update_sorts.is_empty()
  177. }
  178. }
  179. #[derive(Debug, Default, ProtoBuf)]
  180. pub struct ReorderAllRowsPB {
  181. #[pb(index = 1)]
  182. pub row_orders: Vec<String>,
  183. }
  184. #[derive(Debug, Default, ProtoBuf)]
  185. pub struct ReorderSingleRowPB {
  186. #[pb(index = 1)]
  187. pub row_id: String,
  188. #[pb(index = 2)]
  189. pub old_index: i32,
  190. #[pb(index = 3)]
  191. pub new_index: i32,
  192. }