137 lines
3.3 KiB
Rust
Raw Normal View History

2021-07-13 23:08:20 +08:00
use crate::{
entities::{
app::{
2021-08-25 17:34:20 +08:00
parser::{AppColorStyle, AppId, AppName},
ColorStyle,
},
workspace::parser::WorkspaceId,
2021-07-13 23:08:20 +08:00
},
2021-07-25 08:13:59 +08:00
errors::{ErrorBuilder, WorkspaceError, WsErrCode},
2021-07-13 23:08:20 +08:00
};
use flowy_derive::ProtoBuf;
use std::convert::TryInto;
#[derive(ProtoBuf, Default)]
pub struct UpdateAppRequest {
#[pb(index = 1)]
pub app_id: String,
#[pb(index = 2, one_of)]
pub workspace_id: Option<String>,
#[pb(index = 3, one_of)]
pub name: Option<String>,
#[pb(index = 4, one_of)]
pub desc: Option<String>,
#[pb(index = 5, one_of)]
pub color_style: Option<ColorStyle>,
#[pb(index = 6, one_of)]
pub is_trash: Option<bool>,
2021-07-13 23:08:20 +08:00
}
2021-08-25 17:34:20 +08:00
#[derive(ProtoBuf, Default)]
2021-07-13 23:08:20 +08:00
pub struct UpdateAppParams {
2021-08-25 17:34:20 +08:00
#[pb(index = 1)]
2021-07-13 23:08:20 +08:00
pub app_id: String,
2021-08-25 17:34:20 +08:00
#[pb(index = 2, one_of)]
2021-07-13 23:08:20 +08:00
pub workspace_id: Option<String>,
2021-08-25 17:34:20 +08:00
#[pb(index = 3, one_of)]
2021-07-13 23:08:20 +08:00
pub name: Option<String>,
2021-08-25 17:34:20 +08:00
#[pb(index = 4, one_of)]
2021-07-13 23:08:20 +08:00
pub desc: Option<String>,
2021-08-25 17:34:20 +08:00
#[pb(index = 5, one_of)]
2021-07-13 23:08:20 +08:00
pub color_style: Option<ColorStyle>,
2021-08-25 17:34:20 +08:00
#[pb(index = 6, one_of)]
pub is_trash: Option<bool>,
2021-07-13 23:08:20 +08:00
}
2021-08-26 17:58:59 +08:00
impl UpdateAppParams {
pub fn new(app_id: &str) -> Self {
Self {
app_id: app_id.to_string(),
..Default::default()
}
}
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_string());
self
}
pub fn desc(mut self, desc: &str) -> Self {
self.desc = Some(desc.to_string());
self
}
pub fn trash(mut self) -> Self {
self.is_trash = Some(true);
self
}
}
2021-07-13 23:08:20 +08:00
impl TryInto<UpdateAppParams> for UpdateAppRequest {
type Error = WorkspaceError;
fn try_into(self) -> Result<UpdateAppParams, Self::Error> {
2021-08-25 17:34:20 +08:00
let app_id = AppId::parse(self.app_id)
2021-07-25 08:13:59 +08:00
.map_err(|e| ErrorBuilder::new(WsErrCode::AppIdInvalid).msg(e).build())?
2021-07-13 23:08:20 +08:00
.0;
let name = match self.name {
None => None,
Some(name) => Some(
AppName::parse(name)
.map_err(|e| {
2021-07-25 08:13:59 +08:00
ErrorBuilder::new(WsErrCode::WorkspaceNameInvalid)
2021-07-13 23:08:20 +08:00
.msg(e)
.build()
})?
.0,
),
};
let workspace_id = match self.workspace_id {
None => None,
Some(wid) => Some(
WorkspaceId::parse(wid)
.map_err(|e| {
2021-07-25 08:13:59 +08:00
ErrorBuilder::new(WsErrCode::WorkspaceIdInvalid)
2021-07-13 23:08:20 +08:00
.msg(e)
.build()
})?
.0,
),
};
let color_style = match self.color_style {
None => None,
Some(color_style) => Some(
AppColorStyle::parse(color_style)
.map_err(|e| {
2021-07-25 08:13:59 +08:00
ErrorBuilder::new(WsErrCode::AppColorStyleInvalid)
2021-07-13 23:08:20 +08:00
.msg(e)
.build()
})?
.0,
),
};
Ok(UpdateAppParams {
app_id,
workspace_id,
name,
desc: self.desc,
color_style,
is_trash: self.is_trash,
2021-07-13 23:08:20 +08:00
})
}
}