refactoring
This commit is contained in:
parent
bb1946bb83
commit
1d60fc5a3f
|
@ -24,9 +24,8 @@ pub struct TypUpdateInput {
|
||||||
|
|
||||||
impl Typ {
|
impl Typ {
|
||||||
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 id = $1", id)
|
const QUERY: &str = "SELECT * FROM typen WHERE id = $1";
|
||||||
.fetch_one(pool)
|
let row: Typ = sqlx::query_as(QUERY).bind(id).fetch_one(pool).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
@ -39,37 +38,36 @@ impl Typ {
|
||||||
Ok(rows)
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result<Typ> {
|
pub async fn create(pool: &PgPool, input: &TypCreateInput) -> Result<Typ> {
|
||||||
let row = sqlx::query!(
|
const QUERY: &str = "INSERT INTO typen (name) VALUES ($1) RETURNING id, name";
|
||||||
"INSERT INTO typen(name) VALUES ($1) RETURNING id",
|
|
||||||
input.name
|
|
||||||
)
|
|
||||||
.fetch_one(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
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)
|
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();
|
let mut v1: Vec<&str> = Vec::new();
|
||||||
input.iter().for_each(|typ| v1.push(&typ.name));
|
input.iter().for_each(|typ| v1.push(&typ.name));
|
||||||
|
|
||||||
let row = sqlx::query_as(
|
let row = sqlx::query_as!(
|
||||||
|
Typ,
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO typen(name)
|
INSERT INTO typen(name)
|
||||||
SELECT * FROM UNNEST($1)
|
SELECT * FROM UNNEST($1::text[])
|
||||||
RETURNING id, name"#,
|
RETURNING id, name"#,
|
||||||
|
&v1 as _
|
||||||
)
|
)
|
||||||
.bind(&v1)
|
.fetch_all(pool)
|
||||||
.fetch_one(pool)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result<Typ> {
|
pub async fn update(pool: &PgPool, input: &TypUpdateInput) -> Result<Typ> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"UPDATE typen SET name=$1 WHERE id = $2",
|
"UPDATE typen SET name=$1 WHERE id = $2",
|
||||||
input.name,
|
input.name,
|
||||||
|
|
|
@ -9,24 +9,23 @@ pub struct TypMutation;
|
||||||
impl TypMutation {
|
impl TypMutation {
|
||||||
async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> 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, input).await?;
|
let row = Typ::create(pool, &input).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_viele_typen(
|
async fn create_many_typen(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
input: Vec<TypCreateInput>,
|
input: Vec<TypCreateInput>,
|
||||||
) -> FieldResult<Typ> {
|
) -> FieldResult<Vec<Typ>> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
|
||||||
let row = Typ::create_many(pool, &input).await?;
|
let row = Typ::create_many(pool, &input).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult<Typ> {
|
async fn update_typ(&self, ctx: &Context<'_>, input: TypUpdateInput) -> FieldResult<Typ> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Typ::update(pool, input).await?;
|
let row = Typ::update(pool, &input).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,21 +4,19 @@ use sqlx::postgres::PgPool;
|
||||||
use crate::models::hersteller::Hersteller;
|
use crate::models::hersteller::Hersteller;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct HerstellerQuery {
|
pub struct HerstellerQuery {}
|
||||||
pub id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Object(extends)]
|
#[Object(extends)]
|
||||||
impl HerstellerQuery {
|
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> {
|
async fn hersteller<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<Hersteller> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Hersteller::read_one(pool, &id).await?;
|
let row = Hersteller::read_one(pool, &id).await?;
|
||||||
Ok(row)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,7 @@ use sqlx::postgres::PgPool;
|
||||||
use crate::models::modell::Modell;
|
use crate::models::modell::Modell;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ModellQuery {
|
pub struct ModellQuery {}
|
||||||
pub id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Object(extends)]
|
#[Object(extends)]
|
||||||
impl ModellQuery {
|
impl ModellQuery {
|
||||||
|
|
|
@ -4,9 +4,7 @@ use sqlx::postgres::PgPool;
|
||||||
use crate::models::typ::Typ;
|
use crate::models::typ::Typ;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TypQuery {
|
pub struct TypQuery {}
|
||||||
pub id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[Object(extends)]
|
#[Object(extends)]
|
||||||
impl TypQuery {
|
impl TypQuery {
|
||||||
|
|
Loading…
Reference in New Issue