view_create.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use crate::{
  2. entities::{app::parser::AppId, view::parser::*},
  3. errors::{ErrorBuilder, WorkspaceError, WsErrCode},
  4. impl_def_and_def_mut,
  5. sql_tables::view::ViewTableType,
  6. };
  7. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  8. use std::convert::TryInto;
  9. #[derive(PartialEq, Debug, ProtoBuf_Enum)]
  10. pub enum ViewType {
  11. Blank = 0,
  12. Doc = 1,
  13. }
  14. impl std::default::Default for ViewType {
  15. fn default() -> Self { ViewType::Blank }
  16. }
  17. #[derive(Default, ProtoBuf)]
  18. pub struct CreateViewRequest {
  19. #[pb(index = 1)]
  20. pub app_id: String,
  21. #[pb(index = 2)]
  22. pub name: String,
  23. #[pb(index = 3)]
  24. pub desc: String,
  25. #[pb(index = 4, one_of)]
  26. pub thumbnail: Option<String>,
  27. #[pb(index = 5)]
  28. pub view_type: ViewType,
  29. }
  30. pub struct CreateViewParams {
  31. pub app_id: String,
  32. pub name: String,
  33. pub desc: String,
  34. pub thumbnail: String,
  35. pub view_type: ViewTableType,
  36. }
  37. impl TryInto<CreateViewParams> for CreateViewRequest {
  38. type Error = WorkspaceError;
  39. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  40. let name = ViewName::parse(self.name)
  41. .map_err(|e| ErrorBuilder::new(WsErrCode::ViewNameInvalid).msg(e).build())?
  42. .0;
  43. let app_id = AppId::parse(self.app_id)
  44. .map_err(|e| ErrorBuilder::new(WsErrCode::AppIdInvalid).msg(e).build())?
  45. .0;
  46. let thumbnail = match self.thumbnail {
  47. None => "".to_string(),
  48. Some(thumbnail) => {
  49. ViewThumbnail::parse(thumbnail)
  50. .map_err(|e| {
  51. ErrorBuilder::new(WsErrCode::ViewThumbnailInvalid)
  52. .msg(e)
  53. .build()
  54. })?
  55. .0
  56. },
  57. };
  58. let view_type = ViewTypeCheck::parse(self.view_type).unwrap().0;
  59. Ok(CreateViewParams {
  60. app_id,
  61. name,
  62. desc: self.desc,
  63. thumbnail,
  64. view_type,
  65. })
  66. }
  67. }
  68. #[derive(PartialEq, ProtoBuf, Default, Debug)]
  69. pub struct View {
  70. #[pb(index = 1)]
  71. pub id: String,
  72. #[pb(index = 2)]
  73. pub app_id: String,
  74. #[pb(index = 3)]
  75. pub name: String,
  76. #[pb(index = 4)]
  77. pub desc: String,
  78. #[pb(index = 5)]
  79. pub view_type: ViewType,
  80. }
  81. #[derive(PartialEq, Debug, Default, ProtoBuf)]
  82. pub struct RepeatedView {
  83. #[pb(index = 1)]
  84. pub items: Vec<View>,
  85. }
  86. impl_def_and_def_mut!(RepeatedView, View);