app_table.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. use std::convert::TryInto;
  2. use diesel::sql_types::Binary;
  3. use serde::{Deserialize, Serialize, __private::TryFrom};
  4. use flowy_database::schema::app_table;
  5. use crate::{
  6. entities::{
  7. app::{App, ColorStyle, UpdateAppParams},
  8. trash::{Trash, TrashType},
  9. view::RepeatedView,
  10. },
  11. sql_tables::workspace::WorkspaceTable,
  12. };
  13. #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
  14. #[belongs_to(WorkspaceTable, foreign_key = "workspace_id")]
  15. #[table_name = "app_table"]
  16. pub(crate) struct AppTable {
  17. pub id: String,
  18. pub workspace_id: String, // equal to #[belongs_to(Workspace, foreign_key = "workspace_id")].
  19. pub name: String,
  20. pub desc: String,
  21. pub color_style: ColorStyleCol,
  22. pub last_view_id: Option<String>,
  23. pub modified_time: i64,
  24. pub create_time: i64,
  25. pub version: i64,
  26. pub is_trash: bool,
  27. }
  28. impl AppTable {
  29. pub fn new(app: App) -> Self {
  30. Self {
  31. id: app.id,
  32. workspace_id: app.workspace_id,
  33. name: app.name,
  34. desc: app.desc,
  35. color_style: ColorStyleCol::default(),
  36. last_view_id: None,
  37. modified_time: app.modified_time,
  38. create_time: app.create_time,
  39. version: 0,
  40. is_trash: false,
  41. }
  42. }
  43. }
  44. impl std::convert::From<AppTable> for Trash {
  45. fn from(table: AppTable) -> Self {
  46. Trash {
  47. id: table.id,
  48. name: table.name,
  49. modified_time: table.modified_time,
  50. create_time: table.create_time,
  51. ty: TrashType::App,
  52. }
  53. }
  54. }
  55. #[derive(Clone, PartialEq, Serialize, Deserialize, Debug, Default, FromSqlRow, AsExpression)]
  56. #[sql_type = "Binary"]
  57. pub(crate) struct ColorStyleCol {
  58. pub(crate) theme_color: String,
  59. }
  60. impl std::convert::From<ColorStyle> for ColorStyleCol {
  61. fn from(s: ColorStyle) -> Self {
  62. Self {
  63. theme_color: s.theme_color,
  64. }
  65. }
  66. }
  67. impl std::convert::TryInto<Vec<u8>> for &ColorStyleCol {
  68. type Error = String;
  69. fn try_into(self) -> Result<Vec<u8>, Self::Error> { bincode::serialize(self).map_err(|e| format!("{:?}", e)) }
  70. }
  71. impl std::convert::TryFrom<&[u8]> for ColorStyleCol {
  72. type Error = String;
  73. fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
  74. bincode::deserialize(value).map_err(|e| format!("{:?}", e))
  75. }
  76. }
  77. impl_sql_binary_expression!(ColorStyleCol);
  78. #[derive(AsChangeset, Identifiable, Default, Debug)]
  79. #[table_name = "app_table"]
  80. pub(crate) struct AppTableChangeset {
  81. pub id: String,
  82. pub name: Option<String>,
  83. pub desc: Option<String>,
  84. pub is_trash: Option<bool>,
  85. }
  86. impl AppTableChangeset {
  87. pub(crate) fn new(params: UpdateAppParams) -> Self {
  88. AppTableChangeset {
  89. id: params.app_id,
  90. name: params.name,
  91. desc: params.desc,
  92. is_trash: params.is_trash,
  93. }
  94. }
  95. pub(crate) fn from_table(table: AppTable) -> Self {
  96. AppTableChangeset {
  97. id: table.id,
  98. name: Some(table.name),
  99. desc: Some(table.desc),
  100. is_trash: Some(table.is_trash),
  101. }
  102. }
  103. }
  104. impl std::convert::From<AppTable> for App {
  105. fn from(table: AppTable) -> Self {
  106. App {
  107. id: table.id,
  108. workspace_id: table.workspace_id,
  109. name: table.name,
  110. desc: table.desc,
  111. belongings: RepeatedView::default(),
  112. version: table.version,
  113. modified_time: table.modified_time,
  114. create_time: table.create_time,
  115. }
  116. }
  117. }