database_entities.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::{FieldIdPB, RowPB};
  3. use flowy_derive::ProtoBuf;
  4. use flowy_error::ErrorCode;
  5. /// [DatabasePB] describes how many fields and blocks the grid has
  6. #[derive(Debug, Clone, Default, ProtoBuf)]
  7. pub struct DatabasePB {
  8. #[pb(index = 1)]
  9. pub id: String,
  10. #[pb(index = 2)]
  11. pub fields: Vec<FieldIdPB>,
  12. #[pb(index = 3)]
  13. pub rows: Vec<RowPB>,
  14. }
  15. #[derive(ProtoBuf, Default)]
  16. pub struct CreateDatabasePayloadPB {
  17. #[pb(index = 1)]
  18. pub name: String,
  19. }
  20. #[derive(Clone, ProtoBuf, Default, Debug)]
  21. pub struct DatabaseViewIdPB {
  22. #[pb(index = 1)]
  23. pub value: String,
  24. }
  25. impl AsRef<str> for DatabaseViewIdPB {
  26. fn as_ref(&self) -> &str {
  27. &self.value
  28. }
  29. }
  30. #[derive(Debug, Clone, Default, ProtoBuf)]
  31. pub struct MoveFieldPayloadPB {
  32. #[pb(index = 1)]
  33. pub view_id: String,
  34. #[pb(index = 2)]
  35. pub field_id: String,
  36. #[pb(index = 3)]
  37. pub from_index: i32,
  38. #[pb(index = 4)]
  39. pub to_index: i32,
  40. }
  41. #[derive(Clone)]
  42. pub struct MoveFieldParams {
  43. pub view_id: String,
  44. pub field_id: String,
  45. pub from_index: i32,
  46. pub to_index: i32,
  47. }
  48. impl TryInto<MoveFieldParams> for MoveFieldPayloadPB {
  49. type Error = ErrorCode;
  50. fn try_into(self) -> Result<MoveFieldParams, Self::Error> {
  51. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?;
  52. let item_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::InvalidData)?;
  53. Ok(MoveFieldParams {
  54. view_id: view_id.0,
  55. field_id: item_id.0,
  56. from_index: self.from_index,
  57. to_index: self.to_index,
  58. })
  59. }
  60. }
  61. #[derive(Debug, Clone, Default, ProtoBuf)]
  62. pub struct MoveRowPayloadPB {
  63. #[pb(index = 1)]
  64. pub view_id: String,
  65. #[pb(index = 2)]
  66. pub from_row_id: String,
  67. #[pb(index = 4)]
  68. pub to_row_id: String,
  69. }
  70. pub struct MoveRowParams {
  71. pub view_id: String,
  72. pub from_row_id: String,
  73. pub to_row_id: String,
  74. }
  75. impl TryInto<MoveRowParams> for MoveRowPayloadPB {
  76. type Error = ErrorCode;
  77. fn try_into(self) -> Result<MoveRowParams, Self::Error> {
  78. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?;
  79. let from_row_id = NotEmptyStr::parse(self.from_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  80. let to_row_id = NotEmptyStr::parse(self.to_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  81. Ok(MoveRowParams {
  82. view_id: view_id.0,
  83. from_row_id: from_row_id.0,
  84. to_row_id: to_row_id.0,
  85. })
  86. }
  87. }
  88. #[derive(Debug, Clone, Default, ProtoBuf)]
  89. pub struct MoveGroupRowPayloadPB {
  90. #[pb(index = 1)]
  91. pub view_id: String,
  92. #[pb(index = 2)]
  93. pub from_row_id: String,
  94. #[pb(index = 3)]
  95. pub to_group_id: String,
  96. #[pb(index = 4, one_of)]
  97. pub to_row_id: Option<String>,
  98. }
  99. pub struct MoveGroupRowParams {
  100. pub view_id: String,
  101. pub from_row_id: String,
  102. pub to_group_id: String,
  103. pub to_row_id: Option<String>,
  104. }
  105. impl TryInto<MoveGroupRowParams> for MoveGroupRowPayloadPB {
  106. type Error = ErrorCode;
  107. fn try_into(self) -> Result<MoveGroupRowParams, Self::Error> {
  108. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?;
  109. let from_row_id = NotEmptyStr::parse(self.from_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  110. let to_group_id =
  111. NotEmptyStr::parse(self.to_group_id).map_err(|_| ErrorCode::GroupIdIsEmpty)?;
  112. let to_row_id = match self.to_row_id {
  113. None => None,
  114. Some(to_row_id) => Some(
  115. NotEmptyStr::parse(to_row_id)
  116. .map_err(|_| ErrorCode::RowIdIsEmpty)?
  117. .0,
  118. ),
  119. };
  120. Ok(MoveGroupRowParams {
  121. view_id: view_id.0,
  122. from_row_id: from_row_id.0,
  123. to_group_id: to_group_id.0,
  124. to_row_id,
  125. })
  126. }
  127. }
  128. #[derive(Debug, Default, ProtoBuf)]
  129. pub struct DatabaseDescPB {
  130. #[pb(index = 1)]
  131. pub name: String,
  132. #[pb(index = 2)]
  133. pub database_id: String,
  134. }
  135. #[derive(Debug, Default, ProtoBuf)]
  136. pub struct RepeatedDatabaseDescPB {
  137. #[pb(index = 1)]
  138. pub items: Vec<DatabaseDescPB>,
  139. }
  140. #[derive(Debug, Clone, Default, ProtoBuf)]
  141. pub struct DatabaseGroupIdPB {
  142. #[pb(index = 1)]
  143. pub view_id: String,
  144. #[pb(index = 2)]
  145. pub group_id: String,
  146. }
  147. pub struct DatabaseGroupIdParams {
  148. pub view_id: String,
  149. pub group_id: String,
  150. }
  151. impl TryInto<DatabaseGroupIdParams> for DatabaseGroupIdPB {
  152. type Error = ErrorCode;
  153. fn try_into(self) -> Result<DatabaseGroupIdParams, Self::Error> {
  154. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?;
  155. let group_id = NotEmptyStr::parse(self.group_id).map_err(|_| ErrorCode::GroupIdIsEmpty)?;
  156. Ok(DatabaseGroupIdParams {
  157. view_id: view_id.0,
  158. group_id: group_id.0,
  159. })
  160. }
  161. }