309 lines
6.7 KiB
Rust
Raw Normal View History

2021-07-19 22:44:37 +08:00
use crate::{
entities::trash::{Trash, TrashType},
errors::ErrorCode,
2021-07-20 15:51:49 +08:00
impl_def_and_def_mut,
parser::{
2021-12-31 10:32:25 +08:00
app::AppIdentify,
2022-01-13 11:16:26 +08:00
view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail},
},
2021-07-19 22:44:37 +08:00
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
2022-01-15 23:58:36 +08:00
use serde::{Deserialize, Serialize};
2021-07-19 22:44:37 +08:00
use std::convert::TryInto;
2022-01-15 23:58:36 +08:00
#[derive(PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
2022-01-13 11:16:26 +08:00
pub struct View {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub belong_to_id: String,
#[pb(index = 3)]
pub name: String,
#[pb(index = 4)]
pub desc: String,
#[pb(index = 5)]
pub view_type: ViewType,
#[pb(index = 6)]
pub version: i64,
#[pb(index = 7)]
pub belongings: RepeatedView,
#[pb(index = 8)]
pub modified_time: i64,
#[pb(index = 9)]
pub create_time: i64,
}
2022-01-15 23:58:36 +08:00
#[derive(PartialEq, Debug, Default, ProtoBuf, Clone, Serialize, Deserialize)]
#[serde(transparent)]
2022-01-13 11:16:26 +08:00
pub struct RepeatedView {
#[pb(index = 1)]
pub items: Vec<View>,
}
impl_def_and_def_mut!(RepeatedView, View);
impl std::convert::From<View> for Trash {
fn from(view: View) -> Self {
Trash {
id: view.id,
name: view.name,
modified_time: view.modified_time,
create_time: view.create_time,
ty: TrashType::View,
}
}
}
2022-01-15 23:58:36 +08:00
#[derive(PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
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-12-29 00:34:00 +08:00
2022-01-01 14:23:58 +08:00
// ViewType::Doc -> Delta string
2021-12-29 00:34:00 +08:00
#[pb(index = 6)]
pub view_data: String,
#[pb(index = 7)]
pub view_id: String,
2021-09-14 16:22:44 +08:00
}
impl CreateViewParams {
2021-12-29 00:34:00 +08:00
pub fn new(
belong_to_id: String,
name: String,
desc: String,
view_type: ViewType,
thumbnail: String,
view_data: String,
view_id: String,
) -> Self {
2021-09-14 16:22:44 +08:00
Self {
belong_to_id,
name,
desc,
thumbnail,
view_type,
2021-12-29 00:34:00 +08:00
view_data,
view_id,
2021-09-14 16:22:44 +08:00
}
}
2021-07-19 22:44:37 +08:00
}
impl TryInto<CreateViewParams> for CreateViewRequest {
type Error = ErrorCode;
2021-07-19 22:44:37 +08:00
fn try_into(self) -> Result<CreateViewParams, Self::Error> {
let name = ViewName::parse(self.name)?.0;
2021-12-31 10:32:25 +08:00
let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
2022-01-15 23:58:36 +08:00
let view_data = "".to_string();
2021-12-29 00:34:00 +08:00
let view_id = uuid::Uuid::new_v4().to_string();
2021-07-19 22:44:37 +08:00
let thumbnail = match self.thumbnail {
None => "".to_string(),
Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
2021-07-19 22:44:37 +08:00
};
2021-09-26 16:39:57 +08:00
Ok(CreateViewParams::new(
belong_to_id,
name,
self.desc,
self.view_type,
thumbnail,
2021-12-29 00:34:00 +08:00
view_data,
view_id,
2021-09-26 16:39:57 +08:00
))
2021-07-19 22:44:37 +08:00
}
}
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf)]
pub struct QueryViewRequest {
2021-07-19 22:44:37 +08:00
#[pb(index = 1)]
2022-01-13 11:16:26 +08:00
pub view_ids: Vec<String>,
}
2021-07-19 22:44:37 +08:00
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct ViewId {
#[pb(index = 1)]
pub view_id: String,
}
2021-07-19 22:44:37 +08:00
2022-01-13 11:16:26 +08:00
impl std::convert::From<String> for ViewId {
fn from(view_id: String) -> Self { ViewId { view_id } }
}
2021-07-19 22:44:37 +08:00
2022-01-13 11:16:26 +08:00
impl TryInto<ViewId> for QueryViewRequest {
type Error = ErrorCode;
fn try_into(self) -> Result<ViewId, Self::Error> {
debug_assert!(self.view_ids.len() == 1);
if self.view_ids.len() != 1 {
log::error!("The len of view_ids should be equal to 1");
return Err(ErrorCode::ViewIdInvalid);
}
2021-07-28 15:13:48 +08:00
2022-01-13 11:16:26 +08:00
let view_id = self.view_ids.first().unwrap().clone();
let view_id = ViewIdentify::parse(view_id)?.0;
2022-01-13 11:16:26 +08:00
Ok(ViewId { view_id })
}
}
2021-09-02 19:57:19 +08:00
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf)]
pub struct RepeatedViewId {
#[pb(index = 1)]
pub items: Vec<String>,
}
2021-09-02 19:57:19 +08:00
2022-01-13 11:16:26 +08:00
impl TryInto<RepeatedViewId> for QueryViewRequest {
type Error = ErrorCode;
fn try_into(self) -> Result<RepeatedViewId, Self::Error> {
let mut view_ids = vec![];
for view_id in self.view_ids {
let view_id = ViewIdentify::parse(view_id)?.0;
view_ids.push(view_id);
}
Ok(RepeatedViewId { items: view_ids })
}
2021-07-19 22:44:37 +08:00
}
2021-07-20 15:51:49 +08:00
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf)]
pub struct UpdateViewRequest {
2021-07-20 15:51:49 +08:00
#[pb(index = 1)]
2022-01-13 11:16:26 +08:00
pub view_id: String,
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub desc: Option<String>,
#[pb(index = 4, one_of)]
pub thumbnail: Option<String>,
2021-07-20 15:51:49 +08:00
}
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct UpdateViewParams {
#[pb(index = 1)]
pub view_id: String,
2021-10-13 11:10:29 +08:00
2022-01-13 11:16:26 +08:00
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub desc: Option<String>,
#[pb(index = 4, one_of)]
pub thumbnail: Option<String>,
}
impl UpdateViewParams {
pub fn new(view_id: &str) -> Self {
Self {
view_id: view_id.to_owned(),
..Default::default()
2021-10-13 11:10:29 +08:00
}
}
2022-01-13 11:16:26 +08:00
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn desc(mut self, desc: &str) -> Self {
self.desc = Some(desc.to_owned());
self
}
}
impl TryInto<UpdateViewParams> for UpdateViewRequest {
type Error = ErrorCode;
fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
let view_id = ViewIdentify::parse(self.view_id)?.0;
let name = match self.name {
None => None,
Some(name) => Some(ViewName::parse(name)?.0),
};
let desc = match self.desc {
None => None,
Some(desc) => Some(ViewDesc::parse(desc)?.0),
};
let thumbnail = match self.thumbnail {
None => None,
Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
};
Ok(UpdateViewParams {
view_id,
name,
desc,
thumbnail,
})
}
2021-10-13 11:10:29 +08:00
}