From c238b8ec45245936f86292b18725ef39c0a8cf06 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sat, 12 Nov 2022 11:27:13 +0100 Subject: [PATCH] feat: candidate fk on parent --- core/src/services/candidate_service.rs | 7 ++--- entity/src/admin.rs | 4 ++- entity/src/candidate.rs | 10 +++++++ entity/src/mod.rs | 2 ++ entity/src/parent.rs | 19 +++++++++++++- entity/src/prelude.rs | 2 ++ entity/src/session.rs | 18 ++++++------- migration/src/lib.rs | 2 ++ ...21112_112212_create_parent_candidate_fk.rs | 26 +++++++++++++++++++ 9 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 migration/src/m20221112_112212_create_parent_candidate_fk.rs diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index b5ced20..f0e1e9d 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -225,14 +225,15 @@ mod tests { let secret_message = "trnka".to_string(); - Mutation::create_parent(&db, 1) - .await.unwrap(); - + let candidate = CandidateService::create(&db, 103151, &plain_text_password, "".to_string()) .await .ok() .unwrap(); + Mutation::create_parent(&db, 103151) + .await.unwrap(); + let encrypted_message = crypto::encrypt_password_with_recipients(&secret_message, &vec![&candidate.public_key]) .await diff --git a/entity/src/admin.rs b/entity/src/admin.rs index cc51dae..8486730 100644 --- a/entity/src/admin.rs +++ b/entity/src/admin.rs @@ -1,9 +1,11 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[sea_orm(table_name = "admin")] pub struct Model { - #[sea_orm(column_type = "Integer", primary_key)] + #[sea_orm(primary_key)] pub id: i32, pub name: String, pub public_key: String, diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index 6a044fa..6b7d5bd 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -1,3 +1,5 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] @@ -28,10 +30,18 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm(has_many = "super::parent::Entity")] + Parent, #[sea_orm(has_many = "super::session::Entity")] Session, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::Session.def() diff --git a/entity/src/mod.rs b/entity/src/mod.rs index ef5f522..63cf621 100644 --- a/entity/src/mod.rs +++ b/entity/src/mod.rs @@ -1,3 +1,5 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + pub mod prelude; pub mod admin; diff --git a/entity/src/parent.rs b/entity/src/parent.rs index 7e1b00c..f748482 100644 --- a/entity/src/parent.rs +++ b/entity/src/parent.rs @@ -1,3 +1,5 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] @@ -14,6 +16,21 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::candidate::Entity", + from = "Column::Application", + to = "super::candidate::Column::Application", + on_update = "Cascade", + on_delete = "Cascade" + )] + Candidate, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Candidate.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/entity/src/prelude.rs b/entity/src/prelude.rs index af8586b..73b608f 100644 --- a/entity/src/prelude.rs +++ b/entity/src/prelude.rs @@ -1,3 +1,5 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + pub use super::admin::Entity as Admin; pub use super::candidate::Entity as Candidate; pub use super::parent::Entity as Parent; diff --git a/entity/src/session.rs b/entity/src/session.rs index f4ecf37..e0b5829 100644 --- a/entity/src/session.rs +++ b/entity/src/session.rs @@ -1,3 +1,5 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 + use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] @@ -5,10 +7,8 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: Uuid, - #[sea_orm(column_type = "Integer", nullable)] - pub admin_id: Option, - #[sea_orm(column_type = "Integer", nullable)] pub user_id: Option, + pub admin_id: Option, pub ip_address: String, pub created_at: DateTime, pub expires_at: DateTime, @@ -34,16 +34,16 @@ pub enum Relation { Candidate, } -impl Related for Entity { - fn to() -> RelationDef { - Relation::Candidate.def() - } -} - impl Related for Entity { fn to() -> RelationDef { Relation::Admin.def() } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Candidate.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 511d8c0..6e2081d 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -7,6 +7,7 @@ mod m20221024_134454_insert_sample_admin; mod m20221025_154422_create_session; mod m20221027_194728_session_create_user_fk; mod m20221028_194728_session_create_admin_fk; +mod m20221112_112212_create_parent_candidate_fk; pub struct Migrator; #[async_trait::async_trait] @@ -20,6 +21,7 @@ impl MigratorTrait for Migrator { Box::new(m20221025_154422_create_session::Migration), Box::new(m20221027_194728_session_create_user_fk::Migration), Box::new(m20221028_194728_session_create_admin_fk::Migration), + Box::new(m20221112_112212_create_parent_candidate_fk::Migration), ] } } diff --git a/migration/src/m20221112_112212_create_parent_candidate_fk.rs b/migration/src/m20221112_112212_create_parent_candidate_fk.rs new file mode 100644 index 0000000..c390fa0 --- /dev/null +++ b/migration/src/m20221112_112212_create_parent_candidate_fk.rs @@ -0,0 +1,26 @@ +use sea_orm_migration::prelude::*; + +use crate::{m20221024_124701_create_parent::Parent, m20221024_121621_create_candidate::Candidate}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.create_foreign_key(ForeignKey::create() + .name("candidate_fk") + .from(Parent::Table, Parent::Application) + .to(Candidate::Table, Candidate::Application) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned()).await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.drop_foreign_key(ForeignKey::drop() + .name("candidate_fk") + .table(Parent::Table) + .to_owned()).await + } +} \ No newline at end of file