From eea2472f6bdd8022ebf76ae24706c869ae76e091 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 7 Nov 2024 18:11:02 +0100 Subject: [PATCH] add thumbnails model --- migration/src/lib.rs | 4 +- .../m20241106_005545_project_thumbnails.rs | 69 +++++++++++++++++++ src/models/_entities/data.rs | 20 +++++- src/models/_entities/mod.rs | 1 + src/models/_entities/prelude.rs | 1 + src/models/_entities/project_thumbnails.rs | 47 +++++++++++++ src/models/_entities/projects.rs | 20 +++++- src/models/mod.rs | 1 + src/models/project_thumbnails.rs | 7 ++ src/models/projects.rs | 9 +++ tests/models/mod.rs | 3 +- tests/models/project_thumbnails.rs | 31 +++++++++ 12 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 migration/src/m20241106_005545_project_thumbnails.rs create mode 100644 src/models/_entities/project_thumbnails.rs create mode 100644 src/models/project_thumbnails.rs create mode 100644 tests/models/project_thumbnails.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index bf6d965..b0a3974 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -8,6 +8,7 @@ mod m20241029_235230_skills; mod m20241030_002154_jobs; mod m20241030_024340_projects; mod m20241030_024830_data; +mod m20241106_005545_project_thumbnails; pub struct Migrator; #[async_trait::async_trait] @@ -15,6 +16,7 @@ impl MigratorTrait for Migrator { fn migrations() -> Vec> { vec![ // inject-below + Box::new(m20241106_005545_project_thumbnails::Migration), Box::new(m20241030_024830_data::Migration), Box::new(m20241030_024340_projects::Migration), Box::new(m20241030_002154_jobs::Migration), @@ -22,4 +24,4 @@ impl MigratorTrait for Migrator { Box::new(m20220101_000001_users::Migration), ] } -} +} \ No newline at end of file diff --git a/migration/src/m20241106_005545_project_thumbnails.rs b/migration/src/m20241106_005545_project_thumbnails.rs new file mode 100644 index 0000000..5a71a08 --- /dev/null +++ b/migration/src/m20241106_005545_project_thumbnails.rs @@ -0,0 +1,69 @@ +use loco_rs::schema::table_auto_tz; +use sea_orm_migration::{prelude::*, schema::*}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + table_auto_tz(ProjectThumbnails::Table) + .primary_key( + Index::create() + .name("idx-project_thumbnails-refs-pk") + .table(ProjectThumbnails::Table) + .col(ProjectThumbnails::ProjectId) + .col(ProjectThumbnails::DataId) + , + ) + .col(integer(ProjectThumbnails::ProjectId)) + .col(integer(ProjectThumbnails::DataId)) + .foreign_key( + ForeignKey::create() + .name("fk-project_thumbnails-projects") + .from(ProjectThumbnails::Table, ProjectThumbnails::ProjectId) + .to(Projects::Table, Projects::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .name("fk-project_thumbnails-data") + .from(ProjectThumbnails::Table, ProjectThumbnails::DataId) + .to(Data::Table, Data::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(ProjectThumbnails::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum ProjectThumbnails { + Table, + ProjectId, + DataId, + +} + + +#[derive(DeriveIden)] +enum Projects { + Table, + Id, +} +#[derive(DeriveIden)] +enum Data { + Table, + Id, +} diff --git a/src/models/_entities/data.rs b/src/models/_entities/data.rs index b9f9f38..50cbdcd 100644 --- a/src/models/_entities/data.rs +++ b/src/models/_entities/data.rs @@ -16,4 +16,22 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::project_thumbnails::Entity")] + ProjectThumbnails, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ProjectThumbnails.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::project_thumbnails::Relation::Projects.def() + } + fn via() -> Option { + Some(super::project_thumbnails::Relation::Data.def().rev()) + } +} diff --git a/src/models/_entities/mod.rs b/src/models/_entities/mod.rs index 7d3cb49..f620d13 100644 --- a/src/models/_entities/mod.rs +++ b/src/models/_entities/mod.rs @@ -4,6 +4,7 @@ pub mod prelude; pub mod data; pub mod jobs; +pub mod project_thumbnails; pub mod projects; pub mod skills; pub mod users; diff --git a/src/models/_entities/prelude.rs b/src/models/_entities/prelude.rs index ebe2b91..4187500 100644 --- a/src/models/_entities/prelude.rs +++ b/src/models/_entities/prelude.rs @@ -2,6 +2,7 @@ pub use super::data::Entity as Data; pub use super::jobs::Entity as Jobs; +pub use super::project_thumbnails::Entity as ProjectThumbnails; pub use super::projects::Entity as Projects; pub use super::skills::Entity as Skills; pub use super::users::Entity as Users; diff --git a/src/models/_entities/project_thumbnails.rs b/src/models/_entities/project_thumbnails.rs new file mode 100644 index 0000000..5d53a4d --- /dev/null +++ b/src/models/_entities/project_thumbnails.rs @@ -0,0 +1,47 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "project_thumbnails")] +pub struct Model { + pub created_at: DateTimeWithTimeZone, + pub updated_at: DateTimeWithTimeZone, + #[sea_orm(primary_key, auto_increment = false)] + pub project_id: i32, + #[sea_orm(primary_key, auto_increment = false)] + pub data_id: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::data::Entity", + from = "Column::DataId", + to = "super::data::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + Data, + #[sea_orm( + belongs_to = "super::projects::Entity", + from = "Column::ProjectId", + to = "super::projects::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + Projects, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Data.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Projects.def() + } +} diff --git a/src/models/_entities/projects.rs b/src/models/_entities/projects.rs index 3de2e60..2b0e0c2 100644 --- a/src/models/_entities/projects.rs +++ b/src/models/_entities/projects.rs @@ -21,4 +21,22 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::project_thumbnails::Entity")] + ProjectThumbnails, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ProjectThumbnails.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::project_thumbnails::Relation::Data.def() + } + fn via() -> Option { + Some(super::project_thumbnails::Relation::Projects.def().rev()) + } +} \ No newline at end of file diff --git a/src/models/mod.rs b/src/models/mod.rs index c24d2a1..5d3fdfd 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -4,3 +4,4 @@ pub mod skills; pub mod jobs; pub mod projects; pub mod data; +pub mod project_thumbnails; diff --git a/src/models/project_thumbnails.rs b/src/models/project_thumbnails.rs new file mode 100644 index 0000000..3158e9d --- /dev/null +++ b/src/models/project_thumbnails.rs @@ -0,0 +1,7 @@ +use sea_orm::entity::prelude::*; +use super::_entities::project_thumbnails::{ActiveModel, Entity}; +pub type ProjectThumbnails = Entity; + +impl ActiveModelBehavior for ActiveModel { + // extend activemodel below (keep comment for generators) +} diff --git a/src/models/projects.rs b/src/models/projects.rs index 771183d..3fee725 100644 --- a/src/models/projects.rs +++ b/src/models/projects.rs @@ -19,3 +19,12 @@ struct ProjectWithTechnologies { pub download_url: Option, pub visit_url: Option, } + +#[derive(Debug)] +pub enum Category { + Web, + Mobile, + Desktop, + Game, + Api, +} diff --git a/tests/models/mod.rs b/tests/models/mod.rs index 8925bf3..533c73b 100644 --- a/tests/models/mod.rs +++ b/tests/models/mod.rs @@ -3,4 +3,5 @@ mod users; mod skills; mod jobs; mod projects; -mod data; \ No newline at end of file +mod data; +mod project_thumbnails; \ No newline at end of file diff --git a/tests/models/project_thumbnails.rs b/tests/models/project_thumbnails.rs new file mode 100644 index 0000000..45d14a2 --- /dev/null +++ b/tests/models/project_thumbnails.rs @@ -0,0 +1,31 @@ +use gabrielkaszewski_rs::app::App; +use loco_rs::testing; +use serial_test::serial; + +macro_rules! configure_insta { + ($($expr:expr),*) => { + let mut settings = insta::Settings::clone_current(); + settings.set_prepend_module_to_snapshot(false); + let _guard = settings.bind_to_scope(); + }; +} + +#[tokio::test] +#[serial] +async fn test_model() { + configure_insta!(); + + let boot = testing::boot_test::().await.unwrap(); + testing::seed::(&boot.app_context.db).await.unwrap(); + + // query your model, e.g.: + // + // let item = models::posts::Model::find_by_pid( + // &boot.app_context.db, + // "11111111-1111-1111-1111-111111111111", + // ) + // .await; + + // snapshot the result: + // assert_debug_snapshot!(item); +}