row_entities.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. use std::collections::HashMap;
  2. use collab_database::rows::{Row, RowId, RowMeta};
  3. use collab_database::views::RowOrder;
  4. use flowy_derive::ProtoBuf;
  5. use flowy_error::ErrorCode;
  6. use crate::entities::parser::NotEmptyStr;
  7. use crate::services::database::{InsertedRow, UpdatedRow};
  8. /// [RowPB] Describes a row. Has the id of the parent Block. Has the metadata of the row.
  9. #[derive(Debug, Default, Clone, ProtoBuf, Eq, PartialEq)]
  10. pub struct RowPB {
  11. #[pb(index = 1)]
  12. pub id: String,
  13. #[pb(index = 2)]
  14. pub height: i32,
  15. }
  16. impl std::convert::From<&Row> for RowPB {
  17. fn from(row: &Row) -> Self {
  18. Self {
  19. id: row.id.clone().into_inner(),
  20. height: row.height,
  21. }
  22. }
  23. }
  24. impl std::convert::From<Row> for RowPB {
  25. fn from(row: Row) -> Self {
  26. Self {
  27. id: row.id.into_inner(),
  28. height: row.height,
  29. }
  30. }
  31. }
  32. impl From<RowOrder> for RowPB {
  33. fn from(data: RowOrder) -> Self {
  34. Self {
  35. id: data.id.into_inner(),
  36. height: data.height,
  37. }
  38. }
  39. }
  40. #[derive(Debug, Default, Clone, ProtoBuf)]
  41. pub struct RowMetaPB {
  42. #[pb(index = 1)]
  43. pub id: String,
  44. #[pb(index = 2)]
  45. pub document_id: String,
  46. #[pb(index = 3, one_of)]
  47. pub icon: Option<String>,
  48. #[pb(index = 4, one_of)]
  49. pub cover: Option<String>,
  50. }
  51. impl std::convert::From<&RowMeta> for RowMetaPB {
  52. fn from(row_meta: &RowMeta) -> Self {
  53. Self {
  54. id: row_meta.row_id.clone(),
  55. document_id: row_meta.document_id.clone(),
  56. icon: row_meta.icon_url.clone(),
  57. cover: row_meta.cover_url.clone(),
  58. }
  59. }
  60. }
  61. impl std::convert::From<RowMeta> for RowMetaPB {
  62. fn from(row_meta: RowMeta) -> Self {
  63. Self {
  64. id: row_meta.row_id,
  65. document_id: row_meta.document_id,
  66. icon: row_meta.icon_url,
  67. cover: row_meta.cover_url,
  68. }
  69. }
  70. }
  71. #[derive(Debug, Default, Clone, ProtoBuf)]
  72. pub struct UpdateRowMetaChangesetPB {
  73. #[pb(index = 1)]
  74. pub id: String,
  75. #[pb(index = 2)]
  76. pub view_id: String,
  77. #[pb(index = 3, one_of)]
  78. pub icon_url: Option<String>,
  79. #[pb(index = 4, one_of)]
  80. pub cover_url: Option<String>,
  81. }
  82. #[derive(Debug)]
  83. pub struct UpdateRowMetaParams {
  84. pub id: String,
  85. pub view_id: String,
  86. pub icon_url: Option<String>,
  87. pub cover_url: Option<String>,
  88. }
  89. impl TryInto<UpdateRowMetaParams> for UpdateRowMetaChangesetPB {
  90. type Error = ErrorCode;
  91. fn try_into(self) -> Result<UpdateRowMetaParams, Self::Error> {
  92. let row_id = NotEmptyStr::parse(self.id)
  93. .map_err(|_| ErrorCode::RowIdIsEmpty)?
  94. .0;
  95. let view_id = NotEmptyStr::parse(self.view_id)
  96. .map_err(|_| ErrorCode::ViewIdIsInvalid)?
  97. .0;
  98. Ok(UpdateRowMetaParams {
  99. id: row_id,
  100. view_id,
  101. icon_url: self.icon_url,
  102. cover_url: self.cover_url,
  103. })
  104. }
  105. }
  106. #[derive(Debug, Default, Clone, ProtoBuf)]
  107. pub struct UpdateRowPayloadPB {
  108. #[pb(index = 1)]
  109. pub row_id: String,
  110. #[pb(index = 2, one_of)]
  111. pub insert_document: Option<bool>,
  112. #[pb(index = 3, one_of)]
  113. pub insert_comment: Option<RowCommentPayloadPB>,
  114. }
  115. #[derive(Debug, Default, Clone)]
  116. pub struct UpdateRowParams {
  117. pub row_id: String,
  118. pub insert_comment: Option<RowCommentParams>,
  119. }
  120. impl TryInto<UpdateRowParams> for UpdateRowPayloadPB {
  121. type Error = ErrorCode;
  122. fn try_into(self) -> Result<UpdateRowParams, Self::Error> {
  123. let row_id = NotEmptyStr::parse(self.row_id)
  124. .map_err(|_| ErrorCode::RowIdIsEmpty)?
  125. .0;
  126. let insert_comment = self
  127. .insert_comment
  128. .map(|comment| comment.try_into())
  129. .transpose()?;
  130. Ok(UpdateRowParams {
  131. row_id,
  132. insert_comment,
  133. })
  134. }
  135. }
  136. #[derive(Debug, Default, Clone, ProtoBuf)]
  137. pub struct RowCommentPayloadPB {
  138. #[pb(index = 1)]
  139. pub uid: String,
  140. #[pb(index = 2)]
  141. pub comment: String,
  142. }
  143. #[derive(Debug, Default, Clone)]
  144. pub struct RowCommentParams {
  145. pub uid: String,
  146. pub comment: String,
  147. }
  148. impl TryInto<RowCommentParams> for RowCommentPayloadPB {
  149. type Error = ErrorCode;
  150. fn try_into(self) -> Result<RowCommentParams, Self::Error> {
  151. let uid = NotEmptyStr::parse(self.uid)
  152. .map_err(|_| ErrorCode::RowIdIsEmpty)?
  153. .0;
  154. let comment = NotEmptyStr::parse(self.comment)
  155. .map_err(|_| ErrorCode::RowIdIsEmpty)?
  156. .0;
  157. Ok(RowCommentParams { uid, comment })
  158. }
  159. }
  160. #[derive(Debug, Default, ProtoBuf)]
  161. pub struct OptionalRowPB {
  162. #[pb(index = 1, one_of)]
  163. pub row: Option<RowPB>,
  164. }
  165. #[derive(Debug, Default, ProtoBuf)]
  166. pub struct RepeatedRowPB {
  167. #[pb(index = 1)]
  168. pub items: Vec<RowPB>,
  169. }
  170. impl std::convert::From<Vec<RowPB>> for RepeatedRowPB {
  171. fn from(items: Vec<RowPB>) -> Self {
  172. Self { items }
  173. }
  174. }
  175. #[derive(Debug, Clone, Default, ProtoBuf)]
  176. pub struct InsertedRowPB {
  177. #[pb(index = 1)]
  178. pub row_meta: RowMetaPB,
  179. #[pb(index = 2, one_of)]
  180. pub index: Option<i32>,
  181. #[pb(index = 3)]
  182. pub is_new: bool,
  183. }
  184. impl InsertedRowPB {
  185. pub fn new(row_meta: RowMetaPB) -> Self {
  186. Self {
  187. row_meta,
  188. index: None,
  189. is_new: false,
  190. }
  191. }
  192. pub fn with_index(mut self, index: i32) -> Self {
  193. self.index = Some(index);
  194. self
  195. }
  196. }
  197. impl std::convert::From<RowMetaPB> for InsertedRowPB {
  198. fn from(row_meta: RowMetaPB) -> Self {
  199. Self {
  200. row_meta,
  201. index: None,
  202. is_new: false,
  203. }
  204. }
  205. }
  206. impl From<InsertedRow> for InsertedRowPB {
  207. fn from(data: InsertedRow) -> Self {
  208. Self {
  209. row_meta: data.row_meta.into(),
  210. index: data.index,
  211. is_new: data.is_new,
  212. }
  213. }
  214. }
  215. #[derive(Debug, Clone, Default, ProtoBuf)]
  216. pub struct UpdatedRowPB {
  217. #[pb(index = 1)]
  218. pub row_id: String,
  219. // Indicates the field ids of the cells that were updated in this row.
  220. #[pb(index = 2)]
  221. pub field_ids: Vec<String>,
  222. /// The meta of row was updated if this is Some.
  223. #[pb(index = 3, one_of)]
  224. pub row_meta: Option<RowMetaPB>,
  225. }
  226. impl From<UpdatedRow> for UpdatedRowPB {
  227. fn from(data: UpdatedRow) -> Self {
  228. let row_meta = data.row_meta.map(RowMetaPB::from);
  229. Self {
  230. row_id: data.row_id,
  231. field_ids: data.field_ids,
  232. row_meta,
  233. }
  234. }
  235. }
  236. #[derive(Debug, Default, Clone, ProtoBuf)]
  237. pub struct RowIdPB {
  238. #[pb(index = 1)]
  239. pub view_id: String,
  240. #[pb(index = 2)]
  241. pub row_id: String,
  242. #[pb(index = 3, one_of)]
  243. pub group_id: Option<String>,
  244. }
  245. pub struct RowIdParams {
  246. pub view_id: String,
  247. pub row_id: RowId,
  248. pub group_id: Option<String>,
  249. }
  250. impl TryInto<RowIdParams> for RowIdPB {
  251. type Error = ErrorCode;
  252. fn try_into(self) -> Result<RowIdParams, Self::Error> {
  253. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?;
  254. let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  255. let group_id = match self.group_id {
  256. Some(group_id) => Some(
  257. NotEmptyStr::parse(group_id)
  258. .map_err(|_| ErrorCode::GroupIdIsEmpty)?
  259. .0,
  260. ),
  261. None => None,
  262. };
  263. Ok(RowIdParams {
  264. view_id: view_id.0,
  265. row_id: RowId::from(row_id.0),
  266. group_id,
  267. })
  268. }
  269. }
  270. #[derive(Debug, Default, Clone, ProtoBuf)]
  271. pub struct BlockRowIdPB {
  272. #[pb(index = 1)]
  273. pub block_id: String,
  274. #[pb(index = 2)]
  275. pub row_id: String,
  276. }
  277. #[derive(ProtoBuf, Default)]
  278. pub struct CreateRowPayloadPB {
  279. #[pb(index = 1)]
  280. pub view_id: String,
  281. #[pb(index = 2, one_of)]
  282. pub start_row_id: Option<String>,
  283. #[pb(index = 3, one_of)]
  284. pub group_id: Option<String>,
  285. #[pb(index = 4, one_of)]
  286. pub data: Option<RowDataPB>,
  287. }
  288. #[derive(ProtoBuf, Default)]
  289. pub struct RowDataPB {
  290. #[pb(index = 1)]
  291. pub cell_data_by_field_id: HashMap<String, String>,
  292. }
  293. #[derive(Default)]
  294. pub struct CreateRowParams {
  295. pub view_id: String,
  296. pub start_row_id: Option<RowId>,
  297. pub group_id: Option<String>,
  298. pub cell_data_by_field_id: Option<HashMap<String, String>>,
  299. }
  300. impl TryInto<CreateRowParams> for CreateRowPayloadPB {
  301. type Error = ErrorCode;
  302. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  303. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::ViewIdIsInvalid)?;
  304. let start_row_id = self.start_row_id.map(RowId::from);
  305. Ok(CreateRowParams {
  306. view_id: view_id.0,
  307. start_row_id,
  308. group_id: self.group_id,
  309. cell_data_by_field_id: self.data.map(|data| data.cell_data_by_field_id),
  310. })
  311. }
  312. }