diff --git a/src/models/typ.rs b/src/models/typ.rs index 6805ad1..8ebecfd 100644 --- a/src/models/typ.rs +++ b/src/models/typ.rs @@ -24,9 +24,8 @@ pub struct TypUpdateInput { impl Typ { pub async fn read_one(pool: &PgPool, id: &i32) -> Result { - let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE id = $1", id) - .fetch_one(pool) - .await?; + const QUERY: &str = "SELECT * FROM typen WHERE id = $1"; + let row: Typ = sqlx::query_as(QUERY).bind(id).fetch_one(pool).await?; Ok(row) } @@ -39,37 +38,36 @@ impl Typ { Ok(rows) } - pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result { - let row = sqlx::query!( - "INSERT INTO typen(name) VALUES ($1) RETURNING id", - input.name - ) - .fetch_one(pool) - .await?; + pub async fn create(pool: &PgPool, input: &TypCreateInput) -> Result { + const QUERY: &str = "INSERT INTO typen (name) VALUES ($1) RETURNING id, name"; - let result = Self::read_one(pool, &row.id).await?; + let result: Typ = sqlx::query_as(QUERY) + .bind(&input.name) + .fetch_one(pool) + .await?; Ok(result) } - pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> 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( + let row = sqlx::query_as!( + Typ, r#" INSERT INTO typen(name) - SELECT * FROM UNNEST($1) + SELECT * FROM UNNEST($1::text[]) RETURNING id, name"#, + &v1 as _ ) - .bind(&v1) - .fetch_one(pool) + .fetch_all(pool) .await?; Ok(row) } - pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result { + pub async fn update(pool: &PgPool, input: &TypUpdateInput) -> Result { sqlx::query!( "UPDATE typen SET name=$1 WHERE id = $2", input.name, diff --git a/src/mutations/typ.rs b/src/mutations/typ.rs index 5bdc6e1..8920a46 100644 --- a/src/mutations/typ.rs +++ b/src/mutations/typ.rs @@ -9,24 +9,23 @@ pub struct TypMutation; impl TypMutation { async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> FieldResult { let pool = ctx.data::()?; - let row = Typ::create(pool, input).await?; + let row = Typ::create(pool, &input).await?; Ok(row) } - async fn create_viele_typen( + async fn create_many_typen( &self, ctx: &Context<'_>, input: Vec, - ) -> FieldResult { + ) -> 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?; + let row = Typ::update(pool, &input).await?; Ok(row) } diff --git a/src/queries/hersteller.rs b/src/queries/hersteller.rs index c8297c4..7b6ad83 100644 --- a/src/queries/hersteller.rs +++ b/src/queries/hersteller.rs @@ -4,21 +4,19 @@ use sqlx::postgres::PgPool; use crate::models::hersteller::Hersteller; #[derive(Default)] -pub struct HerstellerQuery { - pub id: i32, -} +pub struct HerstellerQuery {} #[Object(extends)] impl HerstellerQuery { - async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { - let pool = ctx.data::()?; - let rows = Hersteller::read_all(pool).await?; - Ok(rows) - } - async fn hersteller<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult { let pool = ctx.data::()?; let row = Hersteller::read_one(pool, &id).await?; Ok(row) } + + async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult> { + let pool = ctx.data::()?; + let rows = Hersteller::read_all(pool).await?; + Ok(rows) + } } diff --git a/src/queries/modell.rs b/src/queries/modell.rs index a0fd573..57ee77a 100644 --- a/src/queries/modell.rs +++ b/src/queries/modell.rs @@ -4,9 +4,7 @@ use sqlx::postgres::PgPool; use crate::models::modell::Modell; #[derive(Default)] -pub struct ModellQuery { - pub id: i32, -} +pub struct ModellQuery {} #[Object(extends)] impl ModellQuery { diff --git a/src/queries/typ.rs b/src/queries/typ.rs index 3af0803..7f4c66d 100644 --- a/src/queries/typ.rs +++ b/src/queries/typ.rs @@ -4,9 +4,7 @@ use sqlx::postgres::PgPool; use crate::models::typ::Typ; #[derive(Default)] -pub struct TypQuery { - pub id: i32, -} +pub struct TypQuery {} #[Object(extends)] impl TypQuery {