entities.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. use anyhow::bail;
  2. use collab::core::any_map::AnyMapExtension;
  3. use collab_database::database::gen_database_group_id;
  4. use collab_database::rows::{Row, RowId};
  5. use collab_database::views::{GroupMap, GroupMapBuilder, GroupSettingBuilder, GroupSettingMap};
  6. use serde::{Deserialize, Serialize};
  7. #[derive(Debug, Clone, Default)]
  8. pub struct GroupSetting {
  9. pub id: String,
  10. pub field_id: String,
  11. pub field_type: i64,
  12. pub groups: Vec<Group>,
  13. pub content: String,
  14. }
  15. pub struct GroupSettingChangeset {
  16. pub update_groups: Vec<GroupChangeset>,
  17. }
  18. pub struct GroupChangeset {
  19. pub group_id: String,
  20. pub name: Option<String>,
  21. pub visible: Option<bool>,
  22. }
  23. impl GroupSetting {
  24. pub fn new(field_id: String, field_type: i64, content: String) -> Self {
  25. Self {
  26. id: gen_database_group_id(),
  27. field_id,
  28. field_type,
  29. groups: vec![],
  30. content,
  31. }
  32. }
  33. }
  34. const GROUP_ID: &str = "id";
  35. const FIELD_ID: &str = "field_id";
  36. const FIELD_TYPE: &str = "ty";
  37. const GROUPS: &str = "groups";
  38. const CONTENT: &str = "content";
  39. impl TryFrom<GroupSettingMap> for GroupSetting {
  40. type Error = anyhow::Error;
  41. fn try_from(value: GroupSettingMap) -> Result<Self, Self::Error> {
  42. match (
  43. value.get_str_value(GROUP_ID),
  44. value.get_str_value(FIELD_ID),
  45. value.get_i64_value(FIELD_TYPE),
  46. ) {
  47. (Some(id), Some(field_id), Some(field_type)) => {
  48. let content = value.get_str_value(CONTENT).unwrap_or_default();
  49. let groups = value.try_get_array(GROUPS);
  50. Ok(Self {
  51. id,
  52. field_id,
  53. field_type,
  54. groups,
  55. content,
  56. })
  57. },
  58. _ => {
  59. bail!("Invalid group setting data")
  60. },
  61. }
  62. }
  63. }
  64. impl From<GroupSetting> for GroupSettingMap {
  65. fn from(setting: GroupSetting) -> Self {
  66. GroupSettingBuilder::new()
  67. .insert_str_value(GROUP_ID, setting.id)
  68. .insert_str_value(FIELD_ID, setting.field_id)
  69. .insert_i64_value(FIELD_TYPE, setting.field_type)
  70. .insert_maps(GROUPS, setting.groups)
  71. .insert_str_value(CONTENT, setting.content)
  72. .build()
  73. }
  74. }
  75. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  76. pub struct Group {
  77. pub id: String,
  78. pub name: String,
  79. #[serde(default = "GROUP_VISIBILITY")]
  80. pub visible: bool,
  81. }
  82. impl TryFrom<GroupMap> for Group {
  83. type Error = anyhow::Error;
  84. fn try_from(value: GroupMap) -> Result<Self, Self::Error> {
  85. match value.get_str_value("id") {
  86. None => bail!("Invalid group data"),
  87. Some(id) => {
  88. let name = value.get_str_value("name").unwrap_or_default();
  89. let visible = value.get_bool_value("visible").unwrap_or_default();
  90. Ok(Self { id, name, visible })
  91. },
  92. }
  93. }
  94. }
  95. impl From<Group> for GroupMap {
  96. fn from(group: Group) -> Self {
  97. GroupMapBuilder::new()
  98. .insert_str_value("id", group.id)
  99. .insert_str_value("name", group.name)
  100. .insert_bool_value("visible", group.visible)
  101. .build()
  102. }
  103. }
  104. const GROUP_VISIBILITY: fn() -> bool = || true;
  105. impl Group {
  106. pub fn new(id: String, name: String) -> Self {
  107. Self {
  108. id,
  109. name,
  110. visible: true,
  111. }
  112. }
  113. }
  114. #[derive(Clone, Debug)]
  115. pub struct GroupData {
  116. pub id: String,
  117. pub field_id: String,
  118. pub name: String,
  119. pub is_default: bool,
  120. pub is_visible: bool,
  121. pub(crate) rows: Vec<Row>,
  122. /// [filter_content] is used to determine which group the cell belongs to.
  123. pub filter_content: String,
  124. }
  125. impl GroupData {
  126. pub fn new(id: String, field_id: String, name: String, filter_content: String) -> Self {
  127. let is_default = id == field_id;
  128. Self {
  129. id,
  130. field_id,
  131. is_default,
  132. is_visible: true,
  133. name,
  134. rows: vec![],
  135. filter_content,
  136. }
  137. }
  138. pub fn contains_row(&self, row_id: &RowId) -> bool {
  139. self.rows.iter().any(|row| &row.id == row_id)
  140. }
  141. pub fn remove_row(&mut self, row_id: &RowId) {
  142. match self.rows.iter().position(|row| &row.id == row_id) {
  143. None => {},
  144. Some(pos) => {
  145. self.rows.remove(pos);
  146. },
  147. }
  148. }
  149. pub fn add_row(&mut self, row: Row) {
  150. match self.rows.iter().find(|r| r.id == row.id) {
  151. None => {
  152. self.rows.push(row);
  153. },
  154. Some(_) => {},
  155. }
  156. }
  157. pub fn insert_row(&mut self, index: usize, row: Row) {
  158. if index < self.rows.len() {
  159. self.rows.insert(index, row);
  160. } else {
  161. tracing::error!(
  162. "Insert row index:{} beyond the bounds:{},",
  163. index,
  164. self.rows.len()
  165. );
  166. }
  167. }
  168. pub fn index_of_row(&self, row_id: &RowId) -> Option<usize> {
  169. self.rows.iter().position(|row| &row.id == row_id)
  170. }
  171. pub fn number_of_row(&self) -> usize {
  172. self.rows.len()
  173. }
  174. pub fn is_empty(&self) -> bool {
  175. self.rows.is_empty()
  176. }
  177. }