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
58
59
60
use std::{collections::HashMap, str::FromStr, sync::Arc};

use async_graphql::dataloader::Loader;

use uuid::Uuid;

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

use super::team::{Team, TeamVisibility};

pub struct TeamLoader(Arc<SDKEngine>);

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

impl Loader<Uuid> for TeamLoader {
    type Value = Team;
    type Error = Arc<sqlx::Error>;

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

        //iterate to get the hashmap
        let teams_map: HashMap<Uuid, Team> = teams
            .iter()
            .map(|team| {
                (
                    team.id,
                    Team {
                        id: team.id,
                        created_at: team.created_at,
                        updated_at: team.updated_at,
                        name: team.name.clone(),
                        owner_id: team.owner_id,
                        visibility: team
                            .visibility
                            .clone()
                            .and_then(|a| TeamVisibility::from_str(&a).ok())
                            .unwrap_or_default(),
                        prefix: team.prefix.clone(),
                    },
                )
            })
            .collect();

        //println!("{:?}", teams);
        Ok(teams_map)
    }
}