sort_entities.rs 5.6 KB

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