rolle domain hinzugefügt
This commit is contained in:
@@ -3,6 +3,6 @@ use crate::scalar::{Id, Time};
|
||||
pub struct Rolle {
|
||||
pub id: Id,
|
||||
pub rollenname: String,
|
||||
pub erstellt_am: Time,
|
||||
pub geaendert_am: Time,
|
||||
pub erstellt_am: Option<Time>,
|
||||
pub geaendert_am: Option<Time>,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
mod rolle;
|
||||
mod rolle_create_input;
|
||||
mod rolle_update_input;
|
||||
pub mod rolle;
|
||||
pub mod rolle_erstelle_input;
|
||||
pub mod rolle_update_input;
|
||||
|
||||
pub use rolle::Rolle;
|
||||
pub use rolle_create_input::RolleCreateInput;
|
||||
pub use rolle_erstelle_input::RolleErstelleInput;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use async_graphql::InputObject;
|
||||
|
||||
#[derive(InputObject)]
|
||||
pub struct RolleCreateInput {
|
||||
pub struct RolleErstelleInput {
|
||||
/// Der Name einer Rolle
|
||||
pub rollenname: String,
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
mod create_rolle;
|
||||
mod delete_rolle;
|
||||
mod find_all_rolle;
|
||||
mod find_rolle_by_id;
|
||||
mod rolle_erstellen;
|
||||
mod rollen_dataloader;
|
||||
mod update_rolle;
|
||||
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
use anyhow::Error;
|
||||
|
||||
use super::Repository;
|
||||
use crate::database::Queryer;
|
||||
use crate::domain::rolle::entity;
|
||||
use crate::scalar::Id;
|
||||
|
||||
impl Repository {
|
||||
pub async fn create_rolle<'c, C: Queryer<'c>>(
|
||||
&self,
|
||||
db: C,
|
||||
rolle: &entity::Rolle,
|
||||
) -> Result<Id, Error> {
|
||||
const QUERY: &str = "insert into rolle (id, created_at, updated_at,
|
||||
name) values ($1, $2, $3, $4) returning id";
|
||||
|
||||
match sqlx::query_scalar::<_, Id>(QUERY)
|
||||
.bind(rolle.id)
|
||||
.bind(rolle.erstellt_am)
|
||||
.bind(rolle.geaendert_am)
|
||||
.bind(&rolle.rollenname)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
{
|
||||
Err(err) => {
|
||||
tracing::error!("{}", &err);
|
||||
Err(err.into())
|
||||
}
|
||||
Ok(id) => Ok(id),
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/domain/rolle/repository/rolle_erstellen.rs
Normal file
29
src/domain/rolle/repository/rolle_erstellen.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use anyhow::Error;
|
||||
|
||||
use super::Repository;
|
||||
use crate::database::Queryer;
|
||||
use crate::domain::rolle::{entity, model};
|
||||
|
||||
impl Repository {
|
||||
pub async fn rolle_erstellen<'c, C: Queryer<'c>>(
|
||||
&self,
|
||||
db: C,
|
||||
rolle: &entity::Rolle,
|
||||
) -> Result<model::Rolle, Error> {
|
||||
const QUERY: &str = r#"
|
||||
INSERT INTO rollen (id, erstellt_am, geaendert_am, rollenname) VALUES (
|
||||
$1, $2, $3, $4
|
||||
) RETURNING id, erstellt_am, geaendert_am, rollenname
|
||||
"#;
|
||||
|
||||
let rolle = sqlx::query_as::<_, model::Rolle>(QUERY)
|
||||
.bind(rolle.id)
|
||||
.bind(rolle.erstellt_am)
|
||||
.bind(rolle.geaendert_am)
|
||||
.bind(&rolle.rollenname)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
|
||||
Ok(rolle)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
mod create_user;
|
||||
mod rolle_erstellen;
|
||||
|
||||
use super::repository::Repository;
|
||||
use crate::database::DB;
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
use anyhow::Error;
|
||||
use chrono::Utc;
|
||||
use ulid::Ulid;
|
||||
|
||||
use super::Service;
|
||||
use crate::domain::rolle::model::RolleCreateInput;
|
||||
use crate::domain::rolle::{entity, model};
|
||||
|
||||
impl Service {
|
||||
pub async fn create_rolle(&self, input: RolleCreateInput) -> Result<model::Rolle, Error> {
|
||||
// let username_exists = self.check_username_exists(&self.db, &input.name).await?;
|
||||
// if username_exists {
|
||||
// return Err(Error::UsernameAlreadyExists.into());
|
||||
// }
|
||||
|
||||
let rolle_input = entity::Rolle {
|
||||
id: Ulid::new().into(),
|
||||
rollenname: input.rollenname,
|
||||
erstellt_am: Utc::now(),
|
||||
geaendert_am: Utc::now(),
|
||||
};
|
||||
|
||||
let created_id = self.repo.create_rolle(&self.db, &rolle_input).await?;
|
||||
let rolle = self.repo.find_rolle_by_id(&self.db, created_id).await?;
|
||||
Ok(rolle)
|
||||
}
|
||||
}
|
||||
26
src/domain/rolle/service/rolle_erstellen.rs
Normal file
26
src/domain/rolle/service/rolle_erstellen.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use anyhow::Error;
|
||||
use chrono::Utc;
|
||||
use ulid::Ulid;
|
||||
|
||||
use super::Service;
|
||||
use crate::domain::rolle::{
|
||||
entity,
|
||||
model::{self, RolleErstelleInput},
|
||||
};
|
||||
|
||||
impl Service {
|
||||
pub async fn rolle_erstellen(&self, input: RolleErstelleInput) -> Result<model::Rolle, Error> {
|
||||
let rolle_input = entity::Rolle {
|
||||
id: Ulid::new().into(),
|
||||
rollenname: input.rollenname,
|
||||
erstellt_am: Some(Utc::now()),
|
||||
geaendert_am: Some(Utc::now()),
|
||||
};
|
||||
|
||||
// let created_id = self.repo.rolle_erstellen(&self.db, &rolle_input).await?;
|
||||
// let rolle = self.repo.find_rolle_by_id(&self.db, created_id).await?;
|
||||
|
||||
let rolle = self.repo.rolle_erstellen(&self.db, &rolle_input).await?;
|
||||
Ok(rolle)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ pub mod benutzer;
|
||||
pub mod gruppe;
|
||||
pub mod hersteller;
|
||||
pub mod modell;
|
||||
pub mod rolle;
|
||||
pub mod typ;
|
||||
|
||||
use async_graphql::MergedObject;
|
||||
@@ -13,4 +14,5 @@ pub struct Mutation(
|
||||
modell::ModellMutation,
|
||||
benutzer::BenutzerMutation,
|
||||
gruppe::GruppeMutation,
|
||||
rolle::RolleMutation,
|
||||
);
|
||||
|
||||
23
src/mutations/rolle.rs
Normal file
23
src/mutations/rolle.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::domain::rolle::{
|
||||
model::{Rolle, RolleErstelleInput},
|
||||
service::Service,
|
||||
};
|
||||
use async_graphql::{Context, FieldResult};
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RolleMutation;
|
||||
|
||||
#[async_graphql::Object]
|
||||
impl RolleMutation {
|
||||
async fn rolle_erstellen(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
input: RolleErstelleInput,
|
||||
) -> FieldResult<Rolle> {
|
||||
let pool = ctx.data::<PgPool>()?;
|
||||
|
||||
let gruppe = Service::new(pool.clone()).rolle_erstellen(input).await?;
|
||||
Ok(gruppe)
|
||||
}
|
||||
}
|
||||
13
test.sh
Executable file
13
test.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
ROLLEN='
|
||||
{"query": "mutation {
|
||||
rolleErstellen(input: { rollenname: \"Rolle 1\" }) { id }
|
||||
rolleErstellen(input: { rollenname: \"Rolle 22\" }) { id }
|
||||
}"}
|
||||
'
|
||||
|
||||
curl \
|
||||
-X POST http://localhost:8000/ \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$ROLLEN"
|
||||
Reference in New Issue
Block a user