Modelle hinzugefügt
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
pub mod hersteller;
|
||||
pub mod modell;
|
||||
pub mod modell_temp;
|
||||
pub mod typ;
|
||||
|
||||
@@ -1,81 +1,112 @@
|
||||
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,
|
||||
}
|
||||
|
||||
#[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 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
|
||||
pub async fn read_one(pool: &PgPool, id: &i32) -> Result<Modell> {
|
||||
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?;
|
||||
|
||||
Ok(Modell {
|
||||
modell_id: row.modell_id,
|
||||
typ_id,
|
||||
hersteller_id,
|
||||
name: name.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
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)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
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?;
|
||||
|
||||
let t = Modell::read_one(pool, id).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, &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,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user