116 lines
3.1 KiB
Rust
Raw Normal View History

2021-07-13 23:08:20 +08:00
use crate::{
2021-07-20 15:51:49 +08:00
entities::{
app::{App, ColorStyle, CreateAppParams, UpdateAppParams},
view::RepeatedView,
},
2021-07-13 23:08:20 +08:00
impl_sql_binary_expression,
sql_tables::workspace::WorkspaceTable,
2021-07-13 23:08:20 +08:00
};
2021-07-13 17:19:39 +08:00
use diesel::sql_types::Binary;
use flowy_database::schema::app_table;
2021-07-13 17:19:39 +08:00
use flowy_infra::{timestamp, uuid};
use serde::{Deserialize, Serialize, __private::TryFrom};
use std::convert::TryInto;
2021-07-19 22:44:37 +08:00
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
#[belongs_to(WorkspaceTable, foreign_key = "workspace_id")]
2021-07-13 17:19:39 +08:00
#[table_name = "app_table"]
pub(crate) struct AppTable {
2021-07-13 17:19:39 +08:00
pub id: String,
pub workspace_id: String, // equal to #[belongs_to(Workspace, foreign_key = "workspace_id")].
pub name: String,
pub desc: String,
pub color_style: ColorStyleCol,
pub last_view_id: Option<String>,
pub modified_time: i64,
pub create_time: i64,
pub version: i64,
pub is_trash: bool,
2021-07-13 17:19:39 +08:00
}
impl AppTable {
2021-07-13 23:08:20 +08:00
pub fn new(params: CreateAppParams) -> Self {
let app_id = uuid();
let time = timestamp();
Self {
id: app_id,
workspace_id: params.workspace_id,
name: params.name,
desc: params.desc,
color_style: params.color_style.into(),
last_view_id: None,
modified_time: time,
create_time: time,
version: 0,
is_trash: false,
2021-07-13 23:08:20 +08:00
}
}
}
2021-07-13 17:19:39 +08:00
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, Default, FromSqlRow, AsExpression)]
#[sql_type = "Binary"]
pub(crate) struct ColorStyleCol {
pub(crate) theme_color: String,
}
2021-07-13 23:08:20 +08:00
impl std::convert::From<ColorStyle> for ColorStyleCol {
fn from(s: ColorStyle) -> Self {
Self {
theme_color: s.theme_color,
}
}
}
2021-07-13 17:19:39 +08:00
impl std::convert::TryInto<Vec<u8>> for &ColorStyleCol {
type Error = String;
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
bincode::serialize(self).map_err(|e| format!("{:?}", e))
}
}
impl std::convert::TryFrom<&[u8]> for ColorStyleCol {
type Error = String;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
bincode::deserialize(value).map_err(|e| format!("{:?}", e))
}
}
impl_sql_binary_expression!(ColorStyleCol);
2021-07-13 23:08:20 +08:00
#[derive(AsChangeset, Identifiable, Default, Debug)]
#[table_name = "app_table"]
pub struct AppTableChangeset {
2021-07-13 23:08:20 +08:00
pub id: String,
pub workspace_id: Option<String>,
pub name: Option<String>,
pub desc: Option<String>,
pub is_trash: Option<bool>,
2021-07-13 23:08:20 +08:00
}
impl AppTableChangeset {
2021-07-13 23:08:20 +08:00
pub fn new(params: UpdateAppParams) -> Self {
AppTableChangeset {
2021-07-13 23:08:20 +08:00
id: params.app_id,
workspace_id: params.workspace_id,
name: params.name,
desc: params.desc,
is_trash: params.is_trash,
2021-07-13 23:08:20 +08:00
}
}
}
2021-07-19 22:44:37 +08:00
impl std::convert::Into<App> for AppTable {
fn into(self) -> App {
App {
id: self.id,
workspace_id: self.workspace_id,
name: self.name,
desc: self.desc,
2021-07-20 15:51:49 +08:00
views: RepeatedView::default(),
2021-07-28 15:13:48 +08:00
version: self.version,
}
}
}