refactor typ

This commit is contained in:
2026-06-04 23:12:32 +02:00
parent 5fb2b3666f
commit 581c8bee88
15 changed files with 46 additions and 55 deletions

View File

@@ -1,6 +1,5 @@
CREATE TABLE IF NOT EXISTS typen ( CREATE TABLE IF NOT EXISTS typen (
typ_id UUID PRIMARY KEY, typ_id UUID PRIMARY KEY,
-- id CHAR(26) UNIQUE NOT NULL,
id ULID UNIQUE NOT NULL, id ULID UNIQUE NOT NULL,
typname VARCHAR NOT NULL, typname VARCHAR NOT NULL,
erstellt_am TIMESTAMP WITH TIME ZONE NOT NULL, erstellt_am TIMESTAMP WITH TIME ZONE NOT NULL,

View File

@@ -2,20 +2,12 @@ use async_graphql::{ComplexObject, SimpleObject};
use crate::scalar::{Time, Ulid}; 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)] #[derive(SimpleObject, Clone, sqlx::FromRow)]
pub struct Typ { pub struct Typ {
/// Die Ulid eines Gerätetypen /// Die Ulid eines Gerätetypen
pub id: Ulid, pub id: Ulid,
/// Der Typname /// Der Name eines Gerätetyps
pub typname: String, pub typname: String,
/// Wann der Typ erstellt wurde /// Wann der Typ erstellt wurde
@@ -25,16 +17,5 @@ pub struct Typ {
pub geaendert_am: Time, pub geaendert_am: Time,
} }
// impl From<TypDb> 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] #[ComplexObject]
impl Typ {} impl Typ {}

View File

@@ -4,6 +4,6 @@ use crate::scalar::Ulid;
#[derive(InputObject)] #[derive(InputObject)]
pub struct TypLoeschenInput { pub struct TypLoeschenInput {
/// Die ULID von einem Typ /// Die Ulid eines Gerätetypen
pub id: Ulid, pub id: Ulid,
} }

View File

@@ -4,9 +4,9 @@ use crate::scalar::Ulid;
#[derive(InputObject)] #[derive(InputObject)]
pub struct TypUpdateInput { pub struct TypUpdateInput {
/// Die ULID von einem Typen /// Die ULID von einem Typ
pub id: Ulid, pub id: Ulid,
/// Der Name einer Typ /// Der Name eines Gerätetyps
pub typname: String, pub typname: String,
} }

View File

@@ -23,12 +23,10 @@ impl TypMutation {
ctx: &Context<'_>, ctx: &Context<'_>,
input: Vec<TypErstelleInput>, input: Vec<TypErstelleInput>,
) -> FieldResult<Vec<Typ>> { ) -> FieldResult<Vec<Typ>> {
let pool = ctx.data::<PgPool>()?; let pool = ctx.data::<PgPool>()?.clone();
let gruppe = Service::new(pool.clone()) let typen = Service::new(pool).typ_erstellen_viele(&input).await?;
.typ_erstellen_viele(&input) Ok(typen)
.await?;
Ok(gruppe)
} }
async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult<Typ> { async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult<Typ> {
@@ -39,9 +37,9 @@ impl TypMutation {
} }
async fn loesche_typ(&self, ctx: &Context<'_>, input: TypLoeschenInput) -> FieldResult<Typ> { async fn loesche_typ(&self, ctx: &Context<'_>, input: TypLoeschenInput) -> FieldResult<Typ> {
let pool = ctx.data::<PgPool>()?; let pool = ctx.data::<PgPool>()?.clone();
let typ = Service::new(pool.clone()).typ_loeschen(input).await?; let typ = Service::new(pool).typ_loeschen(input).await?;
Ok(typ) Ok(typ)
} }
} }

View File

@@ -1,7 +1,10 @@
use async_graphql::{Context, FieldResult, Object}; use async_graphql::{Context, FieldResult, Object};
use sqlx::postgres::PgPool; use sqlx::postgres::PgPool;
use crate::domain::typ::{model::Typ, service::Service}; use crate::{
domain::typ::{model::Typ, service::Service},
scalar::Ulid,
};
#[derive(Default)] #[derive(Default)]
pub struct TypQuery {} pub struct TypQuery {}
@@ -9,8 +12,14 @@ pub struct TypQuery {}
#[Object(extends)] #[Object(extends)]
impl TypQuery { impl TypQuery {
async fn typen(&self, ctx: &Context<'_>) -> FieldResult<Vec<Typ>> { async fn typen(&self, ctx: &Context<'_>) -> FieldResult<Vec<Typ>> {
let pool = ctx.data::<PgPool>()?; let pool = ctx.data::<PgPool>()?.clone();
let typen = Service::new(pool.clone()).typ_alle().await?; let typen = Service::new(pool).typ_alle().await?;
Ok(typen) Ok(typen)
} }
async fn einen_typen(&self, ctx: &Context<'_>, id: Ulid) -> FieldResult<Typ> {
let pool = ctx.data::<PgPool>()?.clone();
let typ = Service::new(pool).typ_einen_zeigen(id).await?;
Ok(typ)
}
} }

