1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{collections::HashMap, sync::Arc};

use async_graphql::dataloader::Loader;

use uuid::Uuid;

use crate::backend::engine::SDKEngine;

use super::label::Label;

// #[derive(Clone)]
pub struct LabelLoader(Arc<SDKEngine>);

impl LabelLoader {
    pub fn new(e: Arc<SDKEngine>) -> Self {
        Self(e)
    }
}

impl Loader<Uuid> for LabelLoader {
    type Value = Label;
    type Error = Arc<sqlx::Error>;

    async fn load(&self, keys: &'_ [Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
        let labels = sqlx::query!(
            r#"
            SELECT * FROM labels WHERE id  = ANY($1)
            "#,
            &keys
        )
        .fetch_all(&*self.0.db_pool)
        .await
        .unwrap();

        //iterate to get the hashmap
        let labels_map: HashMap<Uuid, Label> = labels
            .iter()
            .map(|label| {
                (
                    label.id,
                    Label {
                        id: label.id,
                        created_at: label.created_at,
                        updated_at: label.updated_at,
                        name: label.name.clone(),
                        owner_id: label.owner_id,
                        description: label.description.clone(),
                        color: label.color.clone(),
                    },
                )
            })
            .collect();

        //println!("{:?}", labels);
        Ok(labels_map)
    }
}