trash_table.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use crate::entities::trash::{Trash, TrashType};
  2. use diesel::sql_types::Integer;
  3. use flowy_database::schema::trash_table;
  4. #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
  5. #[table_name = "trash_table"]
  6. pub(crate) struct TrashTable {
  7. pub id: String,
  8. pub name: String,
  9. pub desc: String,
  10. pub modified_time: i64,
  11. pub create_time: i64,
  12. pub ty: SqlTrashType,
  13. }
  14. impl std::convert::Into<Trash> for TrashTable {
  15. fn into(self) -> Trash {
  16. Trash {
  17. id: self.id,
  18. name: self.name,
  19. modified_time: self.modified_time,
  20. create_time: self.create_time,
  21. ty: self.ty.into(),
  22. }
  23. }
  24. }
  25. impl std::convert::From<Trash> for TrashTable {
  26. fn from(trash: Trash) -> Self {
  27. TrashTable {
  28. id: trash.id,
  29. name: trash.name,
  30. desc: "".to_owned(),
  31. modified_time: trash.modified_time,
  32. create_time: trash.create_time,
  33. ty: trash.ty.into(),
  34. }
  35. }
  36. }
  37. #[derive(AsChangeset, Identifiable, Clone, Default, Debug)]
  38. #[table_name = "trash_table"]
  39. pub(crate) struct TrashTableChangeset {
  40. pub id: String,
  41. pub name: Option<String>,
  42. pub modified_time: i64,
  43. }
  44. impl std::convert::From<TrashTable> for TrashTableChangeset {
  45. fn from(trash: TrashTable) -> Self {
  46. TrashTableChangeset {
  47. id: trash.id,
  48. name: Some(trash.name),
  49. modified_time: trash.modified_time,
  50. }
  51. }
  52. }
  53. #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
  54. #[repr(i32)]
  55. #[sql_type = "Integer"]
  56. pub(crate) enum SqlTrashType {
  57. Unknown = 0,
  58. View = 1,
  59. App = 2,
  60. }
  61. impl std::convert::From<i32> for SqlTrashType {
  62. fn from(value: i32) -> Self {
  63. match value {
  64. 0 => SqlTrashType::Unknown,
  65. 1 => SqlTrashType::View,
  66. 2 => SqlTrashType::App,
  67. _o => SqlTrashType::Unknown,
  68. }
  69. }
  70. }
  71. impl_sql_integer_expression!(SqlTrashType);
  72. impl std::convert::Into<TrashType> for SqlTrashType {
  73. fn into(self) -> TrashType {
  74. match self {
  75. SqlTrashType::Unknown => TrashType::Unknown,
  76. SqlTrashType::View => TrashType::View,
  77. SqlTrashType::App => TrashType::App,
  78. }
  79. }
  80. }
  81. impl std::convert::From<TrashType> for SqlTrashType {
  82. fn from(ty: TrashType) -> Self {
  83. match ty {
  84. TrashType::Unknown => SqlTrashType::Unknown,
  85. TrashType::View => SqlTrashType::View,
  86. TrashType::App => SqlTrashType::App,
  87. }
  88. }
  89. }