Compare commits
No commits in common. "bb1946bb8342576c73447e287ccbae06f8b4bc8f" and "534d819865dfe4645aca0c373dfdce0eb2915d80" have entirely different histories.
bb1946bb83
...
534d819865
|
@ -1,5 +1,5 @@
|
||||||
-- Add up migration script here
|
-- Add up migration script here
|
||||||
CREATE TABLE IF NOT EXISTS typen (
|
CREATE TABLE IF NOT EXISTS typen (
|
||||||
id SERIAL PRIMARY KEY,
|
typ_id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR NOT NULL
|
name VARCHAR NOT NULL
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
-- Add up migration script here
|
-- Add up migration script here
|
||||||
CREATE TABLE IF NOT EXISTS hersteller (
|
CREATE TABLE IF NOT EXISTS hersteller (
|
||||||
id SERIAL PRIMARY KEY,
|
hersteller_id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR NOT NULL
|
name VARCHAR NOT NULL
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
-- Add up migration script here
|
-- Add up migration script here
|
||||||
CREATE TABLE IF NOT EXISTS modelle (
|
CREATE TABLE IF NOT EXISTS modelle (
|
||||||
id SERIAL PRIMARY KEY,
|
modell_id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR NOT NULL,
|
name VARCHAR NOT NULL,
|
||||||
typ_id SERIAL,
|
typ_id SERIAL,
|
||||||
hersteller_id SERIAL,
|
hersteller_id SERIAL,
|
||||||
CONSTRAINT fk_typ FOREIGN KEY(typ_id) REFERENCES typen(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
|
CONSTRAINT fk_typ FOREIGN KEY(typ_id) REFERENCES typen(typ_id) ON DELETE NO ACTION ON UPDATE NO ACTION,
|
||||||
CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(id) ON DELETE NO ACTION ON UPDATE NO ACTION
|
CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(hersteller_id) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,76 +1,71 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_graphql::{InputObject, SimpleObject};
|
use async_graphql::SimpleObject;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, PgPool};
|
use sqlx::{FromRow, PgPool};
|
||||||
|
|
||||||
#[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, sqlx::Type)]
|
#[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, sqlx::Type)]
|
||||||
pub struct Hersteller {
|
pub struct Hersteller {
|
||||||
/// Die Datenbank-ID
|
/// Die Datenbank-ID
|
||||||
pub id: i32,
|
pub hersteller_id: i32,
|
||||||
|
|
||||||
/// Der Name eines Herstellers
|
/// Der Name eines Herstellers
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
|
||||||
pub struct HerstellerCreateInput {
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
|
||||||
pub struct HerstellerUpdateInput {
|
|
||||||
pub 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!(Hersteller, "SELECT * FROM hersteller WHERE id = $1", id)
|
let row = sqlx::query_as!(
|
||||||
.fetch_one(pool)
|
Hersteller,
|
||||||
.await?;
|
"SELECT * FROM hersteller WHERE hersteller_id = $1",
|
||||||
|
id
|
||||||
|
)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read_all(pool: &PgPool) -> Result<Vec<Hersteller>> {
|
pub async fn read_all(pool: &PgPool) -> Result<Vec<Hersteller>> {
|
||||||
let rows = sqlx::query_as!(Hersteller, "SELECT id, name FROM hersteller")
|
let rows = sqlx::query_as!(Hersteller, "SELECT hersteller_id, name FROM hersteller")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(rows)
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(pool: &PgPool, input: &HerstellerCreateInput) -> Result<Hersteller> {
|
pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result<Hersteller> {
|
||||||
let row = sqlx::query!(
|
|
||||||
"INSERT INTO hersteller(name) VALUES ($1) RETURNING id",
|
|
||||||
input.name
|
|
||||||
)
|
|
||||||
.fetch_one(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let result = Self::read_one(pool, &row.id).await?;
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update(pool: &PgPool, input: &HerstellerUpdateInput) -> Result<Hersteller> {
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"UPDATE hersteller SET name=$1 WHERE id = $2",
|
"UPDATE hersteller SET name=$1 WHERE hersteller_id = $2",
|
||||||
input.name,
|
name,
|
||||||
input.id
|
id
|
||||||
)
|
)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let t = Hersteller::read_one(pool, &input.id).await?;
|
let t = Hersteller::read_one(pool, id).await?;
|
||||||
|
|
||||||
Ok(t)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(pool: &PgPool, id: &i32) -> Result<bool> {
|
pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> {
|
||||||
let result = sqlx::query!("DELETE FROM hersteller WHERE id = $1", id)
|
sqlx::query!("DELETE FROM hersteller WHERE hersteller_id = $1", id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(result.rows_affected() > 0)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ use sqlx::{FromRow, PgPool};
|
||||||
|
|
||||||
use super::{hersteller::Hersteller, typ::Typ};
|
use super::{hersteller::Hersteller, typ::Typ};
|
||||||
|
|
||||||
#[derive(SimpleObject, FromRow, Deserialize, Serialize, Debug)]
|
#[derive(SimpleObject, FromRow, Deserialize, Serialize)]
|
||||||
pub struct Modell {
|
pub struct Modell {
|
||||||
/// Die Datenbank-ID
|
/// Die Datenbank-ID
|
||||||
pub id: i32,
|
pub modell_id: i32,
|
||||||
|
|
||||||
/// Der Geräte-Typ z.B. Monitor
|
/// Der Geräte-Typ z.B. Monitor
|
||||||
typ: Typ,
|
typ: Typ,
|
||||||
|
@ -20,16 +20,16 @@ pub struct Modell {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
#[derive(InputObject)]
|
||||||
pub struct ModellCreateInput {
|
pub struct ModellCreateInput {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub typ_id: i32,
|
pub typ_id: i32,
|
||||||
pub hersteller_id: i32,
|
pub hersteller_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
#[derive(InputObject)]
|
||||||
pub struct ModellUpdateInput {
|
pub struct ModellUpdateInput {
|
||||||
pub id: i32,
|
pub modell_id: i32,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub typ_id: Option<i32>,
|
pub typ_id: Option<i32>,
|
||||||
pub hersteller_id: Option<i32>,
|
pub hersteller_id: Option<i32>,
|
||||||
|
@ -41,13 +41,13 @@ impl Modell {
|
||||||
Modell,
|
Modell,
|
||||||
r#"
|
r#"
|
||||||
select
|
select
|
||||||
m.id, m.name,
|
m.modell_id, m.name,
|
||||||
(t.id, t.name) as "typ!: Typ",
|
(t.typ_id, t.name) as "typ!: Typ",
|
||||||
(h.id, h.name) as "hersteller!: Hersteller"
|
(h.hersteller_id, h.name) as "hersteller!: Hersteller"
|
||||||
from modelle as m
|
from modelle as m
|
||||||
join typen as t on t.id = m.typ_id
|
join typen as t using(typ_id)
|
||||||
join hersteller as h on h.id = m.hersteller_id
|
join hersteller as h using(hersteller_id)
|
||||||
where m.id = $1;
|
where m.modell_id = $1;
|
||||||
"#,
|
"#,
|
||||||
id
|
id
|
||||||
)
|
)
|
||||||
|
@ -62,12 +62,12 @@ impl Modell {
|
||||||
Modell,
|
Modell,
|
||||||
r#"
|
r#"
|
||||||
select
|
select
|
||||||
m.id, m.name,
|
m.modell_id, m.name,
|
||||||
(t.id, t.name) as "typ!: Typ",
|
(t.typ_id, t.name) as "typ!: Typ",
|
||||||
(h.id, h.name) as "hersteller!: Hersteller"
|
(h.hersteller_id, h.name) as "hersteller!: Hersteller"
|
||||||
from modelle as m
|
from modelle as m
|
||||||
join typen as t on t.id = m.typ_id
|
join typen as t using(typ_id)
|
||||||
join hersteller as h on h.id = m.hersteller_id
|
join hersteller as h using(hersteller_id)
|
||||||
"#
|
"#
|
||||||
)
|
)
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
|
@ -77,39 +77,36 @@ impl Modell {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(pool: &PgPool, input: &ModellCreateInput) -> Result<Modell> {
|
pub async fn create(pool: &PgPool, input: &ModellCreateInput) -> Result<Modell> {
|
||||||
let row = sqlx::query!(
|
let row = sqlx::query!(r#"INSERT INTO modelle(typ_id, hersteller_id, name) VALUES ($1, $2, $3) RETURNING modell_id"#, input.typ_id, input.hersteller_id, input.name
|
||||||
r#"INSERT INTO modelle(typ_id, hersteller_id, name) VALUES ($1, $2, $3) RETURNING id"#,
|
|
||||||
input.typ_id,
|
|
||||||
input.hersteller_id,
|
|
||||||
input.name
|
|
||||||
)
|
)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Self::read_one(pool, &row.id).await
|
Self::read_one(pool, &row.modell_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(pool: &PgPool, input: &ModellUpdateInput) -> Result<Modell> {
|
pub async fn update(pool: &PgPool, input: &ModellUpdateInput) -> Result<Modell> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"UPDATE modelle SET name=COALESCE($1, name), typ_id=COALESCE($2, typ_id), hersteller_id=COALESCE($3, hersteller_id) WHERE id = $4",
|
"UPDATE modelle SET name=COALESCE($1, name), typ_id=COALESCE($2, typ_id), hersteller_id=COALESCE($3, hersteller_id) WHERE modell_id = $4",
|
||||||
input.name,
|
input.name,
|
||||||
input.typ_id,
|
input.typ_id,
|
||||||
input.hersteller_id,
|
input.hersteller_id,
|
||||||
input.id
|
input.modell_id
|
||||||
)
|
)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let t = Modell::read_one(pool, &input.id).await?;
|
let t = Modell::read_one(pool, &input.modell_id).await?;
|
||||||
|
|
||||||
Ok(t)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(pool: &PgPool, id: &i32) -> Result<bool> {
|
pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> {
|
||||||
let result = sqlx::query!("DELETE FROM modelle WHERE id = $1", id)
|
let result = sqlx::query!("DELETE FROM modelle WHERE modell_id = $1", id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(result.rows_affected() > 0)
|
println!("{:#?}", result);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,30 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_graphql::{InputObject, SimpleObject};
|
use async_graphql::SimpleObject;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, PgPool, Type};
|
use sqlx::{FromRow, PgPool, Type};
|
||||||
|
|
||||||
#[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, Type)]
|
#[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize, Type)]
|
||||||
pub struct Typ {
|
pub struct Typ {
|
||||||
pub id: i32,
|
pub typ_id: i32,
|
||||||
|
|
||||||
/// Name eines Typs
|
/// Name eines Typs
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
|
||||||
pub struct TypCreateInput {
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(InputObject, Debug)]
|
|
||||||
pub struct TypUpdateInput {
|
|
||||||
pub 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 id = $1", id)
|
let row = sqlx::query_as!(Typ, "SELECT * FROM typen WHERE typ_id = $1", id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -32,61 +32,28 @@ impl Typ {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn read_all(pool: &PgPool) -> Result<Vec<Typ>> {
|
pub async fn read_all(pool: &PgPool) -> Result<Vec<Typ>> {
|
||||||
let rows = sqlx::query_as!(Typ, "SELECT id, name FROM typen")
|
let rows = sqlx::query_as!(Typ, "SELECT typ_id, name FROM typen")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(rows)
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(pool: &PgPool, input: TypCreateInput) -> Result<Typ> {
|
pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result<Typ> {
|
||||||
let row = sqlx::query!(
|
sqlx::query!("UPDATE typen SET name=$1 WHERE typ_id = $2", name, id)
|
||||||
"INSERT INTO typen(name) VALUES ($1) RETURNING id",
|
|
||||||
input.name
|
|
||||||
)
|
|
||||||
.fetch_one(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let result = Self::read_one(pool, &row.id).await?;
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn create_many(pool: &PgPool, input: &[TypCreateInput]) -> Result<Typ> {
|
|
||||||
let mut v1: Vec<&str> = Vec::new();
|
|
||||||
input.iter().for_each(|typ| v1.push(&typ.name));
|
|
||||||
|
|
||||||
let row = sqlx::query_as(
|
|
||||||
r#"
|
|
||||||
INSERT INTO typen(name)
|
|
||||||
SELECT * FROM UNNEST($1)
|
|
||||||
RETURNING id, name"#,
|
|
||||||
)
|
|
||||||
.bind(&v1)
|
|
||||||
.fetch_one(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(row)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update(pool: &PgPool, input: TypUpdateInput) -> Result<Typ> {
|
|
||||||
sqlx::query!(
|
|
||||||
"UPDATE typen SET name=$1 WHERE id = $2",
|
|
||||||
input.name,
|
|
||||||
input.id
|
|
||||||
)
|
|
||||||
.execute(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let t = Typ::read_one(pool, &input.id).await?;
|
|
||||||
Ok(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete(pool: &PgPool, id: &i32) -> Result<bool> {
|
|
||||||
let result = sqlx::query!("DELETE FROM typen WHERE id = $1", id)
|
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(result.rows_affected() > 0)
|
let t = Typ::read_one(pool, id).await?;
|
||||||
|
|
||||||
|
Ok(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(pool: &PgPool, id: &i32) -> Result<()> {
|
||||||
|
sqlx::query!("DELETE FROM typen WHERE typ_id = $1", id)
|
||||||
|
.execute(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::models::hersteller::{Hersteller, HerstellerCreateInput, HerstellerUpdateInput};
|
use crate::models::hersteller::Hersteller;
|
||||||
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(
|
async fn create_hersteller(&self, ctx: &Context<'_>, name: String) -> FieldResult<Hersteller> {
|
||||||
&self,
|
|
||||||
ctx: &Context<'_>,
|
|
||||||
input: HerstellerCreateInput,
|
|
||||||
) -> FieldResult<Hersteller> {
|
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Hersteller::create(pool, &input).await?;
|
let row = Hersteller::create(pool, &name).await?;
|
||||||
Ok(row)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn update_hersteller(
|
|
||||||
&self,
|
|
||||||
ctx: &Context<'_>,
|
|
||||||
input: HerstellerUpdateInput,
|
|
||||||
) -> FieldResult<Hersteller> {
|
|
||||||
let pool = ctx.data::<PgPool>()?;
|
|
||||||
let row = Hersteller::update(pool, &input).await?;
|
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
async fn delete_hersteller(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
Ok(Hersteller::delete(pool, &id).await?)
|
|
||||||
|
Hersteller::delete(pool, &id).await?;
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_hersteller(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
id: i32,
|
||||||
|
name: String,
|
||||||
|
) -> FieldResult<Hersteller> {
|
||||||
|
let pool = ctx.data::<PgPool>()?;
|
||||||
|
|
||||||
|
let row = Hersteller::update(pool, &id, &name).await?;
|
||||||
|
Ok(row)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,24 +10,26 @@ impl ModellMutation {
|
||||||
async fn create_modell(
|
async fn create_modell(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
input: ModellCreateInput,
|
modell: ModellCreateInput,
|
||||||
) -> FieldResult<Modell> {
|
) -> FieldResult<Modell> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Modell::create(pool, &input).await?;
|
let row = Modell::create(pool, &modell).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_modell(
|
async fn update_modell(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
input: ModellUpdateInput,
|
modell: ModellUpdateInput,
|
||||||
) -> FieldResult<Modell> {
|
) -> FieldResult<Modell> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Modell::update(pool, &input).await?;
|
let row = Modell::update(pool, &modell).await?;
|
||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
async fn delete_modell(&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(Modell::delete(pool, &id).await?)
|
|
||||||
|
Modell::delete(pool, &id).await?;
|
||||||
|
Ok(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::models::typ::{Typ, TypCreateInput, TypUpdateInput};
|
use crate::models::typ::Typ;
|
||||||
use async_graphql::{Context, FieldResult};
|
use async_graphql::{Context, FieldResult};
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
|
||||||
|
@ -7,31 +7,23 @@ pub struct TypMutation;
|
||||||
|
|
||||||
#[async_graphql::Object]
|
#[async_graphql::Object]
|
||||||
impl TypMutation {
|
impl TypMutation {
|
||||||
async fn create_typ(&self, ctx: &Context<'_>, input: TypCreateInput) -> FieldResult<Typ> {
|
async fn create_typ(&self, ctx: &Context<'_>, name: String) -> FieldResult<Typ> {
|
||||||
let pool = ctx.data::<PgPool>()?;
|
let pool = ctx.data::<PgPool>()?;
|
||||||
let row = Typ::create(pool, input).await?;
|
let row = Typ::create(pool, &name).await?;
|
||||||
Ok(row)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn create_viele_typen(
|
|
||||||
&self,
|
|
||||||
ctx: &Context<'_>,
|
|
||||||
input: Vec<TypCreateInput>,
|
|
||||||
) -> FieldResult<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?;
|
|
||||||
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,6 +10,17 @@ 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,6 +10,16 @@ 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?;
|
||||||
|
@ -21,4 +31,11 @@ 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