113 lines
2.6 KiB
Rust
Raw Normal View History

2021-07-19 22:44:37 +08:00
use crate::{
2021-07-28 13:41:39 +08:00
entities::{app::parser::BelongToId, view::parser::*},
2021-07-25 08:13:59 +08:00
errors::{ErrorBuilder, WorkspaceError, WsErrCode},
2021-07-20 15:51:49 +08:00
impl_def_and_def_mut,
2021-07-21 22:41:44 +08:00
sql_tables::view::ViewTableType,
2021-07-19 22:44:37 +08:00
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use std::convert::TryInto;
2021-07-20 15:51:49 +08:00
#[derive(PartialEq, Debug, ProtoBuf_Enum)]
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
}
#[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
}
pub struct CreateViewParams {
2021-07-28 13:41:39 +08:00
pub belong_to_id: String,
2021-07-19 22:44:37 +08:00
pub name: String,
pub desc: String,
pub thumbnail: String,
2021-07-21 22:41:44 +08:00
pub view_type: ViewTableType,
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-07-25 08:13:59 +08:00
.map_err(|e| ErrorBuilder::new(WsErrCode::ViewNameInvalid).msg(e).build())?
2021-07-19 22:44:37 +08:00
.0;
2021-07-28 13:41:39 +08:00
let belong_to_id = BelongToId::parse(self.belong_to_id)
2021-07-25 08:13:59 +08:00
.map_err(|e| ErrorBuilder::new(WsErrCode::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)
.map_err(|e| {
2021-07-25 08:13:59 +08:00
ErrorBuilder::new(WsErrCode::ViewThumbnailInvalid)
2021-07-19 22:44:37 +08:00
.msg(e)
.build()
})?
.0
},
};
let view_type = ViewTypeCheck::parse(self.view_type).unwrap().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,
view_type,
})
}
}
2021-07-20 15:51:49 +08:00
#[derive(PartialEq, ProtoBuf, Default, Debug)]
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,
#[pb(index = 7)]
pub belongings: RepeatedView,
2021-07-19 22:44:37 +08:00
}
2021-07-20 15:51:49 +08:00
#[derive(PartialEq, Debug, Default, ProtoBuf)]
pub struct RepeatedView {
#[pb(index = 1)]
pub items: Vec<View>,
}
impl_def_and_def_mut!(RepeatedView, View);