migration GeräteEvents hinzugefügt; Entities erstellt

This commit is contained in:
Peter 2025-02-04 00:40:08 +01:00
parent e94bc657e8
commit 530b8c31d6
13 changed files with 359 additions and 6 deletions

View File

@ -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
```

View File

@ -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<super::hardware::Entity> for Entity {
fn to() -> RelationDef {
Relation::Hardware.def()
}
}
impl Related<super::hardware_events::Entity> for Entity {
fn to() -> RelationDef {
Relation::HardwareEvents.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -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<super::benutzer::Entity> for Entity {
fn to() -> RelationDef {
Relation::Benutzer.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -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<super::benutzer::Entity> for Entity {
fn to() -> RelationDef {
Relation::Benutzer.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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<String>,
pub status: String,
pub created_at: chrono::DateTime<chrono::Utc>,
}
// Conversion from Entity to Response Model
impl From<entities::devices::Model> 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,
}
}
}

View File

@ -0,0 +1 @@
pub mod benutzer;

View File

@ -0,0 +1 @@
pub use super::benutzer::ErstelleBenutzerDto as ErstelleBenutzer;

View File

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

View File

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

View File

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