cala_server/graphql/
job.rs

1use async_graphql::{types::connection::*, *};
2use serde::{Deserialize, Serialize};
3
4use super::{convert::ToGlobalId, primitives::*};
5
6#[derive(SimpleObject)]
7pub struct Job {
8    pub id: ID,
9    pub job_id: UUID,
10    pub name: String,
11    pub description: Option<String>,
12}
13
14#[derive(SimpleObject)]
15pub struct JobCreatePayload {
16    pub job: Job,
17}
18
19#[derive(Serialize, Deserialize)]
20pub(super) struct JobByNameCursor {
21    pub name: String,
22    pub id: crate::primitives::JobId,
23}
24
25impl CursorType for JobByNameCursor {
26    type Error = String;
27
28    fn encode_cursor(&self) -> String {
29        use base64::{engine::general_purpose, Engine as _};
30        let json = serde_json::to_string(&self).expect("could not serialize token");
31        general_purpose::STANDARD_NO_PAD.encode(json.as_bytes())
32    }
33
34    fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
35        use base64::{engine::general_purpose, Engine as _};
36        let bytes = general_purpose::STANDARD_NO_PAD
37            .decode(s.as_bytes())
38            .map_err(|e| e.to_string())?;
39        let json = String::from_utf8(bytes).map_err(|e| e.to_string())?;
40        serde_json::from_str(&json).map_err(|e| e.to_string())
41    }
42}
43
44impl ToGlobalId for crate::primitives::JobId {
45    fn to_global_id(&self) -> async_graphql::types::ID {
46        async_graphql::types::ID::from(format!("job:{}", self))
47    }
48}
49
50impl From<JobByNameCursor> for crate::job::JobByNameCursor {
51    fn from(cursor: JobByNameCursor) -> Self {
52        Self {
53            name: cursor.name,
54            id: cursor.id,
55        }
56    }
57}
58
59impl From<&crate::job::Job> for JobByNameCursor {
60    fn from(job: &crate::job::Job) -> Self {
61        Self {
62            name: job.name.clone(),
63            id: job.id,
64        }
65    }
66}
67
68impl From<crate::job::Job> for Job {
69    fn from(job: crate::job::Job) -> Self {
70        Self {
71            id: job.id.to_global_id(),
72            job_id: UUID::from(job.id),
73            name: job.name,
74            description: job.description,
75        }
76    }
77}