refactoring delete models
This commit is contained in:
parent
534d819865
commit
85f1840d7b
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_graphql::SimpleObject;
|
use async_graphql::{InputObject, SimpleObject};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, PgPool};
|
use sqlx::{FromRow, PgPool};
|
||||||
|
|
||||||
|
@ -12,21 +12,18 @@ pub struct Hersteller {
|
||||||
name: String,
|
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 {
|
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> {
|
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<Hersteller> {
|
||||||
let row = sqlx::query_as!(
|
let row = sqlx::query_as!(
|
||||||
Hersteller,
|
Hersteller,
|
||||||
|
@ -47,25 +44,37 @@ impl Hersteller {
|
||||||
Ok(rows)
|
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!(
|
sqlx::query!(
|
||||||
"UPDATE hersteller SET name=$1 WHERE hersteller_id = $2",
|
"UPDATE hersteller SET name=$1 WHERE hersteller_id = $2",
|
||||||
name,
|
input.name,
|
||||||
id
|
input.hersteller_id
|
||||||
)
|
)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let t = Hersteller::read_one(pool, id).await?;
|
let t = Hersteller::read_one(pool, &input.hersteller_id).await?;
|
||||||
|
|
||||||
Ok(t)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> {
|
pub async fn delete(pool: &PgPool, id: &i32) -> Result<bool> {
|
||||||
sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id)
|
let result = sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(result.rows_affected() > 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,12 +101,11 @@ impl Modell {
|
||||||
Ok(t)
|
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)
|
let result = sqlx::query!("DELETE FROM modelle WHERE modell_id = $1", id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
println!("{:#?}", result);
|
Ok(result.rows_affected() > 0)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_graphql::SimpleObject;
|
use async_graphql::{InputObject, SimpleObject};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, PgPool, Type};
|
use sqlx::{FromRow, PgPool, Type};
|
||||||
|
|
||||||
|
@ -11,18 +11,18 @@ pub struct Typ {
|
||||||
pub name: String,
|
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 {
|
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> {
|
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)
|
let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE typ_id = $1", id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
|
@ -39,21 +39,37 @@ impl Typ {
|
||||||
Ok(rows)
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result<Typ> {
|
pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result<Typ> {
|
||||||
sqlx::query!("UPDATE typen SET name=$1 WHERE typ_id = $2", name, id)
|
let row = sqlx::query!(
|
||||||
.execute(pool)
|
"INSERT INTO typen(name) VALUES ($1) RETURNING typ_id",
|
||||||
.await?;
|
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)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> {
|
pub async fn delete(pool: &PgPool, id: &i32) -> Result<bool> {
|
||||||
sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id)
|
let result = sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(result.rows_affected() > 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::models::hersteller::Hersteller;
|
use crate::models::hersteller::{Hersteller, HerstellerCreateInput, HerstellerUpdateInput};
|
||||||
use async_graphql::{Context, FieldResult};
|
use async_graphql::{Context, FieldResult};
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
|
||||||
|
@ -7,28 +7,28 @@ pub struct HerstellerMutation;
|
||||||
|
|
||||||
#[async_graphql::Object]
|
#[async_graphql::Object]
|
||||||
impl HerstellerMutation {
|
impl HerstellerMutation {
|
||||||
async fn create_hersteller(&self, ctx: &Context<'_>, name: String) -> FieldResult<Hersteller> {
|
async fn create_hersteller(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
input: HerstellerCreateInput,
|
||||||
|
) -> FieldResult<Hersteller> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Hersteller::create(pool, &name).await?;
|
let row = Hersteller::create(pool, &input).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
|
||||||
let pool = ctx.data::<PgPool>()?;
|
|
||||||
|
|
||||||
Hersteller::delete(pool, &id).await?;
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn update_hersteller(
|
async fn update_hersteller(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
id: i32,
|
input: HerstellerUpdateInput,
|
||||||
name: String,
|
|
||||||
) -> FieldResult<Hersteller> {
|
) -> FieldResult<Hersteller> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
let row = Hersteller::update(pool, &input).await?;
|
||||||
let row = Hersteller::update(pool, &id, &name).await?;
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
||||||
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
Ok(Hersteller::delete(pool, &id).await?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,8 @@ impl ModellMutation {
|
||||||
let row = Modell::update(pool, &modell).await?;
|
let row = Modell::update(pool, &modell).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
async fn delete_modell(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
Ok(Modell::delete(pool, &id).await?)
|
||||||
Modell::delete(pool, &id).await?;
|
|
||||||
Ok(true)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::models::typ::Typ;
|
use crate::models::typ::{Typ, TypCreateInput, TypUpdateInput};
|
||||||
use async_graphql::{Context, FieldResult};
|
use async_graphql::{Context, FieldResult};
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
|
||||||
|
@ -7,23 +7,20 @@ pub struct TypMutation;
|
||||||
|
|
||||||
#[async_graphql::Object]
|
#[async_graphql::Object]
|
||||||
impl TypMutation {
|
impl TypMutation {
|
||||||
async fn create_typ(&self, ctx: &Context<'_>, name: String) -> FieldResult<Typ> {
|
async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> FieldResult<Typ> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
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<Typ> {
|
||||||
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
let row = Typ::update(pool, input).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
Ok(Typ::delete(pool, &id).await?)
|
||||||
Typ::delete(pool, &id).await?;
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn update_typ(&self, ctx: &Context<'_>, id: i32, name: String) -> FieldResult<Typ> {
|
|
||||||
let pool = ctx.data::<PgPool>()?;
|
|
||||||
|
|
||||||
let row = Typ::update(pool, &id, &name).await?;
|
|
||||||
Ok(row)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,6 @@ pub struct HerstellerQuery {
|
||||||
|
|
||||||
#[Object(extends)]
|
#[Object(extends)]
|
||||||
impl HerstellerQuery {
|
impl HerstellerQuery {
|
||||||
// #[graphql(external)]
|
|
||||||
// async fn id(&self) -> &i32 {
|
|
||||||
// &self.id
|
|
||||||
// }
|
|
||||||
|
|
||||||
// async fn modell<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Modell>> {
|
|
||||||
// let pool = ctx.data::<PgPool>().unwrap();
|
|
||||||
// let rows = Modell::read_by_typ(pool, &self.id).await?;
|
|
||||||
// Ok(rows)
|
|
||||||
// }
|
|
||||||
|
|
||||||
async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Hersteller>> {
|
async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Hersteller>> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let rows = Hersteller::read_all(pool).await?;
|
let rows = Hersteller::read_all(pool).await?;
|
||||||
|
|
|
@ -10,16 +10,6 @@ pub struct TypQuery {
|
||||||
|
|
||||||
#[Object(extends)]
|
#[Object(extends)]
|
||||||
impl TypQuery {
|
impl TypQuery {
|
||||||
// #[graphql(external)]
|
|
||||||
// async fn id(&self) -> &i32 {
|
|
||||||
// &self.id
|
|
||||||
// }
|
|
||||||
// async fn modell<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Modell>> {
|
|
||||||
// let pool = ctx.data::<PgPool>().unwrap();
|
|
||||||
// let rows = Modell::read_by_typ(pool, &self.id).await?;
|
|
||||||
// Ok(rows)
|
|
||||||
// }
|
|
||||||
|
|
||||||
async fn typen<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Typ>> {
|
async fn typen<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Typ>> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let rows = Typ::read_all(pool).await?;
|
let rows = Typ::read_all(pool).await?;
|
||||||
|
@ -31,11 +21,4 @@ impl TypQuery {
|
||||||
let row = Typ::read_one(pool, &id).await?;
|
let row = Typ::read_one(pool, &id).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[graphql(entity)]
|
|
||||||
// async fn find_typ_by_id<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<Typ> {
|
|
||||||
// let pool = ctx.data::<PgPool>().unwrap();
|
|
||||||
// let row = Typ::read_one(pool, &id).await?;
|
|
||||||
// Ok(row)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue