diff --git a/migration/src/lib.rs b/migration/src/lib.rs index f5645ed..08e7ff4 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -6,6 +6,8 @@ mod m20220101_000001_users; mod m20241029_235230_skills; mod m20241030_002154_jobs; +mod m20241030_024340_projects; +mod m20241030_024830_data; pub struct Migrator; #[async_trait::async_trait] @@ -13,6 +15,8 @@ impl MigratorTrait for Migrator { fn migrations() -> Vec> { vec![ // inject-below + Box::new(m20241030_024830_data::Migration), + Box::new(m20241030_024340_projects::Migration), Box::new(m20241030_002154_jobs::Migration), Box::new(m20241029_235230_skills::Migration), Box::new(m20220101_000001_users::Migration), diff --git a/migration/src/m20241030_024340_projects.rs b/migration/src/m20241030_024340_projects.rs new file mode 100644 index 0000000..3d4e0a2 --- /dev/null +++ b/migration/src/m20241030_024340_projects.rs @@ -0,0 +1,49 @@ +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(Projects::Table) + .col(pk_auto(Projects::Id)) + .col(string(Projects::Name)) + .col(string(Projects::Technology)) + .col(string(Projects::ShortDescription)) + .col(string_null(Projects::Description)) + .col(string(Projects::Category)) + .col(string_null(Projects::GithubUrl)) + .col(string_null(Projects::DownloadUrl)) + .col(string_null(Projects::VisitUrl)) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Projects::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum Projects { + Table, + Id, + Name, + Technology, + ShortDescription, + Description, + Category, + GithubUrl, + DownloadUrl, + VisitUrl, + +} + + diff --git a/migration/src/m20241030_024830_data.rs b/migration/src/m20241030_024830_data.rs new file mode 100644 index 0000000..ae511cc --- /dev/null +++ b/migration/src/m20241030_024830_data.rs @@ -0,0 +1,37 @@ +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(Data::Table) + .col(pk_auto(Data::Id)) + .col(string(Data::FileUrl)) + .col(boolean(Data::Protected)) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Data::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum Data { + Table, + Id, + FileUrl, + Protected, + +} + + diff --git a/src/models/_entities/data.rs b/src/models/_entities/data.rs new file mode 100644 index 0000000..9ae0816 --- /dev/null +++ b/src/models/_entities/data.rs @@ -0,0 +1,18 @@ +//! `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 = "data")] +pub struct Model { + pub created_at: DateTimeWithTimeZone, + pub updated_at: DateTimeWithTimeZone, + #[sea_orm(primary_key)] + pub id: i32, + pub file_url: String, + pub protected: bool, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} diff --git a/src/models/_entities/jobs.rs b/src/models/_entities/jobs.rs index e0d1165..46522aa 100644 --- a/src/models/_entities/jobs.rs +++ b/src/models/_entities/jobs.rs @@ -19,4 +19,4 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} \ No newline at end of file +pub enum Relation {} diff --git a/src/models/_entities/mod.rs b/src/models/_entities/mod.rs index 5fc3168..7d3cb49 100644 --- a/src/models/_entities/mod.rs +++ b/src/models/_entities/mod.rs @@ -2,6 +2,8 @@ pub mod prelude; +pub mod data; pub mod jobs; +pub mod projects; pub mod skills; pub mod users; diff --git a/src/models/_entities/prelude.rs b/src/models/_entities/prelude.rs index ed26b77..ebe2b91 100644 --- a/src/models/_entities/prelude.rs +++ b/src/models/_entities/prelude.rs @@ -1,5 +1,7 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 +pub use super::data::Entity as Data; pub use super::jobs::Entity as Jobs; +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/projects.rs b/src/models/_entities/projects.rs new file mode 100644 index 0000000..3de2e60 --- /dev/null +++ b/src/models/_entities/projects.rs @@ -0,0 +1,24 @@ +//! `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 = "projects")] +pub struct Model { + pub created_at: DateTimeWithTimeZone, + pub updated_at: DateTimeWithTimeZone, + #[sea_orm(primary_key)] + pub id: i32, + pub name: String, + pub technology: String, + pub short_description: String, + pub description: Option, + pub category: String, + pub github_url: Option, + pub download_url: Option, + pub visit_url: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} diff --git a/src/models/data.rs b/src/models/data.rs new file mode 100644 index 0000000..b33c404 --- /dev/null +++ b/src/models/data.rs @@ -0,0 +1,7 @@ +use sea_orm::entity::prelude::*; +use super::_entities::data::{ActiveModel, Entity}; +pub type Data = Entity; + +impl ActiveModelBehavior for ActiveModel { + // extend activemodel below (keep comment for generators) +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 0f94d27..c24d2a1 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -2,3 +2,5 @@ pub mod _entities; pub mod users; pub mod skills; pub mod jobs; +pub mod projects; +pub mod data; diff --git a/src/models/projects.rs b/src/models/projects.rs new file mode 100644 index 0000000..771183d --- /dev/null +++ b/src/models/projects.rs @@ -0,0 +1,21 @@ +use super::_entities::projects::{ActiveModel, Entity}; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; +pub type Projects = Entity; + +impl ActiveModelBehavior for ActiveModel { + // extend activemodel below (keep comment for generators) +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +struct ProjectWithTechnologies { + pub id: i32, + pub name: String, + pub technologies: Vec, + pub short_description: String, + pub description: Option, + pub category: String, + pub github_url: Option, + pub download_url: Option, + pub visit_url: Option, +} diff --git a/src/services/jobs.rs b/src/services/jobs.rs index 39d25f2..b01463f 100644 --- a/src/services/jobs.rs +++ b/src/services/jobs.rs @@ -1,7 +1,7 @@ use loco_rs::prelude::*; use crate::models::{ - _entities::jobs::{ActiveModel, Entity, Model}, + _entities::jobs::{Entity, Model}, jobs::{get_technologies_from_string, JobWithTechnologies}, }; diff --git a/src/services/skills.rs b/src/services/skills.rs index be694fc..64ef4fd 100644 --- a/src/services/skills.rs +++ b/src/services/skills.rs @@ -1,6 +1,6 @@ use loco_rs::prelude::*; -use crate::models::_entities::skills::{ActiveModel, Entity, Model}; +use crate::models::_entities::skills::{Entity, Model}; pub async fn get_all_skills(ctx: &AppContext) -> Result> { let skills = Entity::find().all(&ctx.db).await?; diff --git a/tests/models/data.rs b/tests/models/data.rs new file mode 100644 index 0000000..45d14a2 --- /dev/null +++ b/tests/models/data.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); +} diff --git a/tests/models/mod.rs b/tests/models/mod.rs index 5378f29..8925bf3 100644 --- a/tests/models/mod.rs +++ b/tests/models/mod.rs @@ -1,4 +1,6 @@ mod users; mod skills; -mod jobs; \ No newline at end of file +mod jobs; +mod projects; +mod data; \ No newline at end of file diff --git a/tests/models/projects.rs b/tests/models/projects.rs new file mode 100644 index 0000000..45d14a2 --- /dev/null +++ b/tests/models/projects.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); +}