app_sql.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. use crate::entities::{
  2. app::UpdateAppParams,
  3. trash::{TrashPB, TrashType},
  4. };
  5. use crate::{errors::FlowyError, services::persistence::version_1::workspace_sql::WorkspaceTable};
  6. use flowy_database::{
  7. prelude::*,
  8. schema::{app_table, app_table::dsl},
  9. SqliteConnection,
  10. };
  11. use folder_rev_model::AppRevision;
  12. pub struct AppTableSql();
  13. impl AppTableSql {
  14. pub(crate) fn create_app(app_rev: AppRevision, conn: &SqliteConnection) -> Result<(), FlowyError> {
  15. let app_table = AppTable::new(app_rev);
  16. match diesel_record_count!(app_table, &app_table.id, conn) {
  17. 0 => diesel_insert_table!(app_table, app_table.clone(), conn),
  18. _ => {
  19. let changeset = AppChangeset::from_table(app_table);
  20. diesel_update_table!(app_table, changeset, conn)
  21. }
  22. }
  23. Ok(())
  24. }
  25. pub(crate) fn update_app(changeset: AppChangeset, conn: &SqliteConnection) -> Result<(), FlowyError> {
  26. diesel_update_table!(app_table, changeset, conn);
  27. Ok(())
  28. }
  29. pub(crate) fn read_app(app_id: &str, conn: &SqliteConnection) -> Result<AppTable, FlowyError> {
  30. let filter = dsl::app_table.filter(app_table::id.eq(app_id)).into_boxed();
  31. let app_table = filter.first::<AppTable>(conn)?;
  32. Ok(app_table)
  33. }
  34. pub(crate) fn read_workspace_apps(
  35. workspace_id: &str,
  36. conn: &SqliteConnection,
  37. ) -> Result<Vec<AppTable>, FlowyError> {
  38. let app_table = dsl::app_table
  39. .filter(app_table::workspace_id.eq(workspace_id))
  40. .order(app_table::create_time.asc())
  41. .load::<AppTable>(conn)?;
  42. Ok(app_table)
  43. }
  44. pub(crate) fn delete_app(app_id: &str, conn: &SqliteConnection) -> Result<AppTable, FlowyError> {
  45. let app_table = dsl::app_table
  46. .filter(app_table::id.eq(app_id))
  47. .first::<AppTable>(conn)?;
  48. diesel_delete_table!(app_table, app_id, conn);
  49. Ok(app_table)
  50. }
  51. // pub(crate) fn read_views_belong_to_app(
  52. // &self,
  53. // app_id: &str,
  54. // ) -> Result<Vec<ViewTable>, FlowyError> {
  55. // let conn = self.database.db_connection()?;
  56. //
  57. // let views = conn.immediate_transaction::<_, FlowyError, _>(|| {
  58. // let app_table: AppTable = dsl::app_table
  59. // .filter(app_table::id.eq(app_id))
  60. // .first::<AppTable>(&*(conn))?;
  61. // let views =
  62. // ViewTable::belonging_to(&app_table).load::<ViewTable>(&*conn)?;
  63. // Ok(views)
  64. // })?;
  65. //
  66. // Ok(views)
  67. // }
  68. }
  69. #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
  70. #[belongs_to(WorkspaceTable, foreign_key = "workspace_id")]
  71. #[table_name = "app_table"]
  72. pub(crate) struct AppTable {
  73. pub id: String,
  74. pub workspace_id: String, // equal to #[belongs_to(Workspace, foreign_key = "workspace_id")].
  75. pub name: String,
  76. pub desc: String,
  77. pub color_style: Vec<u8>,
  78. pub last_view_id: Option<String>,
  79. pub modified_time: i64,
  80. pub create_time: i64,
  81. pub version: i64,
  82. pub is_trash: bool,
  83. }
  84. impl AppTable {
  85. pub fn new(app_rev: AppRevision) -> Self {
  86. Self {
  87. id: app_rev.id,
  88. workspace_id: app_rev.workspace_id,
  89. name: app_rev.name,
  90. desc: app_rev.desc,
  91. color_style: Default::default(),
  92. last_view_id: None,
  93. modified_time: app_rev.modified_time,
  94. create_time: app_rev.create_time,
  95. version: 0,
  96. is_trash: false,
  97. }
  98. }
  99. }
  100. impl std::convert::From<AppTable> for TrashPB {
  101. fn from(table: AppTable) -> Self {
  102. TrashPB {
  103. id: table.id,
  104. name: table.name,
  105. modified_time: table.modified_time,
  106. create_time: table.create_time,
  107. ty: TrashType::TrashApp,
  108. }
  109. }
  110. }
  111. #[derive(AsChangeset, Identifiable, Default, Debug)]
  112. #[table_name = "app_table"]
  113. pub struct AppChangeset {
  114. pub id: String,
  115. pub name: Option<String>,
  116. pub desc: Option<String>,
  117. pub is_trash: Option<bool>,
  118. }
  119. impl AppChangeset {
  120. pub(crate) fn new(params: UpdateAppParams) -> Self {
  121. AppChangeset {
  122. id: params.app_id,
  123. name: params.name,
  124. desc: params.desc,
  125. is_trash: params.is_trash,
  126. }
  127. }
  128. pub(crate) fn from_table(table: AppTable) -> Self {
  129. AppChangeset {
  130. id: table.id,
  131. name: Some(table.name),
  132. desc: Some(table.desc),
  133. is_trash: Some(table.is_trash),
  134. }
  135. }
  136. }
  137. impl std::convert::From<AppTable> for AppRevision {
  138. fn from(table: AppTable) -> Self {
  139. AppRevision {
  140. id: table.id,
  141. workspace_id: table.workspace_id,
  142. name: table.name,
  143. desc: table.desc,
  144. belongings: vec![],
  145. version: table.version,
  146. modified_time: table.modified_time,
  147. create_time: table.create_time,
  148. }
  149. }
  150. }