typ refactor

This commit is contained in:
2026-06-03 22:08:44 +02:00
parent 16f8a929d2
commit b3b7b75ea8
22 changed files with 92 additions and 64 deletions

View File

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

View File

@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS hersteller (
id UUID PRIMARY KEY,
hersteller_id UUID PRIMARY KEY,
id CHAR(26) UNIQUE NOT NULL,
herstellername VARCHAR NOT NULL,
erstellt_am TIMESTAMP WITH TIME ZONE NOT NULL,
geaendert_am TIMESTAMP WITH TIME ZONE NOT NULL

View File

@@ -5,12 +5,12 @@ CREATE TABLE IF NOT EXISTS modelle (
hersteller_id UUID,
CONSTRAINT fk_typ FOREIGN KEY (typ_id)
REFERENCES typen (id)
REFERENCES typen (typ_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id)
REFERENCES hersteller (id)
REFERENCES hersteller (hersteller_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)

View File

@@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS liegenschaften (
id_liegenschaft UUID PRIMARY KEY,
id VARCHAR UNIQUE,
id CHAR(26) UNIQUE,
liegenschaftname VARCHAR NOT NULL,
erstellt_am TIMESTAMP WITH TIME ZONE NOT NULL,
geaendert_am TIMESTAMP WITH TIME ZONE NOT NULL

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

@@ -1,2 +1,6 @@
pub mod typ;
pub use typ::Typ;
// pub use typ::Typ;
pub use typ::TypErstellen;
pub use typ::TypLoeschen;
pub use typ::TypUpdate;

View File

@@ -1,8 +1,19 @@
use crate::scalar::{Id, Time};
use crate::scalar::{Id, Time, Ulid};
pub struct Typ {
pub id: Id,
pub struct TypErstellen {
pub typ_id: Id,
pub id: Ulid,
pub typname: String,
pub erstellt_am: Option<Time>,
pub geaendert_am: Option<Time>,
pub erstellt_am: Time,
pub geaendert_am: Time,
}
pub struct TypLoeschen {
pub id: Ulid,
}
pub struct TypUpdate {
pub id: Ulid,
pub typname: String,
pub geaendert_am: Time,
}

View File

@@ -1,20 +1,20 @@
use async_graphql::{ComplexObject, SimpleObject};
use crate::scalar::{Id, Time};
use crate::scalar::{Time, Ulid};
#[derive(sqlx::FromRow, SimpleObject, Clone, Debug)]
#[graphql(complex)]
pub struct Typ {
/// Die UUID einer Typ
pub id: Id,
/// Die Ulid eines Gerätetypen
pub id: Ulid,
/// Der Typname
pub typname: String,
/// Wann die Typ erstellt wurde
/// Wann der Typ erstellt wurde
pub erstellt_am: Time,
/// Wann die Typ geaendert wurde
/// Wann der Typ geaendert wurde
pub geaendert_am: Time,
}

View File

@@ -1,9 +1,9 @@
use async_graphql::InputObject;
use crate::scalar::Id;
use crate::scalar::Ulid;
#[derive(InputObject)]
pub struct TypLoeschenInput {
/// Die ID einer Typ
pub id: Id,
/// Die ULID von einem Typ
pub id: Ulid,
}

View File

@@ -1,10 +1,11 @@
use crate::scalar::Id;
use async_graphql::InputObject;
use crate::scalar::Ulid;
#[derive(InputObject)]
pub struct TypUpdateInput {
/// Die ID einer Typ
pub id: Id,
/// Die ULID von einem Typen
pub id: Ulid,
/// Der Name einer Typ
pub typname: String,

View File

@@ -0,0 +1,3 @@
pub mod typ;
pub use typ::TypMutation;

View File

@@ -12,9 +12,9 @@ pub struct TypMutation;
#[async_graphql::Object]
impl TypMutation {
async fn erstelle_typ(&self, ctx: &Context<'_>, input: TypErstelleInput) -> FieldResult<Typ> {
let pool = ctx.data::<PgPool>()?;
let pool = ctx.data::<PgPool>()?.clone();
let typ = Service::new(pool.clone()).typ_erstellen(input).await?;
let typ = Service::new(pool).typ_erstellen(input).await?;
Ok(typ)
}
@@ -40,6 +40,7 @@ impl TypMutation {
async fn loesche_typ(&self, ctx: &Context<'_>, input: TypLoeschenInput) -> FieldResult<Typ> {
let pool = ctx.data::<PgPool>()?;
let typ = Service::new(pool.clone()).typ_loeschen(input).await?;
Ok(typ)
}

View File

@@ -4,30 +4,33 @@ use super::Repository;
use crate::database::Queryer;
use crate::domain::typ::entity;
use crate::domain::typ::model::{self};
use crate::scalar::{Id, Time};
use crate::scalar::{Id, Time, Ulid};
impl Repository {
pub async fn typ_viele_erstellen<'c, C: Queryer<'c>>(
&self,
db: C,
typen: &[entity::Typ], // input: &[Typ],
typen: &[entity::TypErstellen], // input: &[Typ],
) -> Result<Vec<model::Typ>, Error> {
let id: Vec<Id> = typen.iter().map(|t| t.id).collect();
let typ_id: Vec<Id> = typen.iter().map(|t| t.typ_id).collect();
let id: Vec<Ulid> = typen.iter().map(|t| t.id.to_string()).collect();
let typnamen: Vec<String> = typen.iter().map(|t| t.typname.clone()).collect();
let erstellt_am: Vec<Option<Time>> = typen.iter().map(|t| t.erstellt_am).collect();
let geaendert_am: Vec<Option<Time>> = typen.iter().map(|t| t.geaendert_am).collect();
let erstellt_am: Vec<Time> = typen.iter().map(|t| t.erstellt_am).collect();
let geaendert_am: Vec<Time> = typen.iter().map(|t| t.geaendert_am).collect();
const QUERY: &str = r#"
INSERT INTO typen (id, typname, erstellt_am, geaendert_am)
INSERT INTO typen (typ_id, id, typname, erstellt_am, geaendert_am)
SELECT * FROM UNNEST(
$1::uuid[],
$2::text[],
$3::TIMESTAMP[],
$4::TIMESTAMP[]
$3::text[],
$4::TIMESTAMP[],
$5::TIMESTAMP[]
) RETURNING id, erstellt_am, geaendert_am, typname;
"#;
let rows = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(typ_id)
.bind(id)
.bind(typnamen)
.bind(erstellt_am)

View File

@@ -8,16 +8,17 @@ impl Repository {
pub async fn typ_erstellen<'c, C: Queryer<'c>>(
&self,
db: C,
typ: &entity::Typ,
typ: &entity::TypErstellen,
) -> Result<model::Typ, Error> {
const QUERY: &str = r#"
INSERT INTO typen (id, erstellt_am, geaendert_am, typname) VALUES (
$1, $2, $3, $4
INSERT INTO typen (typ_id, id, erstellt_am, geaendert_am, typname) VALUES (
$1, $2, $3, $4, $5
) RETURNING id, erstellt_am, geaendert_am, typname;
"#;
let typ = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(typ.id)
.bind(typ.typ_id)
.bind(typ.id.to_string())
.bind(typ.erstellt_am)
.bind(typ.geaendert_am)
.bind(&typ.typname)

View File

@@ -8,14 +8,14 @@ impl Repository {
pub async fn typ_loeschen<'c, C: Queryer<'c>>(
&self,
db: C,
typ: &entity::Typ,
typ: &entity::TypLoeschen,
) -> Result<model::Typ, Error> {
const QUERY: &str = r#"
DELETE FROM typen WHERE id=$1 RETURNING id, erstellt_am, geaendert_am, typname;
"#;
let typ = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(typ.id)
.bind(typ.id.to_string())
.fetch_one(db)
.await?;

View File

@@ -8,7 +8,7 @@ impl Repository {
pub async fn typ_update<'c, C: Queryer<'c>>(
&self,
db: C,
typ: &entity::Typ,
typ: &entity::TypUpdate,
) -> Result<model::Typ, Error> {
const QUERY: &str = r#"
UPDATE typen
@@ -17,7 +17,7 @@ impl Repository {
"#;
let typ = sqlx::query_as::<_, model::Typ>(QUERY)
.bind(typ.id)
.bind(typ.id.to_string())
.bind(typ.geaendert_am)
.bind(&typ.typname)
.fetch_one(db)

View File

@@ -10,14 +10,15 @@ use crate::domain::typ::{
impl Service {
pub async fn typ_erstellen(&self, input: TypErstelleInput) -> Result<model::Typ, Error> {
let typ_input = entity::Typ {
id: Ulid::new().into(),
let typ_erstellen = entity::TypErstellen {
typ_id: Ulid::new().into(),
id: Ulid::new().to_string(),
typname: input.typname,
erstellt_am: Some(Utc::now()),
geaendert_am: Some(Utc::now()),
erstellt_am: Utc::now(),
geaendert_am: Utc::now(),
};
let typ = self.repo.typ_erstellen(&self.db, &typ_input).await?;
let typ = self.repo.typ_erstellen(&self.db, &typ_erstellen).await?;
Ok(typ)
}
}

View File

@@ -13,19 +13,20 @@ impl Service {
&self,
input: &[TypErstelleInput],
) -> Result<Vec<model::Typ>, Error> {
let typen_input: Vec<entity::Typ> = input
let typen_erstellen: Vec<entity::TypErstellen> = input
.iter()
.map(|t| entity::Typ {
id: Ulid::new().into(),
.map(|t| entity::TypErstellen {
typ_id: Ulid::new().into(),
id: Ulid::new().to_string(),
typname: t.typname.clone(),
erstellt_am: Some(Utc::now()),
geaendert_am: Some(Utc::now()),
erstellt_am: Utc::now(),
geaendert_am: Utc::now(),
})
.collect();
let typen = self
.repo
.typ_viele_erstellen(&self.db, &typen_input)
.typ_viele_erstellen(&self.db, &typen_erstellen)
.await?;
Ok(typen)

View File

@@ -1,5 +1,4 @@
use anyhow::Error;
use chrono::Utc;
use super::Service;
use crate::domain::typ::{
@@ -9,14 +8,9 @@ use crate::domain::typ::{
impl Service {
pub async fn typ_loeschen(&self, input: TypLoeschenInput) -> Result<model::Typ, Error> {
let typ_input = entity::Typ {
id: input.id,
typname: String::new(),
erstellt_am: None,
geaendert_am: Some(Utc::now()),
};
let typ_loeschen = entity::TypLoeschen { id: input.id };
let typ = self.repo.typ_loeschen(&self.db, &typ_input).await?;
let typ = self.repo.typ_loeschen(&self.db, &typ_loeschen).await?;
Ok(typ)
}
}

View File

@@ -9,11 +9,10 @@ use crate::domain::typ::{
impl Service {
pub async fn typ_update(&self, input: TypUpdateInput) -> Result<model::Typ, Error> {
let typ_input = entity::Typ {
let typ_input = entity::TypUpdate {
id: input.id,
typname: input.typname,
erstellt_am: None,
geaendert_am: Some(Utc::now()),
geaendert_am: Utc::now(),
};
let typ = self.repo.typ_update(&self.db, &typ_input).await?;

View File

@@ -4,13 +4,14 @@ pub mod gruppe;
// pub mod modell;
pub mod liegenschaft;
pub mod rolle;
pub mod typ;
use async_graphql::MergedObject;
use crate::domain::typ::mutation::TypMutation;
#[derive(MergedObject, Default)]
pub struct Mutation(
typ::TypMutation,
TypMutation,
// hersteller::HerstellerMutation,
// modell::ModellMutation,
benutzer::BenutzerMutation,

View File

@@ -8,3 +8,8 @@ pub type Time = chrono::DateTime<chrono::Utc>;
/// The ID type appears in a JSON response as a String; however, it is not intended to be human-readable.
/// When expected as an input type, any string (such as "4") or integer (such as 4) input value will be accepted as an ID.
pub type Id = Uuid;
/// ödsklfjsdf
/// debug_assert_eq!(ödsklfjsdfasdfas
/// ödsklfjsdfasdfasdfsa,
pub type Ulid = String;