app.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. use crate::{
  2. entities::view::RepeatedView,
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::{AppColorStyle, AppIdentify, AppName},
  7. workspace::WorkspaceIdentify,
  8. },
  9. };
  10. use flowy_derive::ProtoBuf;
  11. use serde::{Deserialize, Serialize};
  12. use std::convert::TryInto;
  13. #[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
  14. pub struct App {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub workspace_id: String,
  19. #[pb(index = 3)]
  20. pub name: String,
  21. #[pb(index = 4)]
  22. pub desc: String,
  23. #[pb(index = 5)]
  24. pub belongings: RepeatedView,
  25. #[pb(index = 6)]
  26. pub version: i64,
  27. #[pb(index = 7)]
  28. pub modified_time: i64,
  29. #[pb(index = 8)]
  30. pub create_time: i64,
  31. }
  32. #[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone, Serialize, Deserialize)]
  33. #[serde(transparent)]
  34. pub struct RepeatedApp {
  35. #[pb(index = 1)]
  36. pub items: Vec<App>,
  37. }
  38. impl_def_and_def_mut!(RepeatedApp, App);
  39. #[derive(ProtoBuf, Default)]
  40. pub struct CreateAppPayload {
  41. #[pb(index = 1)]
  42. pub workspace_id: String,
  43. #[pb(index = 2)]
  44. pub name: String,
  45. #[pb(index = 3)]
  46. pub desc: String,
  47. #[pb(index = 4)]
  48. pub color_style: ColorStyle,
  49. }
  50. #[derive(ProtoBuf, Default, Debug, Clone)]
  51. pub struct ColorStyle {
  52. #[pb(index = 1)]
  53. pub theme_color: String,
  54. }
  55. #[derive(ProtoBuf, Default, Debug)]
  56. pub struct CreateAppParams {
  57. #[pb(index = 1)]
  58. pub workspace_id: String,
  59. #[pb(index = 2)]
  60. pub name: String,
  61. #[pb(index = 3)]
  62. pub desc: String,
  63. #[pb(index = 4)]
  64. pub color_style: ColorStyle,
  65. }
  66. impl TryInto<CreateAppParams> for CreateAppPayload {
  67. type Error = ErrorCode;
  68. fn try_into(self) -> Result<CreateAppParams, Self::Error> {
  69. let name = AppName::parse(self.name)?;
  70. let id = WorkspaceIdentify::parse(self.workspace_id)?;
  71. let color_style = AppColorStyle::parse(self.color_style.theme_color.clone())?;
  72. Ok(CreateAppParams {
  73. workspace_id: id.0,
  74. name: name.0,
  75. desc: self.desc,
  76. color_style: color_style.into(),
  77. })
  78. }
  79. }
  80. impl std::convert::From<AppColorStyle> for ColorStyle {
  81. fn from(data: AppColorStyle) -> Self {
  82. ColorStyle {
  83. theme_color: data.theme_color,
  84. }
  85. }
  86. }
  87. #[derive(ProtoBuf, Default, Clone, Debug)]
  88. pub struct AppId {
  89. #[pb(index = 1)]
  90. pub value: String,
  91. }
  92. impl AppId {
  93. pub fn new(app_id: &str) -> Self {
  94. Self {
  95. value: app_id.to_string(),
  96. }
  97. }
  98. }
  99. #[derive(ProtoBuf, Default)]
  100. pub struct UpdateAppPayload {
  101. #[pb(index = 1)]
  102. pub app_id: String,
  103. #[pb(index = 2, one_of)]
  104. pub name: Option<String>,
  105. #[pb(index = 3, one_of)]
  106. pub desc: Option<String>,
  107. #[pb(index = 4, one_of)]
  108. pub color_style: Option<ColorStyle>,
  109. #[pb(index = 5, one_of)]
  110. pub is_trash: Option<bool>,
  111. }
  112. #[derive(ProtoBuf, Default, Clone, Debug)]
  113. pub struct UpdateAppParams {
  114. #[pb(index = 1)]
  115. pub app_id: String,
  116. #[pb(index = 2, one_of)]
  117. pub name: Option<String>,
  118. #[pb(index = 3, one_of)]
  119. pub desc: Option<String>,
  120. #[pb(index = 4, one_of)]
  121. pub color_style: Option<ColorStyle>,
  122. #[pb(index = 5, one_of)]
  123. pub is_trash: Option<bool>,
  124. }
  125. impl UpdateAppParams {
  126. pub fn new(app_id: &str) -> Self {
  127. Self {
  128. app_id: app_id.to_string(),
  129. ..Default::default()
  130. }
  131. }
  132. pub fn name(mut self, name: &str) -> Self {
  133. self.name = Some(name.to_string());
  134. self
  135. }
  136. pub fn desc(mut self, desc: &str) -> Self {
  137. self.desc = Some(desc.to_string());
  138. self
  139. }
  140. pub fn trash(mut self) -> Self {
  141. self.is_trash = Some(true);
  142. self
  143. }
  144. }
  145. impl TryInto<UpdateAppParams> for UpdateAppPayload {
  146. type Error = ErrorCode;
  147. fn try_into(self) -> Result<UpdateAppParams, Self::Error> {
  148. let app_id = AppIdentify::parse(self.app_id)?.0;
  149. let name = match self.name {
  150. None => None,
  151. Some(name) => Some(AppName::parse(name)?.0),
  152. };
  153. let color_style = match self.color_style {
  154. None => None,
  155. Some(color_style) => Some(AppColorStyle::parse(color_style.theme_color)?.into()),
  156. };
  157. Ok(UpdateAppParams {
  158. app_id,
  159. name,
  160. desc: self.desc,
  161. color_style,
  162. is_trash: self.is_trash,
  163. })
  164. }
  165. }