From 581c8bee8855891e5a475af7a77f87a03cded8d0 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 4 Jun 2026 23:12:32 +0200 Subject: [PATCH] refactor typ --- migrations/20240530193519_typen.up.sql | 1 - src/domain/typ/model/typ.rs | 21 +------------------ src/domain/typ/model/typ_loeschen_input.rs | 2 +- src/domain/typ/model/typ_update_input.rs | 4 ++-- src/domain/typ/mutation/typ.rs | 12 +++++------ src/domain/typ/queries/typ.rs | 15 ++++++++++--- src/domain/typ/repository/typ_alle.rs | 3 --- src/domain/typ/repository/typ_einen_zeigen.rs | 11 +++++----- .../typ/repository/typ_erstelle_viele.rs | 6 ++---- src/domain/typ/repository/typ_erstellen.rs | 1 - src/domain/typ/repository/typ_loeschen.rs | 1 - src/domain/typ/repository/typ_update.rs | 7 +++---- src/domain/typ/service.rs | 1 + src/domain/typ/service/typ_alle.rs | 4 ++-- src/domain/typ/service/typ_zeige_einen.rs | 12 +++++++++++ 15 files changed, 46 insertions(+), 55 deletions(-) create mode 100644 src/domain/typ/service/typ_zeige_einen.rs diff --git a/migrations/20240530193519_typen.up.sql b/migrations/20240530193519_typen.up.sql index 04f3c4c..a3bf02b 100644 --- a/migrations/20240530193519_typen.up.sql +++ b/migrations/20240530193519_typen.up.sql @@ -1,6 +1,5 @@ CREATE TABLE IF NOT EXISTS typen ( typ_id UUID PRIMARY KEY, - -- id CHAR(26) UNIQUE NOT NULL, id ULID UNIQUE NOT NULL, typname VARCHAR NOT NULL, erstellt_am TIMESTAMP WITH TIME ZONE NOT NULL, diff --git a/src/domain/typ/model/typ.rs b/src/domain/typ/model/typ.rs index 1f8a54b..fe9de70 100644 --- a/src/domain/typ/model/typ.rs +++ b/src/domain/typ/model/typ.rs @@ -2,20 +2,12 @@ use async_graphql::{ComplexObject, SimpleObject}; use crate::scalar::{Time, Ulid}; -// #[derive(Debug, sqlx::FromRow)] -// pub struct TypDb { -// pub id: String, -// pub typname: String, -// pub erstellt_am: Time, -// pub geaendert_am: Time, -// } - #[derive(SimpleObject, Clone, sqlx::FromRow)] pub struct Typ { /// Die Ulid eines Gerätetypen pub id: Ulid, - /// Der Typname + /// Der Name eines Gerätetyps pub typname: String, /// Wann der Typ erstellt wurde @@ -25,16 +17,5 @@ pub struct Typ { pub geaendert_am: Time, } -// impl From for Typ { -// fn from(db: TypDb) -> Self { -// Self { -// id: Ulid(ulid::Ulid::from_string(&db.id).expect("stored ULID is valid")), -// typname: db.typname, -// erstellt_am: db.erstellt_am, -// geaendert_am: db.geaendert_am, -// } -// } -// } - #[ComplexObject] impl Typ {} diff --git a/src/domain/typ/model/typ_loeschen_input.rs b/src/domain/typ/model/typ_loeschen_input.rs index ba3fb5e..e1ad9f4 100644 --- a/src/domain/typ/model/typ_loeschen_input.rs +++ b/src/domain/typ/model/typ_loeschen_input.rs @@ -4,6 +4,6 @@ use crate::scalar::Ulid; #[derive(InputObject)] pub struct TypLoeschenInput { - /// Die ULID von einem Typ + /// Die Ulid eines Gerätetypen pub id: Ulid, } diff --git a/src/domain/typ/model/typ_update_input.rs b/src/domain/typ/model/typ_update_input.rs index cb45fbd..64befe2 100644 --- a/src/domain/typ/model/typ_update_input.rs +++ b/src/domain/typ/model/typ_update_input.rs @@ -4,9 +4,9 @@ use crate::scalar::Ulid; #[derive(InputObject)] pub struct TypUpdateInput { - /// Die ULID von einem Typen + /// Die ULID von einem Typ pub id: Ulid, - /// Der Name einer Typ + /// Der Name eines Gerätetyps pub typname: String, } diff --git a/src/domain/typ/mutation/typ.rs b/src/domain/typ/mutation/typ.rs index 587bf36..9f4ce4d 100644 --- a/src/domain/typ/mutation/typ.rs +++ b/src/domain/typ/mutation/typ.rs @@ -23,12 +23,10 @@ impl TypMutation { ctx: &Context<'_>, input: Vec, ) -> FieldResult> { - let pool = ctx.data::()?; + let pool = ctx.data::()?.clone(); - let gruppe = Service::new(pool.clone()) - .typ_erstellen_viele(&input) - .await?; - Ok(gruppe) + let typen = Service::new(pool).typ_erstellen_viele(&input).await?; + Ok(typen) } async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult { @@ -39,9 +37,9 @@ impl TypMutation { } async fn loesche_typ(&self, ctx: &Context<'_>, input: TypLoeschenInput) -> FieldResult { - let pool = ctx.data::()?; + let pool = ctx.data::()?.clone(); - let typ = Service::new(pool.clone()).typ_loeschen(input).await?; + let typ = Service::new(pool).typ_loeschen(input).await?; Ok(typ) } } diff --git a/src/domain/typ/queries/typ.rs b/src/domain/typ/queries/typ.rs index f83701b..a2a99cc 100644 --- a/src/domain/typ/queries/typ.rs +++ b/src/domain/typ/queries/typ.rs @@ -1,7 +1,10 @@ use async_graphql::{Context, FieldResult, Object}; use sqlx::postgres::PgPool; -use crate::domain::typ::{model::Typ, service::Service}; +use crate::{ + domain::typ::{model::Typ, service::Service}, + scalar::Ulid, +}; #[derive(Default)] pub struct TypQuery {} @@ -9,8 +12,14 @@ pub struct TypQuery {} #[Object(extends)] impl TypQuery { async fn typen(&self, ctx: &Context<'_>) -> FieldResult> { - let pool = ctx.data::()?; - let typen = Service::new(pool.clone()).typ_alle().await?; + let pool = ctx.data::()?.clone(); + let typen = Service::new(pool).typ_alle().await?; Ok(typen) } + + async fn einen_typen(&self, ctx: &Context<'_>, id: Ulid) -> FieldResult { + let pool = ctx.data::()?.clone(); + let typ = Service::new(pool).typ_einen_zeigen(id).await?; + Ok(typ) + } } diff --git a/src/domain/typ/repository/typ_alle.rs b/src/domain/typ/repository/typ_alle.rs index b9c872d..5a524f9 100644 --- a/src/domain/typ/repository/typ_alle.rs +++ b/src/domain/typ/repository/typ_alle.rs @@ -10,9 +10,6 @@ impl Repository { "#; let typen = sqlx::query_as::<_, model::Typ>(QUERY).fetch_all(db).await?; - - // let typen: Vec = typen.into_iter().map(model::Typ::from).collect(); - Ok(typen) } } diff --git a/src/domain/typ/repository/typ_einen_zeigen.rs b/src/domain/typ/repository/typ_einen_zeigen.rs index 92acc24..12603f8 100644 --- a/src/domain/typ/repository/typ_einen_zeigen.rs +++ b/src/domain/typ/repository/typ_einen_zeigen.rs @@ -1,26 +1,25 @@ use anyhow::Error; use super::Repository; -use crate::{database::Queryer, domain::typ::model, scalar::Id}; +use crate::{database::Queryer, domain::typ::model, scalar::Ulid}; impl Repository { pub async fn typ_zeige_einen<'c, C: Queryer<'c>>( &self, db: C, - id: Id, + id: Ulid, ) -> Result { const QUERY: &str = r#" - SELECT id, erstellt_am, geaendert_am, typname + SELECT id, typname, erstellt_am, geaendert_am FROM typen WHERE id = $1 "#; - let row = sqlx::query_as::<_, model::Typ>(QUERY) + let typ = sqlx::query_as::<_, model::Typ>(QUERY) .bind(id) .fetch_one(db) .await?; - // .map(model::Typ::from)?; - Ok(row) + Ok(typ) } } diff --git a/src/domain/typ/repository/typ_erstelle_viele.rs b/src/domain/typ/repository/typ_erstelle_viele.rs index e18097b..2a9063f 100644 --- a/src/domain/typ/repository/typ_erstelle_viele.rs +++ b/src/domain/typ/repository/typ_erstelle_viele.rs @@ -29,7 +29,7 @@ impl Repository { ) RETURNING id, erstellt_am, geaendert_am, typname; "#; - let rows = sqlx::query_as::<_, model::Typ>(QUERY) + let typen = sqlx::query_as::<_, model::Typ>(QUERY) .bind(typ_id) .bind(id) .bind(typnamen) @@ -38,8 +38,6 @@ impl Repository { .fetch_all(db) .await?; - // let rows: Vec = rows.into_iter().map(model::Typ::from).collect(); - - Ok(rows) + Ok(typen) } } diff --git a/src/domain/typ/repository/typ_erstellen.rs b/src/domain/typ/repository/typ_erstellen.rs index 1351ab5..3600f65 100644 --- a/src/domain/typ/repository/typ_erstellen.rs +++ b/src/domain/typ/repository/typ_erstellen.rs @@ -24,7 +24,6 @@ impl Repository { .bind(&typ.typname) .fetch_one(db) .await?; - // .map(model::Typ::from)?; Ok(typ) } diff --git a/src/domain/typ/repository/typ_loeschen.rs b/src/domain/typ/repository/typ_loeschen.rs index aaca52a..4509a42 100644 --- a/src/domain/typ/repository/typ_loeschen.rs +++ b/src/domain/typ/repository/typ_loeschen.rs @@ -18,7 +18,6 @@ impl Repository { .bind(typ.id) .fetch_one(db) .await?; - // .map(model::Typ::from)?; Ok(typ) } diff --git a/src/domain/typ/repository/typ_update.rs b/src/domain/typ/repository/typ_update.rs index 5491a3c..565f08f 100644 --- a/src/domain/typ/repository/typ_update.rs +++ b/src/domain/typ/repository/typ_update.rs @@ -12,17 +12,16 @@ impl Repository { ) -> Result { const QUERY: &str = r#" UPDATE typen - SET geaendert_am = $2, typname = $3 WHERE id = $1 - RETURNING id, erstellt_am, geaendert_am, typname; + SET typname = $2, geaendert_am = $3 WHERE id = $1 + RETURNING id, typname, erstellt_am, geaendert_am; "#; let typ = sqlx::query_as::<_, model::Typ>(QUERY) .bind(typ.id) - .bind(typ.geaendert_am) .bind(&typ.typname) + .bind(typ.geaendert_am) .fetch_one(db) .await?; - // .map(model::Typ::from)?; Ok(typ) } diff --git a/src/domain/typ/service.rs b/src/domain/typ/service.rs index ef93518..ae2bff8 100644 --- a/src/domain/typ/service.rs +++ b/src/domain/typ/service.rs @@ -4,6 +4,7 @@ mod typ_erstellen; mod typ_erstellen_viele; mod typ_loeschen; mod typ_update; +mod typ_zeige_einen; use super::repository::Repository; use crate::database::DB; diff --git a/src/domain/typ/service/typ_alle.rs b/src/domain/typ/service/typ_alle.rs index 1a581fa..197a0de 100644 --- a/src/domain/typ/service/typ_alle.rs +++ b/src/domain/typ/service/typ_alle.rs @@ -6,7 +6,7 @@ use super::Service; impl Service { pub async fn typ_alle(&self) -> Result, Error> { - let typ = self.repo.typ_alle(&self.db).await?; - Ok(typ) + let typen = self.repo.typ_alle(&self.db).await?; + Ok(typen) } } diff --git a/src/domain/typ/service/typ_zeige_einen.rs b/src/domain/typ/service/typ_zeige_einen.rs new file mode 100644 index 0000000..df27b45 --- /dev/null +++ b/src/domain/typ/service/typ_zeige_einen.rs @@ -0,0 +1,12 @@ +use anyhow::Error; + +use crate::{domain::typ::model, scalar::Ulid}; + +use super::Service; + +impl Service { + pub async fn typ_einen_zeigen(&self, id: Ulid) -> Result { + let typ = self.repo.typ_zeige_einen(&self.db, id).await?; + Ok(typ) + } +}