Modelle hinzugefügt

This commit is contained in:
Peter Schiwy 2024-06-03 15:41:00 +02:00
parent 6e6918e8ee
commit 534d819865
12 changed files with 122 additions and 130 deletions

View File

@ -1,5 +1,5 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE typen ( CREATE TABLE IF NOT EXISTS typen (
typ_id SERIAL PRIMARY KEY, typ_id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL name VARCHAR NOT NULL
); );

View File

@ -1,5 +1,5 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE hersteller ( CREATE TABLE IF NOT EXISTS hersteller (
hersteller_id SERIAL PRIMARY KEY, hersteller_id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL name VARCHAR NOT NULL
); );

View File

@ -1,9 +1,9 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE modelle ( CREATE TABLE IF NOT EXISTS modelle (
modell_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(typ_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) CONSTRAINT fk_hersteller FOREIGN KEY (hersteller_id) REFERENCES hersteller(hersteller_id) ON DELETE NO ACTION ON UPDATE NO ACTION
) )

View File

@ -1,4 +1,3 @@
pub mod hersteller; pub mod hersteller;
pub mod modell; pub mod modell;
pub mod modell_temp;
pub mod typ; pub mod typ;

View File

@ -1,49 +1,56 @@
use anyhow::Result; use anyhow::Result;
use async_graphql::SimpleObject; use async_graphql::{InputObject, SimpleObject};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{FromRow, PgPool}; use sqlx::{FromRow, PgPool};
use super::{hersteller::Hersteller, typ::Typ};
#[derive(SimpleObject, FromRow, Deserialize, Serialize)] #[derive(SimpleObject, FromRow, Deserialize, Serialize)]
pub struct Modell { pub struct Modell {
/// Die Datenbank-ID /// Die Datenbank-ID
pub modell_id: i32, pub modell_id: i32,
/// Der Typ /// Der Geräte-Typ z.B. Monitor
typ_id: i32, typ: Typ,
/// Der Hersteller /// Der Geräte-Hersteller z.B. Dell
hersteller_id: i32, hersteller: Hersteller,
/// Der Name eines Modells /// Der Name eines Modells
name: String, name: String,
} }
impl Modell { #[derive(InputObject)]
pub async fn create( pub struct ModellCreateInput {
pool: &PgPool, pub name: String,
typ_id: i32, pub typ_id: i32,
hersteller_id: i32, pub 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 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> { 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) .fetch_one(pool)
.await?; .await?;
@ -51,31 +58,55 @@ impl Modell {
} }
pub async fn read_all(pool: &PgPool) -> Result<Vec<Modell>> { pub async fn read_all(pool: &PgPool) -> Result<Vec<Modell>> {
let rows = sqlx::query_as!( let row = sqlx::query_as!(
Modell, 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) .fetch_all(pool)
.await?; .await?;
Ok(rows) Ok(row)
} }
pub async fn update(pool: &PgPool, id: &i32, name: &str) -> Result<Modell> { pub async fn create(pool: &PgPool, input: &ModellCreateInput) -> Result<Modell> {
sqlx::query!("UPDATE modelle SET name=$1 WHERE modell_id = $2", name, id) 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) .execute(pool)
.await?; .await?;
let t = Modell::read_one(pool, 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<()> { 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) .execute(pool)
.await?; .await?;
println!("{:#?}", result);
Ok(()) Ok(())
} }
} }

View File

@ -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)
}
}

View File

@ -7,7 +7,7 @@ use sqlx::{FromRow, PgPool, Type};
pub struct Typ { pub struct Typ {
pub typ_id: i32, pub typ_id: i32,
/// Der Name eines Typs /// Name eines Typs
pub name: String, pub name: String,
} }

View File

@ -1,7 +1,11 @@
pub mod hersteller; pub mod hersteller;
pub mod modell;
pub mod typ; pub mod typ;
use async_graphql::MergedObject; use async_graphql::MergedObject;
#[derive(MergedObject, Default)] #[derive(MergedObject, Default)]
pub struct Mutation(typ::TypMutation, hersteller::HerstellerMutation); pub struct Mutation(
typ::TypMutation,
hersteller::HerstellerMutation,
modell::ModellMutation,
);

35
src/mutations/modell.rs Normal file
View File

@ -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)
}
}

View File

@ -1,6 +1,5 @@
pub mod hersteller; pub mod hersteller;
pub mod modell; pub mod modell;
pub mod modell_temp;
pub mod typ; pub mod typ;
use async_graphql::MergedObject; use async_graphql::MergedObject;
@ -9,6 +8,5 @@ use async_graphql::MergedObject;
pub struct Query( pub struct Query(
typ::TypQuery, typ::TypQuery,
modell::ModellQuery, modell::ModellQuery,
modell_temp::ModellTempQuery,
hersteller::HerstellerQuery, hersteller::HerstellerQuery,
); );

View File

@ -10,25 +10,16 @@ pub struct ModellQuery {
#[Object(extends)] #[Object(extends)]
impl ModellQuery { 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> { async fn modell<'a>(&self, ctx: &'a Context<'_>, id: i32) -> FieldResult<Modell> {
let pool = ctx.data::<PgPool>()?; let pool = ctx.data::<PgPool>()?;
let row = Modell::read_one(pool, &id).await?; let row = Modell::read_one(pool, &id).await?;
Ok(row) Ok(row)
} }
// #[graphql(entity)] async fn modelle<'a>(&self, ctx: &'a Context<'_>) -> FieldResult<Vec<Modell>> {
// async fn find_modell_by_id(&self, id: i32) -> Modell { let pool = ctx.data::<PgPool>()?;
// Modell { id } let output = Modell::read_all(pool).await?;
// }
Ok(output)
}
} }

View File

@ -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)
}
}