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

@@ -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<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]
impl Typ {}

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -23,12 +23,10 @@ impl TypMutation {
ctx: &Context<'_>,
input: Vec<TypErstelleInput>,
) -> FieldResult<Vec<Typ>> {
let pool = ctx.data::<PgPool>()?;
let pool = ctx.data::<PgPool>()?.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<Typ> {
@@ -39,9 +37,9 @@ impl TypMutation {
}
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)
}
}

View File

@@ -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<Vec<Typ>> {
let pool = ctx.data::<PgPool>()?;
let typen = Service::new(pool.clone()).typ_alle().await?;
let pool = ctx.data::<PgPool>()?.clone();
let typen = Service::new(pool).typ_alle().await?;
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: Vec<model::Typ> = typen.into_iter().map(model::Typ::from).collect();
Ok(typen)
}
}

View File

@@ -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<model::Typ, Error> {
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)
}
}

View File

@@ -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<model::Typ> = rows.into_iter().map(model::Typ::from).collect();
Ok(rows)
Ok(typen)
}
}

View File

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

View File

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

View File

@@ -12,17 +12,16 @@ impl Repository {
) -> Result<model::Typ, Error> {
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)
}

View File

@@ -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;

View File

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