workspace.rs 2.4 KB

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