refactoring delete models

This commit is contained in:
Peter Schiwy
2024-06-04 11:06:51 +02:00
parent 534d819865
commit 85f1840d7b
8 changed files with 95 additions and 104 deletions

View File

@@ -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<String>,
}
impl Hersteller {
pub async fn create(pool: &PgPool, name: &str) -> Result<Hersteller> {
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<Hersteller> {
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<Hersteller> {
pub async fn create(pool: &PgPool, input: &HerstellerCreateInput) -> Result<Hersteller> {
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<Hersteller> {
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<bool> {
let result = sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id)
.execute(pool)
.await?;
Ok(())
Ok(result.rows_affected() > 0)
}
}

View File

@@ -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<bool> {
let result = sqlx::query!("DELETE FROM modelle WHERE modell_id = $1", id)
.execute(pool)
.await?;
println!("{:#?}", result);
Ok(())
Ok(result.rows_affected() > 0)
}
}

View File

@@ -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<String>,
}
impl Typ {
pub async fn create(pool: &PgPool, name: &str) -> Result<Typ> {
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<Typ> {
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<Typ> {
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<Typ> {
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<Typ> {
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<bool> {
let result = sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id)
.execute(pool)
.await?;
Ok(())
Ok(result.rows_affected() > 0)
}
}