Add project and data model

This commit is contained in:
2024-10-30 03:52:26 +01:00
parent ea4942bda1
commit c83c0e7ec4
16 changed files with 234 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ mod m20220101_000001_users;
mod m20241029_235230_skills; mod m20241029_235230_skills;
mod m20241030_002154_jobs; mod m20241030_002154_jobs;
mod m20241030_024340_projects;
mod m20241030_024830_data;
pub struct Migrator; pub struct Migrator;
#[async_trait::async_trait] #[async_trait::async_trait]
@@ -13,6 +15,8 @@ impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> { fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![ vec![
// inject-below // inject-below
Box::new(m20241030_024830_data::Migration),
Box::new(m20241030_024340_projects::Migration),
Box::new(m20241030_002154_jobs::Migration), Box::new(m20241030_002154_jobs::Migration),
Box::new(m20241029_235230_skills::Migration), Box::new(m20241029_235230_skills::Migration),
Box::new(m20220101_000001_users::Migration), Box::new(m20220101_000001_users::Migration),

View File

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

View File

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

View File

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

View File

@@ -2,6 +2,8 @@
pub mod prelude; pub mod prelude;
pub mod data;
pub mod jobs; pub mod jobs;
pub mod projects;
pub mod skills; pub mod skills;
pub mod users; pub mod users;

View File

@@ -1,5 +1,7 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 //! `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::jobs::Entity as Jobs;
pub use super::projects::Entity as Projects;
pub use super::skills::Entity as Skills; pub use super::skills::Entity as Skills;
pub use super::users::Entity as Users; pub use super::users::Entity as Users;

View File

@@ -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<String>,
pub category: String,
pub github_url: Option<String>,
pub download_url: Option<String>,
pub visit_url: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

7
src/models/data.rs Normal file
View File

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

View File

@@ -2,3 +2,5 @@ pub mod _entities;
pub mod users; pub mod users;
pub mod skills; pub mod skills;
pub mod jobs; pub mod jobs;
pub mod projects;
pub mod data;

21
src/models/projects.rs Normal file
View File

@@ -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<String>,
pub short_description: String,
pub description: Option<String>,
pub category: String,
pub github_url: Option<String>,
pub download_url: Option<String>,
pub visit_url: Option<String>,
}

View File

@@ -1,7 +1,7 @@
use loco_rs::prelude::*; use loco_rs::prelude::*;
use crate::models::{ use crate::models::{
_entities::jobs::{ActiveModel, Entity, Model}, _entities::jobs::{Entity, Model},
jobs::{get_technologies_from_string, JobWithTechnologies}, jobs::{get_technologies_from_string, JobWithTechnologies},
}; };

View File

@@ -1,6 +1,6 @@
use loco_rs::prelude::*; 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<Vec<Model>> { pub async fn get_all_skills(ctx: &AppContext) -> Result<Vec<Model>> {
let skills = Entity::find().all(&ctx.db).await?; let skills = Entity::find().all(&ctx.db).await?;

31
tests/models/data.rs Normal file
View File

@@ -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::<App>().await.unwrap();
testing::seed::<App>(&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);
}

View File

@@ -2,3 +2,5 @@ mod users;
mod skills; mod skills;
mod jobs; mod jobs;
mod projects;
mod data;

31
tests/models/projects.rs Normal file
View File

@@ -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::<App>().await.unwrap();
testing::seed::<App>(&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);
}