This commit is contained in:
Peter Schiwy
2024-06-20 14:50:36 +02:00
parent 1d60fc5a3f
commit 44931afbe7
23 changed files with 431 additions and 25 deletions

View File

@@ -0,0 +1,2 @@
pub mod rolle;
pub use rolle::Rolle;

View File

@@ -0,0 +1,9 @@
use crate::scalar::{Id, Time};
#[derive(sqlx::FromRow)]
pub struct Rolle {
pub id: Id,
pub created_at: Time,
pub updated_at: Time,
pub name: String,
}

View File

@@ -0,0 +1,5 @@
mod create_rolle_input;
mod rolle;
mod update_rolle_input;
pub use create_rolle_input::CreateRolleInput;

View File

@@ -0,0 +1,7 @@
use async_graphql::InputObject;
#[derive(InputObject)]
pub struct CreateRolleInput {
/// Der Name einer Rolle
pub name: String,
}

View File

@@ -0,0 +1,15 @@
use async_graphql::SimpleObject;
use crate::scalar::{Id, Time};
#[derive(Debug, SimpleObject)]
pub struct Rolle {
/// Die ID einer Rolle
pub id: Id,
/// Wann die Rolle erstellt wurde
pub created_at: Time,
/// Der Name einer Rolle
pub name: String,
}

View File

@@ -0,0 +1,12 @@
use async_graphql::InputObject;
use crate::scalar::Id;
#[derive(InputObject)]
pub struct UpdateRolleInput {
/// Die ID einer Rolle
pub id: Id,
/// Der Name einer Rolle
pub name: String,
}

View File

@@ -0,0 +1,20 @@
mod create_rolle;
mod delete_rolle;
mod find_all_rolle;
mod find_rolle;
mod update_rolle;
#[derive(Debug, Clone)]
pub struct Repository {}
impl Repository {
pub fn new() -> Repository {
Repository {}
}
}
impl Default for Repository {
fn default() -> Self {
Self::new()
}
}

View File

@@ -0,0 +1,30 @@
use anyhow::Error;
use super::Repository;
use crate::{database::Queryer, domain::rolle::entity};
impl Repository {
pub async fn create_rolle<'c, C: Queryer<'c>>(
&self,
db: C,
rolle: &entity::Rolle,
) -> Result<entity::Rolle, Error> {
const QUERY: &str = "insert into rolle (id, created_at, updated_at,
name) values ($1, $2, $3, $4) returning *";
match sqlx::query_as::<_, entity::Rolle>(QUERY)
.bind(rolle.id)
.bind(rolle.created_at)
.bind(rolle.updated_at)
.bind(&rolle.name)
.fetch_one(db)
.await
{
Err(err) => {
tracing::error!("{}", &err);
Err(err.into())
}
Ok(user) => Ok(user),
}
}
}

View File

@@ -0,0 +1,17 @@
mod create_user;
use super::repository::Repository;
use crate::database::DB;
#[derive(Debug)]
pub struct Service {
repo: Repository,
pub db: DB,
}
impl Service {
pub fn new(db: DB) -> Self {
let repo = Repository::new();
Self { repo, db }
}
}

View File

@@ -0,0 +1,26 @@
use anyhow::Error;
use chrono::Utc;
use ulid::Ulid;
use super::Service;
use crate::domain::rolle::entity;
use crate::domain::rolle::model::CreateRolleInput;
impl Service {
pub async fn create_rolle(&self, input: CreateRolleInput) -> Result<entity::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(),
name: input.name,
created_at: Utc::now(),
updated_at: Utc::now(),
};
let rolle = self.repo.create_rolle(&self.db, &rolle_input).await?;
Ok(rolle)
}
}