refactoring

This commit is contained in:
Peter Schiwy 2024-06-07 14:40:44 +02:00
parent bb1946bb83
commit 1d60fc5a3f
5 changed files with 28 additions and 37 deletions

View File

@ -24,9 +24,8 @@ pub struct TypUpdateInput {
impl Typ {
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<Typ> {
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<Typ> {
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<Typ> {
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<Typ> {
pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> Result<Vec<Typ>> {
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<Typ> {
pub async fn update(pool: &PgPool, input: &TypUpdateInput) -> Result<Typ> {
sqlx::query!(
"UPDATE typen SET name=$1 WHERE id = $2",
input.name,

View File

@ -9,24 +9,23 @@ pub struct TypMutation;
impl TypMutation {
async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> FieldResult<Typ> {
let pool = ctx.data::<PgPool>()?;
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<TypCreateInput>,
) -> FieldResult<Typ> {
) -> FieldResult<Vec<Typ>> {
let pool = ctx.data::<PgPool>()?;
let row = Typ::create_many(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?;
let row = Typ::update(pool, &input).await?;
Ok(row)
}

View File

@ -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<Vec<Hersteller>> {
let pool = ctx.data::<PgPool>()?;
let rows = Hersteller::read_all(pool).await?;
Ok(rows)
}
async fn hersteller<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<Hersteller> {
let pool = ctx.data::<PgPool>()?;
let row = Hersteller::read_one(pool, &id).await?;
Ok(row)
}
async fn alle_hersteller<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Hersteller>> {
let pool = ctx.data::<PgPool>()?;
let rows = Hersteller::read_all(pool).await?;
Ok(rows)
}
}

View File

@ -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 {

View File

@ -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 {