view.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. use crate::{
  2. entities::trash::{Trash, TrashType},
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::AppIdentify,
  7. view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail},
  8. },
  9. };
  10. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  11. use serde::{Deserialize, Serialize};
  12. use std::convert::TryInto;
  13. #[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
  14. pub struct View {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub belong_to_id: String,
  19. #[pb(index = 3)]
  20. pub name: String,
  21. #[pb(index = 4)]
  22. pub desc: String,
  23. #[pb(index = 5)]
  24. pub view_type: ViewType,
  25. #[pb(index = 6)]
  26. pub version: i64,
  27. #[pb(index = 7)]
  28. pub belongings: RepeatedView,
  29. #[pb(index = 8)]
  30. pub modified_time: i64,
  31. #[pb(index = 9)]
  32. pub create_time: i64,
  33. }
  34. #[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone, Serialize, Deserialize)]
  35. #[serde(transparent)]
  36. pub struct RepeatedView {
  37. #[pb(index = 1)]
  38. pub items: Vec<View>,
  39. }
  40. impl_def_and_def_mut!(RepeatedView, View);
  41. impl std::convert::From<View> for Trash {
  42. fn from(view: View) -> Self {
  43. Trash {
  44. id: view.id,
  45. name: view.name,
  46. modified_time: view.modified_time,
  47. create_time: view.create_time,
  48. ty: TrashType::TrashView,
  49. }
  50. }
  51. }
  52. #[derive(Eq, PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
  53. pub enum ViewType {
  54. Blank = 0,
  55. QuillDocument = 1,
  56. Kanban = 2,
  57. }
  58. impl std::default::Default for ViewType {
  59. fn default() -> Self {
  60. ViewType::Blank
  61. }
  62. }
  63. impl std::convert::From<i32> for ViewType {
  64. fn from(val: i32) -> Self {
  65. match val {
  66. 0 => ViewType::Blank,
  67. 1 => ViewType::QuillDocument,
  68. 2 => ViewType::Kanban,
  69. _ => {
  70. log::error!("Invalid view type: {}", val);
  71. ViewType::Blank
  72. }
  73. }
  74. }
  75. }
  76. #[derive(Default, ProtoBuf)]
  77. pub struct CreateViewPayload {
  78. #[pb(index = 1)]
  79. pub belong_to_id: String,
  80. #[pb(index = 2)]
  81. pub name: String,
  82. #[pb(index = 3)]
  83. pub desc: String,
  84. #[pb(index = 4, one_of)]
  85. pub thumbnail: Option<String>,
  86. #[pb(index = 5)]
  87. pub view_type: ViewType,
  88. }
  89. #[derive(Default, ProtoBuf, Debug, Clone)]
  90. pub struct CreateViewParams {
  91. #[pb(index = 1)]
  92. pub belong_to_id: String,
  93. #[pb(index = 2)]
  94. pub name: String,
  95. #[pb(index = 3)]
  96. pub desc: String,
  97. #[pb(index = 4)]
  98. pub thumbnail: String,
  99. #[pb(index = 5)]
  100. pub view_type: ViewType,
  101. // ViewType::Doc -> Delta string
  102. #[pb(index = 6)]
  103. pub view_data: String,
  104. #[pb(index = 7)]
  105. pub view_id: String,
  106. }
  107. impl CreateViewParams {
  108. pub fn new(
  109. belong_to_id: String,
  110. name: String,
  111. desc: String,
  112. view_type: ViewType,
  113. thumbnail: String,
  114. view_data: String,
  115. view_id: String,
  116. ) -> Self {
  117. Self {
  118. belong_to_id,
  119. name,
  120. desc,
  121. thumbnail,
  122. view_type,
  123. view_data,
  124. view_id,
  125. }
  126. }
  127. }
  128. impl TryInto<CreateViewParams> for CreateViewPayload {
  129. type Error = ErrorCode;
  130. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  131. let name = ViewName::parse(self.name)?.0;
  132. let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
  133. let view_data = "".to_string();
  134. let view_id = uuid::Uuid::new_v4().to_string();
  135. let thumbnail = match self.thumbnail {
  136. None => "".to_string(),
  137. Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
  138. };
  139. Ok(CreateViewParams::new(
  140. belong_to_id,
  141. name,
  142. self.desc,
  143. self.view_type,
  144. thumbnail,
  145. view_data,
  146. view_id,
  147. ))
  148. }
  149. }
  150. #[derive(Default, ProtoBuf, Clone, Debug)]
  151. pub struct ViewId {
  152. #[pb(index = 1)]
  153. pub value: String,
  154. }
  155. impl std::convert::From<&str> for ViewId {
  156. fn from(value: &str) -> Self {
  157. ViewId {
  158. value: value.to_string(),
  159. }
  160. }
  161. }
  162. #[derive(Default, ProtoBuf)]
  163. pub struct RepeatedViewId {
  164. #[pb(index = 1)]
  165. pub items: Vec<String>,
  166. }
  167. #[derive(Default, ProtoBuf)]
  168. pub struct UpdateViewPayload {
  169. #[pb(index = 1)]
  170. pub view_id: String,
  171. #[pb(index = 2, one_of)]
  172. pub name: Option<String>,
  173. #[pb(index = 3, one_of)]
  174. pub desc: Option<String>,
  175. #[pb(index = 4, one_of)]
  176. pub thumbnail: Option<String>,
  177. }
  178. #[derive(Default, ProtoBuf, Clone, Debug)]
  179. pub struct UpdateViewParams {
  180. #[pb(index = 1)]
  181. pub view_id: String,
  182. #[pb(index = 2, one_of)]
  183. pub name: Option<String>,
  184. #[pb(index = 3, one_of)]
  185. pub desc: Option<String>,
  186. #[pb(index = 4, one_of)]
  187. pub thumbnail: Option<String>,
  188. }
  189. impl UpdateViewParams {
  190. pub fn new(view_id: &str) -> Self {
  191. Self {
  192. view_id: view_id.to_owned(),
  193. ..Default::default()
  194. }
  195. }
  196. pub fn name(mut self, name: &str) -> Self {
  197. self.name = Some(name.to_owned());
  198. self
  199. }
  200. pub fn desc(mut self, desc: &str) -> Self {
  201. self.desc = Some(desc.to_owned());
  202. self
  203. }
  204. }
  205. impl TryInto<UpdateViewParams> for UpdateViewPayload {
  206. type Error = ErrorCode;
  207. fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
  208. let view_id = ViewIdentify::parse(self.view_id)?.0;
  209. let name = match self.name {
  210. None => None,
  211. Some(name) => Some(ViewName::parse(name)?.0),
  212. };
  213. let desc = match self.desc {
  214. None => None,
  215. Some(desc) => Some(ViewDesc::parse(desc)?.0),
  216. };
  217. let thumbnail = match self.thumbnail {
  218. None => None,
  219. Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
  220. };
  221. Ok(UpdateViewParams {
  222. view_id,
  223. name,
  224. desc,
  225. thumbnail,
  226. })
  227. }
  228. }