From bb1946bb8342576c73447e287ccbae06f8b4bc8f Mon Sep 17 00:00:00 2001 From: Peter Schiwy Date: Tue, 4 Jun 2024 15:38:07 +0200 Subject: [PATCH] add create_viele_typen --- migrations/20240530193519_typen.up.sql | 2 +- migrations/20240530193528_hersteller.up.sql | 2 +- migrations/20240530193533_modelle.up.sql | 6 +-- src/models/hersteller.rs | 32 ++++++-------- src/models/modell.rs | 48 +++++++++++---------- src/models/typ.rs | 41 ++++++++++++------ src/mutations/modell.rs | 8 ++-- src/mutations/typ.rs | 11 +++++ 8 files changed, 89 insertions(+), 61 deletions(-) diff --git a/migrations/20240530193519_typen.up.sql b/migrations/20240530193519_typen.up.sql index 7eb3eec..e796472 100644 --- a/migrations/20240530193519_typen.up.sql +++ b/migrations/20240530193519_typen.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS typen ( - typ_id SERIAL PRIMARY KEY, + id SERIAL PRIMARY KEY, name VARCHAR NOT NULL ); diff --git a/migrations/20240530193528_hersteller.up.sql b/migrations/20240530193528_hersteller.up.sql index 227d36b..01b5ce6 100644 --- a/migrations/20240530193528_hersteller.up.sql +++ b/migrations/20240530193528_hersteller.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS hersteller ( - hersteller_id SERIAL PRIMARY KEY, + id SERIAL PRIMARY KEY, name VARCHAR NOT NULL ); diff --git a/migrations/20240530193533_modelle.up.sql b/migrations/20240530193533_modelle.up.sql index ed55bca..730f61a 100644 --- a/migrations/20240530193533_modelle.up.sql +++ b/migrations/20240530193533_modelle.up.sql @@ -1,9 +1,9 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS modelle ( - modell_id SERIAL PRIMARY KEY, + id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, typ_id SERIAL, hersteller_id SERIAL, - CONSTRAINT fk_typ FOREIGN KEY(typ_id) REFERENCES typen(typ_id) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(hersteller_id) ON DELETE NO ACTION ON UPDATE NO ACTION + CONSTRAINT fk_typ FOREIGN KEY(typ_id) REFERENCES typen(id) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(id) ON DELETE NO ACTION ON UPDATE NO ACTION ) diff --git a/src/models/hersteller.rs b/src/models/hersteller.rs index 4bd7da2..a3d3254 100644 --- a/src/models/hersteller.rs +++ b/src/models/hersteller.rs @@ -6,38 +6,34 @@ use sqlx::{FromRow, PgPool}; #[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, sqlx::Type)] pub struct Hersteller { /// Die Datenbank-ID - pub hersteller_id: i32, + pub id: i32, /// Der Name eines Herstellers name: String, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct HerstellerCreateInput { pub name: String, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct HerstellerUpdateInput { - pub hersteller_id: i32, + pub id: i32, pub name: Option, } impl Hersteller { pub async fn read_one(pool: &PgPool, id: &i32) -> Result { - let row = sqlx::query_as!( - Hersteller, - "SELECT * FROM hersteller WHERE hersteller_id = $1", - id - ) - .fetch_one(pool) - .await?; + let row = sqlx::query_as!(Hersteller, "SELECT * FROM hersteller WHERE id = $1", id) + .fetch_one(pool) + .await?; Ok(row) } pub async fn read_all(pool: &PgPool) -> Result> { - let rows = sqlx::query_as!(Hersteller, "SELECT hersteller_id, name FROM hersteller") + let rows = sqlx::query_as!(Hersteller, "SELECT id, name FROM hersteller") .fetch_all(pool) .await?; @@ -46,32 +42,32 @@ impl Hersteller { pub async fn create(pool: &PgPool, input: &HerstellerCreateInput) -> Result { let row = sqlx::query!( - "INSERT INTO hersteller(name) VALUES ($1) RETURNING hersteller_id", + "INSERT INTO hersteller(name) VALUES ($1) RETURNING id", input.name ) .fetch_one(pool) .await?; - let result = Self::read_one(pool, &row.hersteller_id).await?; + let result = Self::read_one(pool, &row.id).await?; Ok(result) } pub async fn update(pool: &PgPool, input: &HerstellerUpdateInput) -> Result { sqlx::query!( - "UPDATE hersteller SET name=$1 WHERE hersteller_id = $2", + "UPDATE hersteller SET name=$1 WHERE id = $2", input.name, - input.hersteller_id + input.id ) .execute(pool) .await?; - let t = Hersteller::read_one(pool, &input.hersteller_id).await?; + let t = Hersteller::read_one(pool, &input.id).await?; Ok(t) } pub async fn delete(pool: &PgPool, id: &i32) -> Result { - let result = sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id) + let result = sqlx::query!("DELETE FROM hersteller WHERE id = $1", id) .execute(pool) .await?; diff --git a/src/models/modell.rs b/src/models/modell.rs index 58dfe4f..34e4d27 100644 --- a/src/models/modell.rs +++ b/src/models/modell.rs @@ -5,10 +5,10 @@ use sqlx::{FromRow, PgPool}; use super::{hersteller::Hersteller, typ::Typ}; -#[derive(SimpleObject, FromRow, Deserialize, Serialize)] +#[derive(SimpleObject, FromRow, Deserialize, Serialize, Debug)] pub struct Modell { /// Die Datenbank-ID - pub modell_id: i32, + pub id: i32, /// Der Geräte-Typ z.B. Monitor typ: Typ, @@ -20,16 +20,16 @@ pub struct Modell { name: String, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct ModellCreateInput { pub name: String, pub typ_id: i32, pub hersteller_id: i32, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct ModellUpdateInput { - pub modell_id: i32, + pub id: i32, pub name: Option, pub typ_id: Option, pub hersteller_id: Option, @@ -41,13 +41,13 @@ impl Modell { Modell, r#" select - m.modell_id, m.name, - (t.typ_id, t.name) as "typ!: Typ", - (h.hersteller_id, h.name) as "hersteller!: Hersteller" + m.id, m.name, + (t.id, t.name) as "typ!: Typ", + (h.id, h.name) as "hersteller!: Hersteller" from modelle as m - join typen as t using(typ_id) - join hersteller as h using(hersteller_id) - where m.modell_id = $1; + join typen as t on t.id = m.typ_id + join hersteller as h on h.id = m.hersteller_id + where m.id = $1; "#, id ) @@ -62,12 +62,12 @@ impl Modell { Modell, r#" select - m.modell_id, m.name, - (t.typ_id, t.name) as "typ!: Typ", - (h.hersteller_id, h.name) as "hersteller!: Hersteller" + m.id, m.name, + (t.id, t.name) as "typ!: Typ", + (h.id, h.name) as "hersteller!: Hersteller" from modelle as m - join typen as t using(typ_id) - join hersteller as h using(hersteller_id) + join typen as t on t.id = m.typ_id + join hersteller as h on h.id = m.hersteller_id "# ) .fetch_all(pool) @@ -77,32 +77,36 @@ impl Modell { } pub async fn create(pool: &PgPool, input: &ModellCreateInput) -> Result { - let row = sqlx::query!(r#"INSERT INTO modelle(typ_id, hersteller_id, name) VALUES ($1, $2, $3) RETURNING modell_id"#, input.typ_id, input.hersteller_id, input.name + let row = sqlx::query!( + r#"INSERT INTO modelle(typ_id, hersteller_id, name) VALUES ($1, $2, $3) RETURNING id"#, + input.typ_id, + input.hersteller_id, + input.name ) .fetch_one(pool) .await?; - Self::read_one(pool, &row.modell_id).await + Self::read_one(pool, &row.id).await } pub async fn update(pool: &PgPool, input: &ModellUpdateInput) -> Result { sqlx::query!( - "UPDATE modelle SET name=COALESCE($1, name), typ_id=COALESCE($2, typ_id), hersteller_id=COALESCE($3, hersteller_id) WHERE modell_id = $4", + "UPDATE modelle SET name=COALESCE($1, name), typ_id=COALESCE($2, typ_id), hersteller_id=COALESCE($3, hersteller_id) WHERE id = $4", input.name, input.typ_id, input.hersteller_id, - input.modell_id + input.id ) .execute(pool) .await?; - let t = Modell::read_one(pool, &input.modell_id).await?; + let t = Modell::read_one(pool, &input.id).await?; Ok(t) } pub async fn delete(pool: &PgPool, id: &i32) -> Result { - let result = sqlx::query!("DELETE FROM modelle WHERE modell_id = $1", id) + let result = sqlx::query!("DELETE FROM modelle WHERE id = $1", id) .execute(pool) .await?; diff --git a/src/models/typ.rs b/src/models/typ.rs index adf2f78..6805ad1 100644 --- a/src/models/typ.rs +++ b/src/models/typ.rs @@ -5,26 +5,26 @@ use sqlx::{FromRow, PgPool, Type}; #[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, Type)] pub struct Typ { - pub typ_id: i32, + pub id: i32, /// Name eines Typs pub name: String, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct TypCreateInput { pub name: String, } -#[derive(InputObject)] +#[derive(InputObject, Debug)] pub struct TypUpdateInput { - pub typ_id: i32, + pub id: i32, pub name: Option, } impl Typ { pub async fn read_one(pool: &PgPool, id: &i32) -> Result { - let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE typ_id = $1", id) + let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE id = $1", id) .fetch_one(pool) .await?; @@ -32,7 +32,7 @@ impl Typ { } pub async fn read_all(pool: &PgPool) -> Result> { - let rows = sqlx::query_as!(Typ, "SELECT typ_id, name FROM typen") + let rows = sqlx::query_as!(Typ, "SELECT id, name FROM typen") .fetch_all(pool) .await?; @@ -41,32 +41,49 @@ impl Typ { pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result { let row = sqlx::query!( - "INSERT INTO typen(name) VALUES ($1) RETURNING typ_id", + "INSERT INTO typen(name) VALUES ($1) RETURNING id", input.name ) .fetch_one(pool) .await?; - let result = Self::read_one(pool, &row.typ_id).await?; + let result = Self::read_one(pool, &row.id).await?; Ok(result) } + pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> Result { + let mut v1: Vec<&str> = Vec::new(); + input.iter().for_each(|typ| v1.push(&typ.name)); + + let row = sqlx::query_as( + r#" + INSERT INTO typen(name) + SELECT * FROM UNNEST($1) + RETURNING id, name"#, + ) + .bind(&v1) + .fetch_one(pool) + .await?; + + Ok(row) + } + pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result { sqlx::query!( - "UPDATE typen SET name=$1 WHERE typ_id = $2", + "UPDATE typen SET name=$1 WHERE id = $2", input.name, - input.typ_id + input.id ) .execute(pool) .await?; - let t = Typ::read_one(pool, &input.typ_id).await?; + let t = Typ::read_one(pool, &input.id).await?; Ok(t) } pub async fn delete(pool: &PgPool, id: &i32) -> Result { - let result = sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id) + let result = sqlx::query!("DELETE FROM typen WHERE id = $1", id) .execute(pool) .await?; diff --git a/src/mutations/modell.rs b/src/mutations/modell.rs index a1f878f..ed80077 100644 --- a/src/mutations/modell.rs +++ b/src/mutations/modell.rs @@ -10,20 +10,20 @@ impl ModellMutation { async fn create_modell( &self, ctx: &Context<'_>, - modell: ModellCreateInput, + input: ModellCreateInput, ) -> FieldResult { let pool = ctx.data::()?; - let row = Modell::create(pool, &modell).await?; + let row = Modell::create(pool, &input).await?; Ok(row) } async fn update_modell( &self, ctx: &Context<'_>, - modell: ModellUpdateInput, + input: ModellUpdateInput, ) -> FieldResult { let pool = ctx.data::()?; - let row = Modell::update(pool, &modell).await?; + let row = Modell::update(pool, &input).await?; Ok(row) } async fn delete_modell(&self, ctx: &Context<'_>, id: i32) -> FieldResult { diff --git a/src/mutations/typ.rs b/src/mutations/typ.rs index 294fd88..5bdc6e1 100644 --- a/src/mutations/typ.rs +++ b/src/mutations/typ.rs @@ -13,6 +13,17 @@ impl TypMutation { Ok(row) } + async fn create_viele_typen( + &self, + ctx: &Context<'_>, + input: Vec, + ) -> FieldResult { + let pool = ctx.data::()?; + + let row = Typ::create_many(pool, &input).await?; + Ok(row) + } + async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult { let pool = ctx.data::()?; let row = Typ::update(pool, input).await?;