From 85f1840d7b152702e73067c3969503c345381bc8 Mon Sep 17 00:00:00 2001 From: Peter Schiwy Date: Tue, 4 Jun 2024 11:06:51 +0200 Subject: [PATCH] refactoring delete models --- src/models/hersteller.rs | 53 ++++++++++++++++++++--------------- src/models/modell.rs | 5 ++-- src/models/typ.rs | 56 ++++++++++++++++++++++++------------- src/mutations/hersteller.rs | 28 +++++++++---------- src/mutations/modell.rs | 6 ++-- src/mutations/typ.rs | 23 +++++++-------- src/queries/hersteller.rs | 11 -------- src/queries/typ.rs | 17 ----------- 8 files changed, 95 insertions(+), 104 deletions(-) diff --git a/src/models/hersteller.rs b/src/models/hersteller.rs index cb56bc7..4bd7da2 100644 --- a/src/models/hersteller.rs +++ b/src/models/hersteller.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use async_graphql::SimpleObject; +use async_graphql::{InputObject, SimpleObject}; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, PgPool}; @@ -12,21 +12,18 @@ pub struct Hersteller { name: String, } +#[derive(InputObject)] +pub struct HerstellerCreateInput { + pub name: String, +} + +#[derive(InputObject)] +pub struct HerstellerUpdateInput { + pub hersteller_id: i32, + pub name: Option, +} + impl Hersteller { - pub async fn create(pool: &PgPool, name: &str) -> Result { - let row = sqlx::query!( - "INSERT INTO hersteller(name) VALUES ($1) RETURNING hersteller_id", - name - ) - .fetch_one(pool) - .await?; - - Ok(Hersteller { - hersteller_id: row.hersteller_id, - name: name.to_string(), - }) - } - pub async fn read_one(pool: &PgPool, id: &i32) -> Result { let row = sqlx::query_as!( Hersteller, @@ -47,25 +44,37 @@ impl Hersteller { Ok(rows) } - pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result { + pub async fn create(pool: &PgPool, input: &HerstellerCreateInput) -> Result { + let row = sqlx::query!( + "INSERT INTO hersteller(name) VALUES ($1) RETURNING hersteller_id", + input.name + ) + .fetch_one(pool) + .await?; + + let result = Self::read_one(pool, &row.hersteller_id).await?; + Ok(result) + } + + pub async fn update(pool: &PgPool, input: &HerstellerUpdateInput) -> Result { sqlx::query!( "UPDATE hersteller SET name=$1 WHERE hersteller_id = $2", - name, - id + input.name, + input.hersteller_id ) .execute(pool) .await?; - let t = Hersteller::read_one(pool, id).await?; + let t = Hersteller::read_one(pool, &input.hersteller_id).await?; Ok(t) } - pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> { - sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id) + pub async fn delete(pool: &PgPool, id: &i32) -> Result { + let result = sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id) .execute(pool) .await?; - Ok(()) + Ok(result.rows_affected() > 0) } } diff --git a/src/models/modell.rs b/src/models/modell.rs index 08a5ded..58dfe4f 100644 --- a/src/models/modell.rs +++ b/src/models/modell.rs @@ -101,12 +101,11 @@ impl Modell { Ok(t) } - pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> { + pub async fn delete(pool: &PgPool, id: &i32) -> Result { let result = sqlx::query!("DELETE FROM modelle WHERE modell_id = $1", id) .execute(pool) .await?; - println!("{:#?}", result); - Ok(()) + Ok(result.rows_affected() > 0) } } diff --git a/src/models/typ.rs b/src/models/typ.rs index 68d584a..adf2f78 100644 --- a/src/models/typ.rs +++ b/src/models/typ.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use async_graphql::SimpleObject; +use async_graphql::{InputObject, SimpleObject}; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, PgPool, Type}; @@ -11,18 +11,18 @@ pub struct Typ { pub name: String, } +#[derive(InputObject)] +pub struct TypCreateInput { + pub name: String, +} + +#[derive(InputObject)] +pub struct TypUpdateInput { + pub typ_id: i32, + pub name: Option, +} + impl Typ { - pub async fn create(pool: &PgPool, name: &str) -> Result { - let row = sqlx::query!("INSERT INTO typen(name) VALUES ($1) RETURNING typ_id", name) - .fetch_one(pool) - .await?; - - Ok(Typ { - typ_id: row.typ_id, - name: name.to_string(), - }) - } - pub async fn read_one(pool: &PgPool, id: &i32) -> Result { let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE typ_id = $1", id) .fetch_one(pool) @@ -39,21 +39,37 @@ impl Typ { Ok(rows) } - pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result { - sqlx::query!("UPDATE typen SET name=$1 WHERE typ_id = $2", name, id) - .execute(pool) - .await?; + pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result { + let row = sqlx::query!( + "INSERT INTO typen(name) VALUES ($1) RETURNING typ_id", + input.name + ) + .fetch_one(pool) + .await?; - let t = Typ::read_one(pool, id).await?; + let result = Self::read_one(pool, &row.typ_id).await?; + Ok(result) + } + + pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result { + sqlx::query!( + "UPDATE typen SET name=$1 WHERE typ_id = $2", + input.name, + input.typ_id + ) + .execute(pool) + .await?; + + let t = Typ::read_one(pool, &input.typ_id).await?; Ok(t) } - pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> { - sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id) + pub async fn delete(pool: &PgPool, id: &i32) -> Result { + let result = sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id) .execute(pool) .await?; - Ok(()) + Ok(result.rows_affected() > 0) } } diff --git a/src/mutations/hersteller.rs b/src/mutations/hersteller.rs index 5536feb..6d49ae0 100644 --- a/src/mutations/hersteller.rs +++ b/src/mutations/hersteller.rs @@ -1,4 +1,4 @@ -use crate::models::hersteller::Hersteller; +use crate::models::hersteller::{Hersteller, HerstellerCreateInput, HerstellerUpdateInput}; use async_graphql::{Context, FieldResult}; use sqlx::postgres::PgPool; @@ -7,28 +7,28 @@ pub struct HerstellerMutation; #[async_graphql::Object] impl HerstellerMutation { - async fn create_hersteller(&self, ctx: &Context<'_>, name: String) -> FieldResult { + async fn create_hersteller( + &self, + ctx: &Context<'_>, + input: HerstellerCreateInput, + ) -> FieldResult { let pool = ctx.data::()?; - let row = Hersteller::create(pool, &name).await?; + let row = Hersteller::create(pool, &input).await?; Ok(row) } - async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult { - let pool = ctx.data::()?; - - Hersteller::delete(pool, &id).await?; - Ok(true) - } - async fn update_hersteller( &self, ctx: &Context<'_>, - id: i32, - name: String, + input: HerstellerUpdateInput, ) -> FieldResult { let pool = ctx.data::()?; - - let row = Hersteller::update(pool, &id, &name).await?; + let row = Hersteller::update(pool, &input).await?; Ok(row) } + + async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult { + let pool = ctx.data::()?; + Ok(Hersteller::delete(pool, &id).await?) + } } diff --git a/src/mutations/modell.rs b/src/mutations/modell.rs index 74a0b44..a1f878f 100644 --- a/src/mutations/modell.rs +++ b/src/mutations/modell.rs @@ -26,10 +26,8 @@ impl ModellMutation { let row = Modell::update(pool, &modell).await?; Ok(row) } - async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult { + async fn delete_modell(&self, ctx: &Context<'_>, id: i32) -> FieldResult { let pool = ctx.data::()?; - - Modell::delete(pool, &id).await?; - Ok(true) + Ok(Modell::delete(pool, &id).await?) } } diff --git a/src/mutations/typ.rs b/src/mutations/typ.rs index b3d1a19..294fd88 100644 --- a/src/mutations/typ.rs +++ b/src/mutations/typ.rs @@ -1,4 +1,4 @@ -use crate::models::typ::Typ; +use crate::models::typ::{Typ, TypCreateInput, TypUpdateInput}; use async_graphql::{Context, FieldResult}; use sqlx::postgres::PgPool; @@ -7,23 +7,20 @@ pub struct TypMutation; #[async_graphql::Object] impl TypMutation { - async fn create_typ(&self, ctx: &Context<'_>, name: String) -> FieldResult { + async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> FieldResult { let pool = ctx.data::()?; - let row = Typ::create(pool, &name).await?; + let row = Typ::create(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?; Ok(row) } async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult { let pool = ctx.data::()?; - - Typ::delete(pool, &id).await?; - Ok(true) - } - - async fn update_typ(&self, ctx: &Context<'_>, id: i32, name: String) -> FieldResult { - let pool = ctx.data::()?; - - let row = Typ::update(pool, &id, &name).await?; - Ok(row) + Ok(Typ::delete(pool, &id).await?) } } diff --git a/src/queries/hersteller.rs b/src/queries/hersteller.rs index d70561a..c8297c4 100644 --- a/src/queries/hersteller.rs +++ b/src/queries/hersteller.rs @@ -10,17 +10,6 @@ pub struct HerstellerQuery { #[Object(extends)] impl HerstellerQuery { - // #[graphql(external)] - // async fn id(&self) -> &i32 { - // &self.id - // } - - // async fn modell<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { - // let pool = ctx.data::().unwrap(); - // let rows = Modell::read_by_typ(pool, &self.id).await?; - // Ok(rows) - // } - async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { let pool = ctx.data::()?; let rows = Hersteller::read_all(pool).await?; diff --git a/src/queries/typ.rs b/src/queries/typ.rs index b08ab61..3af0803 100644 --- a/src/queries/typ.rs +++ b/src/queries/typ.rs @@ -10,16 +10,6 @@ pub struct TypQuery { #[Object(extends)] impl TypQuery { - // #[graphql(external)] - // async fn id(&self) -> &i32 { - // &self.id - // } - // async fn modell<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { - // let pool = ctx.data::().unwrap(); - // let rows = Modell::read_by_typ(pool, &self.id).await?; - // Ok(rows) - // } - async fn typen<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { let pool = ctx.data::()?; let rows = Typ::read_all(pool).await?; @@ -31,11 +21,4 @@ impl TypQuery { let row = Typ::read_one(pool, &id).await?; Ok(row) } - - // #[graphql(entity)] - // async fn find_typ_by_id<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult { - // let pool = ctx.data::().unwrap(); - // let row = Typ::read_one(pool, &id).await?; - // Ok(row) - // } }