Dataloader für BenutzerGruppen und BenutzerRollen hinzugefügt

This commit is contained in:
Peter Schiwy
2024-12-02 23:44:47 +01:00
parent f410791d4b
commit bfce29c8ee
17 changed files with 419 additions and 84 deletions

View File

@@ -0,0 +1,51 @@
use async_graphql::dataloader::*;
use async_graphql::*;
use itertools::Itertools;
use std::collections::HashMap;
use std::sync::Arc;
use crate::models::rolle::Rolle;
use crate::scalar::Id;
pub struct BenutzerRollenLoader {
pub pool: sqlx::PgPool,
}
impl Loader<Id> for BenutzerRollenLoader {
type Value = Vec<Rolle>;
type Error = Arc<sqlx::Error>;
async fn load(&self, keys: &[Id]) -> Result<HashMap<Id, Self::Value>, Self::Error> {
let rows = sqlx::query!(
r#"
SELECT
br.benutzer_id,
r.id,
r.rollenname,
r.erstellt_am,
r.geaendert_am
FROM rollen AS r
LEFT JOIN benutzer_rollen AS br ON r.id = br.rolle_id
WHERE br.benutzer_id = ANY($1);
"#,
keys
)
.fetch_all(&self.pool)
.await?
.into_iter()
.map(|row| {
(
row.benutzer_id,
Rolle {
id: row.id,
rollenname: row.rollenname,
erstellt_am: row.erstellt_am,
geaendert_am: row.geaendert_am,
},
)
})
.into_group_map();
Ok(rows)
}
}