workspace.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use serde::{Deserialize, Serialize};
  2. use sqlx::prelude::Type;
  3. use crate::request::WorkspaceRequest;
  4. #[derive(Debug, Clone, Serialize)]
  5. pub struct Workspace {
  6. pub id: i64,
  7. pub name: String,
  8. }
  9. #[derive(Debug, Clone)]
  10. pub struct WorkspaceEnvironment {
  11. pub id: i64,
  12. pub workspace_id: i64,
  13. pub name: String,
  14. }
  15. #[derive(Debug, Clone, Serialize)]
  16. #[serde(tag = "type", content = "data")]
  17. pub enum WorkspaceEntry {
  18. Collection(WorkspaceEntryBase),
  19. Request(WorkspaceRequest),
  20. }
  21. impl WorkspaceEntry {
  22. pub fn new_req(req: WorkspaceRequest) -> Self {
  23. Self::Request(req)
  24. }
  25. pub fn new_col(col: WorkspaceEntryBase) -> Self {
  26. Self::Collection(col)
  27. }
  28. pub fn id(&self) -> i64 {
  29. match self {
  30. WorkspaceEntry::Collection(c) => c.id,
  31. WorkspaceEntry::Request(r) => r.entry.id,
  32. }
  33. }
  34. pub fn parent_id(&self) -> Option<i64> {
  35. match self {
  36. WorkspaceEntry::Collection(c) => c.parent_id,
  37. WorkspaceEntry::Request(r) => r.entry.parent_id,
  38. }
  39. }
  40. }
  41. /// Database model representation
  42. #[derive(Debug, Clone, Serialize)]
  43. pub struct WorkspaceEntryBase {
  44. pub id: i64,
  45. pub workspace_id: i64,
  46. pub parent_id: Option<i64>,
  47. pub name: String,
  48. pub r#type: WorkspaceEntryType,
  49. }
  50. #[derive(Debug, Clone)]
  51. pub struct WorkspaceEnvVariable {
  52. pub id: i64,
  53. pub env_id: i64,
  54. pub workspace_id: i64,
  55. pub name: String,
  56. pub value: Option<String>,
  57. pub secret: bool,
  58. }
  59. #[derive(Debug, Clone, Serialize, Deserialize)]
  60. pub enum WorkspaceEntryCreate {
  61. Collection {
  62. name: String,
  63. workspace_id: i64,
  64. parent_id: Option<i64>,
  65. },
  66. Request {
  67. name: String,
  68. workspace_id: i64,
  69. parent_id: Option<i64>,
  70. method: String,
  71. url: String,
  72. },
  73. }
  74. #[derive(Debug, Clone, Copy, Type, Serialize)]
  75. #[sqlx(type_name = "INTEGER")]
  76. pub enum WorkspaceEntryType {
  77. Request,
  78. Collection,
  79. }
  80. impl From<i64> for WorkspaceEntryType {
  81. fn from(value: i64) -> Self {
  82. match value {
  83. 0 => Self::Request,
  84. 1 => Self::Collection,
  85. _ => panic!("unrecognized entry type: {value}"),
  86. }
  87. }
  88. }