View File

@@ -10,9 +10,6 @@ impl Repository {
"#; "#;
let typen = sqlx::query_as::<_, model::Typ>(QUERY).fetch_all(db).await?; let typen = sqlx::query_as::<_, model::Typ>(QUERY).fetch_all(db).await?;
// let typen: Vec<model::Typ> = typen.into_iter().map(model::Typ::from).collect();
Ok(typen) Ok(typen)
} }
} }

View File

@@ -1,26 +1,25 @@
use anyhow::Error; use anyhow::Error;
use super::Repository; use super::Repository;
use crate::{database::Queryer, domain::typ::model, scalar::Id}; use crate::{database::Queryer, domain::typ::model, scalar::Ulid};
impl Repository { impl Repository {
pub async fn typ_zeige_einen<'c, C: Queryer<'c>>( pub async fn typ_zeige_einen<'c, C: Queryer<'c>>(
&self, &self,
db: C, db: C,
id: Id, id: Ulid,
) -> Result<model::Typ, Error> { ) -> Result<model::Typ, Error> {
const QUERY: &str = r#" const QUERY: &str = r#"
SELECT id, erstellt_am, geaendert_am, typname SELECT id, typname, erstellt_am, geaendert_am
FROM typen FROM typen
WHERE id = $1 WHERE id = $1
"#; "#;
let row = sqlx::query_as::<_, model::Typ>(QUERY) let typ = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(id) .bind(id)
.fetch_one(db) .fetch_one(db)
.await?; .await?;
// .map(model::Typ::from)?;
Ok(row) Ok(typ)
} }
} }

View File

@@ -29,7 +29,7 @@ impl Repository {
) RETURNING id, erstellt_am, geaendert_am, typname; ) 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(typ_id)
.bind(id) .bind(id)
.bind(typnamen) .bind(typnamen)
@@ -38,8 +38,6 @@ impl Repository {
.fetch_all(db) .fetch_all(db)
.await?; .await?;
// let rows: Vec<model::Typ> = rows.into_iter().map(model::Typ::from).collect(); Ok(typen)
Ok(rows)
} }
} }

View File

@@ -24,7 +24,6 @@ impl Repository {
.bind(&typ.typname) .bind(&typ.typname)
.fetch_one(db) .fetch_one(db)
.await?; .await?;
// .map(model::Typ::from)?;
Ok(typ) Ok(typ)
} }

View File

@@ -18,7 +18,6 @@ impl Repository {
.bind(typ.id) .bind(typ.id)
.fetch_one(db) .fetch_one(db)
.await?; .await?;
// .map(model::Typ::from)?;
Ok(typ) Ok(typ)
} }

View File

@@ -12,17 +12,16 @@ impl Repository {
) -> Result<model::Typ, Error> { ) -> Result<model::Typ, Error> {
const QUERY: &str = r#" const QUERY: &str = r#"
UPDATE typen UPDATE typen
SET geaendert_am = $2, typname = $3 WHERE id = $1 SET typname = $2, geaendert_am = $3 WHERE id = $1
RETURNING id, erstellt_am, geaendert_am, typname; RETURNING id, typname, erstellt_am, geaendert_am;
"#; "#;
let typ = sqlx::query_as::<_, model::Typ>(QUERY) let typ = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(typ.id) .bind(typ.id)
.bind(typ.geaendert_am)
.bind(&typ.typname) .bind(&typ.typname)
.bind(typ.geaendert_am)
.fetch_one(db) .fetch_one(db)
.await?; .await?;
// .map(model::Typ::from)?;
Ok(typ) Ok(typ)
} }

View File

@@ -4,6 +4,7 @@ mod typ_erstellen;
mod typ_erstellen_viele; mod typ_erstellen_viele;
mod typ_loeschen; mod typ_loeschen;
mod typ_update; mod typ_update;
mod typ_zeige_einen;
use super::repository::Repository; use super::repository::Repository;
use crate::database::DB; use crate::database::DB;

View File

@@ -6,7 +6,7 @@ use super::Service;
impl Service { impl Service {
pub async fn typ_alle(&self) -> Result<Vec<model::Typ>, Error> { pub async fn typ_alle(&self) -> Result<Vec<model::Typ>, Error> {
let typ = self.repo.typ_alle(&self.db).await?; let typen = self.repo.typ_alle(&self.db).await?;
Ok(typ) Ok(typen)
} }
} }

View File

@@ -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<model::Typ, Error> {
let typ = self.repo.typ_zeige_einen(&self.db, id).await?;
Ok(typ)
}
}