diff --git a/README.md b/README.md index 3688d27..1334667 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # HIGurke +## Generate Seaography entities + +```console +# List all available commands +sea-orm-cli -h + +# List all subcommands available in `generate` command +sea-orm-cli generate -h + +# Show how to use `generate entity` subcommand +sea-orm-cli generate entity -h + +# Generate Seaography entities +sea-orm-cli generate entity --output-dir ./database/src/entities --with-serde both --with-copy-enums +``` diff --git a/database/src/entities/benutzer.rs b/database/src/entities/benutzer.rs new file mode 100644 index 0000000..b75cb85 --- /dev/null +++ b/database/src/entities/benutzer.rs @@ -0,0 +1,43 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "benutzer")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + #[sea_orm(unique)] + pub e_mail: String, + #[sea_orm(unique)] + pub kennung: String, + pub password_hash: String, + pub nachname: String, + pub vorname: String, + pub erstellt_am: DateTimeWithTimeZone, + pub geaendert_am: DateTimeWithTimeZone, + pub ist_aktiv: bool, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_one = "super::hardware::Entity")] + Hardware, + #[sea_orm(has_many = "super::hardware_events::Entity")] + HardwareEvents, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Hardware.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::HardwareEvents.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/database/src/entities/hardware.rs b/database/src/entities/hardware.rs new file mode 100644 index 0000000..d4fc924 --- /dev/null +++ b/database/src/entities/hardware.rs @@ -0,0 +1,42 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +use super::sea_orm_active_enums::StatusType; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "hardware")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub name: String, + #[sea_orm(unique)] + pub seriennummer: String, + #[sea_orm(column_type = "Text")] + pub beschreibung: String, + pub status: StatusType, + #[sea_orm(unique)] + pub benutzer_id: Uuid, + pub erstellt_am: DateTimeWithTimeZone, + pub geaendert_am: DateTimeWithTimeZone, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::benutzer::Entity", + from = "Column::BenutzerId", + to = "super::benutzer::Column::Id", + on_update = "NoAction", + on_delete = "SetNull" + )] + Benutzer, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Benutzer.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/database/src/entities/hardware_events.rs b/database/src/entities/hardware_events.rs new file mode 100644 index 0000000..5643bba --- /dev/null +++ b/database/src/entities/hardware_events.rs @@ -0,0 +1,41 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +use super::sea_orm_active_enums::EventType; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "hardware_events")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + #[sea_orm(unique)] + pub hardware_id: Uuid, + #[sea_orm(unique)] + pub event: EventType, + pub payload: Json, + pub version: i32, + #[sea_orm(unique)] + pub erstellt_am: DateTimeWithTimeZone, + pub benutzer_id: Uuid, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::benutzer::Entity", + from = "Column::BenutzerId", + to = "super::benutzer::Column::Id", + on_update = "NoAction", + on_delete = "SetNull" + )] + Benutzer, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Benutzer.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/database/src/entities/mod.rs b/database/src/entities/mod.rs new file mode 100644 index 0000000..13c8bb2 --- /dev/null +++ b/database/src/entities/mod.rs @@ -0,0 +1,8 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +pub mod prelude; + +pub mod benutzer; +pub mod hardware; +pub mod hardware_events; +pub mod sea_orm_active_enums; diff --git a/database/src/entities/prelude.rs b/database/src/entities/prelude.rs new file mode 100644 index 0000000..50832a4 --- /dev/null +++ b/database/src/entities/prelude.rs @@ -0,0 +1,5 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +pub use super::benutzer::Entity as Benutzer; +pub use super::hardware::Entity as Hardware; +pub use super::hardware_events::Entity as HardwareEvents; diff --git a/database/src/entities/sea_orm_active_enums.rs b/database/src/entities/sea_orm_active_enums.rs new file mode 100644 index 0000000..cc3d1f0 --- /dev/null +++ b/database/src/entities/sea_orm_active_enums.rs @@ -0,0 +1,33 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, Serialize, Deserialize)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "event_type")] +pub enum EventType { + #[sea_orm(string_value = "erstellt")] + Erstellt, + #[sea_orm(string_value = "geaendert")] + Geaendert, + #[sea_orm(string_value = "geloescht")] + Geloescht, +} +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, Serialize, Deserialize)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "status_type")] +pub enum StatusType { + #[sea_orm(string_value = "verfuegbar")] + Verfuegbar, + #[sea_orm(string_value = "in_benutzung")] + InBenutzung, + #[sea_orm(string_value = "in_wartung")] + InWartung, + #[sea_orm(string_value = "in_reparatur")] + InReparatur, + #[sea_orm(string_value = "verloren")] + Verloren, + #[sea_orm(string_value = "defekt")] + Defekt, + #[sea_orm(string_value = "re_invest")] + ReInvest, +} diff --git a/database/src/models/benutzer.rs b/database/src/models/benutzer.rs new file mode 100644 index 0000000..1b37068 --- /dev/null +++ b/database/src/models/benutzer.rs @@ -0,0 +1,38 @@ +#[derive(Debug, Serialize, Deserialize, Validate)] +pub struct ErstelleBenutzerDto { + #[validate(length(min = 10, max = 255))] + pub e_mail: String, + + #[validate(length(min = 8, max = 8))] + pub kennung: String, + + #[validate(length(max = 100))] + pub nachname: String, + + #[validate(length(max = 100))] + pub vorname: String, +} + +#[derive(Debug, Serialize)] +pub struct DeviceResponse { + pub id: Uuid, + pub name: String, + pub serial_number: String, + pub description: Option, + pub status: String, + pub created_at: chrono::DateTime, +} + +// Conversion from Entity to Response Model +impl From for DeviceResponse { + fn from(entity: entities::devices::Model) -> Self { + Self { + id: entity.id, + name: entity.name, + serial_number: entity.serial_number, + description: entity.description, + status: entity.status, + created_at: entity.created_at, + } + } +} diff --git a/database/src/models/mod.rs b/database/src/models/mod.rs new file mode 100644 index 0000000..4c39041 --- /dev/null +++ b/database/src/models/mod.rs @@ -0,0 +1 @@ +pub mod benutzer; diff --git a/database/src/models/prelude.rs b/database/src/models/prelude.rs new file mode 100644 index 0000000..d146210 --- /dev/null +++ b/database/src/models/prelude.rs @@ -0,0 +1 @@ +pub use super::benutzer::ErstelleBenutzerDto as ErstelleBenutzer; diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 65ee896..275e3e1 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*; mod m20250202_000233_benutzer; mod m20250202_225521_hardware; +mod m20250203_214832_hardware_events; pub struct Migrator; @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m20250202_000233_benutzer::Migration), Box::new(m20250202_225521_hardware::Migration), + Box::new(m20250203_214832_hardware_events::Migration), ] } } diff --git a/migration/src/m20250202_225521_hardware.rs b/migration/src/m20250202_225521_hardware.rs index 1970400..bae47c3 100644 --- a/migration/src/m20250202_225521_hardware.rs +++ b/migration/src/m20250202_225521_hardware.rs @@ -9,8 +9,8 @@ use crate::m20250202_000233_benutzer::Benutzer; pub struct Migration; #[derive(DeriveIden)] -#[sea_orm(iden = "status_type_enum")] -struct StatusTypeEnum; +#[sea_orm(iden = "status_type")] +struct StatusType; #[derive(DeriveIden, EnumIter)] pub enum StatusVarianten { @@ -36,7 +36,7 @@ impl MigrationTrait for Migration { manager .create_type( Type::create() - .as_enum(StatusTypeEnum) + .as_enum(StatusType) .values(StatusVarianten::iter()) .to_owned(), ) @@ -53,7 +53,7 @@ impl MigrationTrait for Migration { .col(text(Hardware::Beschreibung)) .col(enumeration( Hardware::Status, - StatusTypeEnum, + StatusType, StatusVarianten::iter(), )) .col(uuid(Hardware::BenutzerId)) @@ -71,7 +71,8 @@ impl MigrationTrait for Migration { ForeignKey::create() .name("fk_hardware_benutzer") .from(Hardware::Table, Hardware::BenutzerId) - .to(Benutzer::Table, Benutzer::Id), + .to(Benutzer::Table, Benutzer::Id) + .on_delete(ForeignKeyAction::SetNull), ) .to_owned(), ) @@ -106,7 +107,7 @@ impl MigrationTrait for Migration { .await?; manager - .drop_type(Type::drop().name(StatusTypeEnum).to_owned()) + .drop_type(Type::drop().name(StatusType).to_owned()) .await } } diff --git a/migration/src/m20250203_214832_hardware_events.rs b/migration/src/m20250203_214832_hardware_events.rs new file mode 100644 index 0000000..273520e --- /dev/null +++ b/migration/src/m20250203_214832_hardware_events.rs @@ -0,0 +1,123 @@ +use chrono::Utc; +use extension::postgres::Type; +use sea_orm::{EnumIter, Iterable}; +use sea_orm_migration::{prelude::*, schema::*}; + +use crate::m20250202_000233_benutzer::Benutzer; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[derive(DeriveIden)] +#[sea_orm(iden = "event_type")] +struct EventType; + +#[derive(DeriveIden, EnumIter)] +pub enum EventVarianten { + #[sea_orm(iden = "erstellt", string_value = "Erstellt")] + Erstellt, + #[sea_orm(iden = "geaendert", string_value = "Geändert")] + Geaendert, + #[sea_orm(iden = "geloescht", string_value = "Gelöscht")] + Geloescht, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_type( + Type::create() + .as_enum(EventType) + .values(EventVarianten::iter()) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(HardwareEvents::Table) + .if_not_exists() + .col(pk_uuid(HardwareEvents::Id)) + .col(uuid(HardwareEvents::HardwareId)) + .col(enumeration( + HardwareEvents::Event, + EventType, + EventVarianten::iter(), + )) + .col(json(HardwareEvents::Payload)) + .col(integer(HardwareEvents::Version).not_null().default(1)) + .col( + timestamp_with_time_zone(HardwareEvents::ErstelltAm) + .not_null() + .default(Utc::now()), + ) + .col(uuid(HardwareEvents::BenutzerId)) + .foreign_key( + ForeignKey::create() + .name("fk_hardware_event_benutzer") + .from(HardwareEvents::Table, HardwareEvents::BenutzerId) + .to(Benutzer::Table, Benutzer::Id) + .on_delete(ForeignKeyAction::SetNull), + ) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .unique() + .name("idx_hardware_events_hardware_id") + .table(HardwareEvents::Table) + .col(HardwareEvents::HardwareId) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .unique() + .name("idx_hardware_events_erstellt_am") + .table(HardwareEvents::Table) + .col(HardwareEvents::ErstelltAm) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .unique() + .name("idx_hardware_events_event") + .table(HardwareEvents::Table) + .col(HardwareEvents::Event) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(HardwareEvents::Table).to_owned()) + .await?; + + manager + .drop_type(Type::drop().name(EventType).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum HardwareEvents { + Table, + Id, + HardwareId, + Event, + Payload, + Version, + ErstelltAm, + BenutzerId, +}