2021-07-19 22:44:37 +08:00
|
|
|
use crate::{
|
2021-08-25 17:34:20 +08:00
|
|
|
entities::{app::parser::AppId, view::parser::*},
|
2021-08-30 22:44:17 +08:00
|
|
|
errors::{ErrorBuilder, ErrorCode, WorkspaceError},
|
2021-07-20 15:51:49 +08:00
|
|
|
impl_def_and_def_mut,
|
2021-07-19 22:44:37 +08:00
|
|
|
};
|
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
#[derive(PartialEq, Debug, ProtoBuf_Enum, Clone)]
|
2021-07-21 22:41:44 +08:00
|
|
|
pub enum ViewType {
|
2021-07-22 17:39:44 +08:00
|
|
|
Blank = 0,
|
|
|
|
Doc = 1,
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
|
|
|
|
2021-07-21 22:41:44 +08:00
|
|
|
impl std::default::Default for ViewType {
|
2021-07-22 17:39:44 +08:00
|
|
|
fn default() -> Self { ViewType::Blank }
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:33:29 +08:00
|
|
|
impl std::convert::From<i32> for ViewType {
|
|
|
|
fn from(val: i32) -> Self {
|
|
|
|
match val {
|
|
|
|
1 => ViewType::Doc,
|
|
|
|
0 => ViewType::Blank,
|
|
|
|
_ => {
|
|
|
|
log::error!("Invalid view type: {}", val);
|
|
|
|
ViewType::Blank
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:44:37 +08:00
|
|
|
#[derive(Default, ProtoBuf)]
|
|
|
|
pub struct CreateViewRequest {
|
|
|
|
#[pb(index = 1)]
|
2021-07-28 13:41:39 +08:00
|
|
|
pub belong_to_id: String,
|
2021-07-19 22:44:37 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub desc: String,
|
|
|
|
|
|
|
|
#[pb(index = 4, one_of)]
|
|
|
|
pub thumbnail: Option<String>,
|
|
|
|
|
|
|
|
#[pb(index = 5)]
|
2021-07-21 22:41:44 +08:00
|
|
|
pub view_type: ViewType,
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:26:30 +08:00
|
|
|
#[derive(Default, ProtoBuf, Debug, Clone)]
|
2021-07-19 22:44:37 +08:00
|
|
|
pub struct CreateViewParams {
|
2021-08-25 21:33:29 +08:00
|
|
|
#[pb(index = 1)]
|
2021-07-28 13:41:39 +08:00
|
|
|
pub belong_to_id: String,
|
2021-08-25 21:33:29 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-07-19 22:44:37 +08:00
|
|
|
pub name: String,
|
2021-08-25 21:33:29 +08:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
2021-07-19 22:44:37 +08:00
|
|
|
pub desc: String,
|
2021-08-25 21:33:29 +08:00
|
|
|
|
|
|
|
#[pb(index = 4)]
|
2021-07-19 22:44:37 +08:00
|
|
|
pub thumbnail: String,
|
2021-08-25 21:33:29 +08:00
|
|
|
|
|
|
|
#[pb(index = 5)]
|
|
|
|
pub view_type: ViewType,
|
2021-09-11 14:26:30 +08:00
|
|
|
|
|
|
|
#[pb(index = 6)]
|
|
|
|
pub data: String,
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<CreateViewParams> for CreateViewRequest {
|
|
|
|
type Error = WorkspaceError;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<CreateViewParams, Self::Error> {
|
|
|
|
let name = ViewName::parse(self.name)
|
2021-08-30 22:44:17 +08:00
|
|
|
.map_err(|e| ErrorBuilder::new(ErrorCode::ViewNameInvalid).msg(e).build())?
|
2021-07-19 22:44:37 +08:00
|
|
|
.0;
|
|
|
|
|
2021-08-25 17:34:20 +08:00
|
|
|
let belong_to_id = AppId::parse(self.belong_to_id)
|
2021-08-30 22:44:17 +08:00
|
|
|
.map_err(|e| ErrorBuilder::new(ErrorCode::AppIdInvalid).msg(e).build())?
|
2021-07-19 22:44:37 +08:00
|
|
|
.0;
|
|
|
|
|
|
|
|
let thumbnail = match self.thumbnail {
|
|
|
|
None => "".to_string(),
|
|
|
|
Some(thumbnail) => {
|
|
|
|
ViewThumbnail::parse(thumbnail)
|
2021-09-02 19:57:19 +08:00
|
|
|
.map_err(|e| ErrorBuilder::new(ErrorCode::ViewThumbnailInvalid).msg(e).build())?
|
2021-07-19 22:44:37 +08:00
|
|
|
.0
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(CreateViewParams {
|
2021-07-28 13:41:39 +08:00
|
|
|
belong_to_id,
|
2021-07-19 22:44:37 +08:00
|
|
|
name,
|
|
|
|
desc: self.desc,
|
|
|
|
thumbnail,
|
2021-08-25 21:33:29 +08:00
|
|
|
view_type: self.view_type,
|
2021-09-11 14:26:30 +08:00
|
|
|
data: "".to_owned(),
|
2021-07-19 22:44:37 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
#[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
|
2021-07-19 22:44:37 +08:00
|
|
|
pub struct View {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub id: String,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-07-28 13:41:39 +08:00
|
|
|
pub belong_to_id: String,
|
2021-07-19 22:44:37 +08:00
|
|
|
|
|
|
|
#[pb(index = 3)]
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
#[pb(index = 4)]
|
|
|
|
pub desc: String,
|
|
|
|
|
|
|
|
#[pb(index = 5)]
|
2021-07-21 22:41:44 +08:00
|
|
|
pub view_type: ViewType,
|
2021-07-28 15:13:48 +08:00
|
|
|
|
|
|
|
#[pb(index = 6)]
|
|
|
|
pub version: i64,
|
2021-07-29 22:22:35 +08:00
|
|
|
|
|
|
|
#[pb(index = 7)]
|
|
|
|
pub belongings: RepeatedView,
|
2021-09-02 19:57:19 +08:00
|
|
|
|
|
|
|
#[pb(index = 8)]
|
|
|
|
pub modified_time: i64,
|
|
|
|
|
|
|
|
#[pb(index = 9)]
|
|
|
|
pub create_time: i64,
|
2021-07-19 22:44:37 +08:00
|
|
|
}
|
2021-07-20 15:51:49 +08:00
|
|
|
|
2021-09-02 19:57:19 +08:00
|
|
|
#[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
|
2021-07-20 15:51:49 +08:00
|
|
|
pub struct RepeatedView {
|
|
|
|
#[pb(index = 1)]
|
|
|
|
pub items: Vec<View>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_def_and_def_mut!(RepeatedView, View);
|