rollen muations und rolle_viele_erstellen

This commit is contained in:
2026-06-06 16:43:59 +02:00
parent b5182b7f19
commit e649d0211a
10 changed files with 102 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
pub mod dataloader;
pub mod entity;
pub mod model;
pub mod mutation;
pub mod queries;
pub mod repository;
pub mod service;

View File

@@ -0,0 +1,3 @@
pub mod rolle;
pub use rolle::RolleMutation;

View File

@@ -0,0 +1,53 @@
use crate::domain::rolle::{
model::{Rolle, RolleErstelleInput, RolleLoeschenInput, RolleUpdateInput},
service::Service,
};
use async_graphql::{Context, FieldResult};
use sqlx::postgres::PgPool;
#[derive(Default)]
pub struct RolleMutation;
#[async_graphql::Object]
impl RolleMutation {
async fn erstelle_rolle(
&self,
ctx: &Context<'_>,
input: RolleErstelleInput,
) -> FieldResult<Rolle> {
let pool = ctx.data::<PgPool>()?;
let gruppe = Service::new(pool.clone()).rolle_erstellen(input).await?;
Ok(gruppe)
}
async fn erstelle_viele_rollen(
&self,
ctx: &Context<'_>,
input: Vec<RolleErstelleInput>,
) -> FieldResult<Vec<Rolle>> {
let pool = ctx.data::<PgPool>()?.clone();
let typen = Service::new(pool).rolle_erstellen_viele(&input).await?;
Ok(typen)
}
async fn update_rolle(&self, ctx: &Context<'_>, input: RolleUpdateInput) -> FieldResult<Rolle> {
let pool = ctx.data::<PgPool>()?;
let rolle = Service::new(pool.clone()).rolle_update(input).await?;
Ok(rolle)
}
async fn loesche_rolle(
&self,
ctx: &Context<'_>,
input: RolleLoeschenInput,
) -> FieldResult<Rolle> {
let pool = ctx.data::<PgPool>()?;
let rolle = Service::new(pool.clone()).rolle_loeschen(input).await?;
Ok(rolle)
}
}

View File

@@ -2,6 +2,7 @@ mod find_all_rolle;
mod find_rolle_by_id;
mod rolle_alle;
mod rolle_dataloader;
mod rolle_erstelle_viele;
mod rolle_erstellen;
mod rolle_loeschen;
mod rolle_update;

View File

@@ -0,0 +1,43 @@
use anyhow::Error;
use super::Repository;
use crate::database::Queryer;
use crate::domain::rolle::entity;
use crate::domain::rolle::model::{self};
use crate::scalar::{Id, Time, Ulid};
impl Repository {
pub async fn rolle_viele_erstellen<'c, C: Queryer<'c>>(
&self,
db: C,
rollen: &[entity::RolleErstellen],
) -> Result<Vec<model::Rolle>, Error> {
let rolle_id: Vec<Id> = rollen.iter().map(|t| t.rolle_id).collect();
let id: Vec<Ulid> = rollen.iter().map(|t| t.id).collect();
let rollenname: Vec<String> = rollen.iter().map(|t| t.rollenname.clone()).collect();
let erstellt_am: Vec<Time> = rollen.iter().map(|t| t.erstellt_am).collect();
let geaendert_am: Vec<Time> = rollen.iter().map(|t| t.geaendert_am).collect();
const QUERY: &str = r#"
INSERT INTO rollen (rolle_id, id, rollenname, erstellt_am, geaendert_am)
SELECT * FROM UNNEST(
$1::uuid[],
$2::text[],
$3::text[],
$4::TIMESTAMP[],
$5::TIMESTAMP[]
) RETURNING id, rollenname, erstellt_am, geaendert_am;
"#;
let typen = sqlx::query_as::<_, model::Rolle>(QUERY)
.bind(rolle_id)
.bind(id)
.bind(rollenname)
.bind(erstellt_am)
.bind(geaendert_am)
.fetch_all(db)
.await?;
Ok(typen)
}
}

View File

@@ -1,6 +1,7 @@
mod rolle_alle;
mod rolle_dataloader;
mod rolle_erstellen;
mod rolle_erstellen_viele;
mod rolle_loeschen;
mod rolle_update;

View File

@@ -0,0 +1,36 @@
use anyhow::Error;
use chrono::Utc;
use super::Service;
use crate::{
domain::rolle::{
entity,
model::{self, RolleErstelleInput},
},
scalar::Ulid,
};
impl Service {
pub async fn rolle_erstellen_viele(
&self,
input: &[RolleErstelleInput],
) -> Result<Vec<model::Rolle>, Error> {
let rollen_erstellen: Vec<entity::RolleErstellen> = input
.iter()
.map(|t| entity::RolleErstellen {
rolle_id: ulid::Ulid::new().into(),
id: Ulid(ulid::Ulid::new()),
rollenname: t.rollenname.clone(),
erstellt_am: Utc::now(),
geaendert_am: Utc::now(),
})
.collect();
let typen = self
.repo
.rolle_viele_erstellen(&self.db, &rollen_erstellen)
.await?;
Ok(typen)
}
}