Modelle hinzugefügt
This commit is contained in:
parent
6e6918e8ee
commit
534d819865
|
@ -1,5 +1,5 @@
|
|||
-- Add up migration script here
|
||||
CREATE TABLE typen (
|
||||
CREATE TABLE IF NOT EXISTS typen (
|
||||
typ_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL
|
||||
);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
-- Add up migration script here
|
||||
CREATE TABLE hersteller (
|
||||
CREATE TABLE IF NOT EXISTS hersteller (
|
||||
hersteller_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL
|
||||
);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
-- Add up migration script here
|
||||
CREATE TABLE modelle (
|
||||
CREATE TABLE IF NOT EXISTS modelle (
|
||||
modell_id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
typ_id SERIAL,
|
||||
hersteller_id SERIAL,
|
||||
CONSTRAINT fk_typ FOREIGN KEY(typ_id) REFERENCES typen(typ_id),
|
||||
CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(hersteller_id)
|
||||
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(hersteller_id) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
)
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
pub mod hersteller;
|
||||
pub mod modell;
|
||||
pub mod modell_temp;
|
||||
pub mod typ;
|
||||
|
|
|
@ -1,49 +1,56 @@
|
|||
use anyhow::Result;
|
||||
use async_graphql::SimpleObject;
|
||||
use async_graphql::{InputObject, SimpleObject};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, PgPool};
|
||||
|
||||
use super::{hersteller::Hersteller, typ::Typ};
|
||||
|
||||
#[derive(SimpleObject, FromRow, Deserialize, Serialize)]
|
||||
pub struct Modell {
|
||||
/// Die Datenbank-ID
|
||||
pub modell_id: i32,
|
||||
|
||||
/// Der Typ
|
||||
typ_id: i32,
|
||||
/// Der Geräte-Typ z.B. Monitor
|
||||
typ: Typ,
|
||||
|
||||
/// Der Hersteller
|
||||
hersteller_id: i32,
|
||||
/// Der Geräte-Hersteller z.B. Dell
|
||||
hersteller: Hersteller,
|
||||
|
||||
/// Der Name eines Modells
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Modell {
|
||||
pub async fn create(
|
||||
pool: &PgPool,
|
||||
typ_id: i32,
|
||||
hersteller_id: i32,
|
||||
name: &str,
|
||||
) -> Result<Modell> {
|
||||
let row = sqlx::query!(
|
||||
"INSERT INTO modelle(typ_id, hersteller_id, name) VALUES ($1, $2, $3) RETURNING modell_id",
|
||||
typ_id,
|
||||
hersteller_id,
|
||||
name
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(Modell {
|
||||
modell_id: row.modell_id,
|
||||
typ_id,
|
||||
hersteller_id,
|
||||
name: name.to_string(),
|
||||
})
|
||||
#[derive(InputObject)]
|
||||
pub struct ModellCreateInput {
|
||||
pub name: String,
|
||||
pub typ_id: i32,
|
||||
pub hersteller_id: i32,
|
||||
}
|
||||
|
||||
#[derive(InputObject)]
|
||||
pub struct ModellUpdateInput {
|
||||
pub modell_id: i32,
|
||||
pub name: Option<String>,
|
||||
pub typ_id: Option<i32>,
|
||||
pub hersteller_id: Option<i32>,
|
||||
}
|
||||
|
||||
impl Modell {
|
||||
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<Modell> {
|
||||
let row = sqlx::query_as!(Modell, "SELECT * FROM modelle WHERE modell_id = $1", id)
|
||||
let row = sqlx::query_as!(
|
||||
Modell,
|
||||
r#"
|
||||
select
|
||||
m.modell_id, m.name,
|
||||
(t.typ_id, t.name) as "typ!: Typ",
|
||||
(h.hersteller_id, h.name) as "hersteller!: Hersteller"
|
||||
from modelle as m
|
||||
join typen as t using(typ_id)
|
||||
join hersteller as h using(hersteller_id)
|
||||
where m.modell_id = $1;
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
|
@ -51,31 +58,55 @@ impl Modell {
|
|||
}
|
||||
|
||||
pub async fn read_all(pool: &PgPool) -> Result<Vec<Modell>> {
|
||||
let rows = sqlx::query_as!(
|
||||
let row = sqlx::query_as!(
|
||||
Modell,
|
||||
"SELECT modell_id, name, hersteller_id, typ_id FROM modelle"
|
||||
r#"
|
||||
select
|
||||
m.modell_id, m.name,
|
||||
(t.typ_id, t.name) as "typ!: Typ",
|
||||
(h.hersteller_id, h.name) as "hersteller!: Hersteller"
|
||||
from modelle as m
|
||||
join typen as t using(typ_id)
|
||||
join hersteller as h using(hersteller_id)
|
||||
"#
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
Ok(rows)
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result<Modell> {
|
||||
sqlx::query!("UPDATE modelle SET name=$1 WHERE modell_id = $2", name, id)
|
||||
pub async fn create(pool: &PgPool, input: &ModellCreateInput) -> Result<Modell> {
|
||||
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
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Self::read_one(pool, &row.modell_id).await
|
||||
}
|
||||
|
||||
pub async fn update(pool: &PgPool, input: &ModellUpdateInput) -> Result<Modell> {
|
||||
sqlx::query!(
|
||||
"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.typ_id,
|
||||
input.hersteller_id,
|
||||
input.modell_id
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
let t = Modell::read_one(pool, id).await?;
|
||||
let t = Modell::read_one(pool, &input.modell_id).await?;
|
||||
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
pub async fn delete(pool: &PgPool, id: &i32) -> 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)
|
||||
.await?;
|
||||
|
||||
println!("{:#?}", result);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
use anyhow::Result;
|
||||
use async_graphql::SimpleObject;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{FromRow, PgPool};
|
||||
|
||||
use super::{hersteller::Hersteller, typ::Typ};
|
||||
|
||||
#[derive(SimpleObject, Debug, FromRow, Deserialize, Serialize)]
|
||||
pub struct ModellTemp {
|
||||
/// Die Datenbank-ID
|
||||
pub modell_id: i32,
|
||||
|
||||
/// Der Typ
|
||||
pub typ: Typ,
|
||||
|
||||
/// Der Hersteller
|
||||
pub hersteller: Hersteller,
|
||||
|
||||
/// Der Name eines Modells
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl ModellTemp {
|
||||
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<ModellTemp> {
|
||||
let row = sqlx::query_as!(
|
||||
ModellTemp,
|
||||
r#"
|
||||
select
|
||||
m.modell_id, m.name,
|
||||
(t.typ_id, t.name) as "typ!: Typ",
|
||||
(h.hersteller_id, h.name) as "hersteller!: Hersteller"
|
||||
from modelle as m
|
||||
join typen as t using(typ_id)
|
||||
join hersteller as h using(hersteller_id)
|
||||
where m.modell_id = $1;
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use sqlx::{FromRow, PgPool, Type};
|
|||
pub struct Typ {
|
||||
pub typ_id: i32,
|
||||
|
||||
/// Der Name eines Typs
|
||||
/// Name eines Typs
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
pub mod hersteller;
|
||||
pub mod modell;
|
||||
pub mod typ;
|
||||
|
||||
use async_graphql::MergedObject;
|
||||
|
||||
#[derive(MergedObject, Default)]
|
||||
pub struct Mutation(typ::TypMutation, hersteller::HerstellerMutation);
|
||||
pub struct Mutation(
|
||||
typ::TypMutation,
|
||||
hersteller::HerstellerMutation,
|
||||
modell::ModellMutation,
|
||||
);
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
use crate::models::modell::*;
|
||||
use async_graphql::{Context, FieldResult};
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ModellMutation;
|
||||
|
||||
#[async_graphql::Object]
|
||||
impl ModellMutation {
|
||||
async fn create_modell(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
modell: ModellCreateInput,
|
||||
) -> FieldResult<Modell> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let row = Modell::create(pool, &modell).await?;
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
async fn update_modell(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
modell: ModellUpdateInput,
|
||||
) -> FieldResult<Modell> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let row = Modell::update(pool, &modell).await?;
|
||||
Ok(row)
|
||||
}
|
||||
async fn delete_typ(&self, ctx: &Context<'_>, id: i32) -> FieldResult<bool> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
|
||||
Modell::delete(pool, &id).await?;
|
||||
Ok(true)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
pub mod hersteller;
|
||||
pub mod modell;
|
||||
pub mod modell_temp;
|
||||
pub mod typ;
|
||||
|
||||
use async_graphql::MergedObject;
|
||||
|
@ -9,6 +8,5 @@ use async_graphql::MergedObject;
|
|||
pub struct Query(
|
||||
typ::TypQuery,
|
||||
modell::ModellQuery,
|
||||
modell_temp::ModellTempQuery,
|
||||
hersteller::HerstellerQuery,
|
||||
);
|
||||
|
|
|
@ -10,25 +10,16 @@ pub struct ModellQuery {
|
|||
|
||||
#[Object(extends)]
|
||||
impl ModellQuery {
|
||||
// #[graphql(external)]
|
||||
// async fn id(&self) -> &i32 {
|
||||
// &self.id
|
||||
// }
|
||||
|
||||
async fn modelle<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Modell>> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let rows = Modell::read_all(pool).await?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
async fn modell<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<Modell> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let row = Modell::read_one(pool, &id).await?;
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
// #[graphql(entity)]
|
||||
// async fn find_modell_by_id(&self, id: i32) -> Modell {
|
||||
// Modell { id }
|
||||
// }
|
||||
async fn modelle<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Modell>> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let output = Modell::read_all(pool).await?;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
use async_graphql::{Context, FieldResult, Object};
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
use crate::models::modell_temp::ModellTemp;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ModellTempQuery {}
|
||||
|
||||
#[Object(extends)]
|
||||
impl ModellTempQuery {
|
||||
// async fn modelle<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<ModellTemp>> {
|
||||
// let pool = ctx.data::<PgPool>()?;
|
||||
// let rows = ModellTemp::read_all(pool).await?;
|
||||
// Ok(rows)
|
||||
// }
|
||||
|
||||
async fn modell_temp<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<ModellTemp> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
let row = ModellTemp::read_one(pool, &id).await?;
|
||||
Ok(row)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue