diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 4edbbda..df0617d 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_account; mod m20230520_203638_household; mod m20230529_184433_ingredients; +mod m20230618_163416_recipe; pub struct Migrator; @@ -13,6 +14,7 @@ impl MigratorTrait for Migrator { Box::new(m20220101_000001_account::Migration), Box::new(m20230520_203638_household::Migration), Box::new(m20230529_184433_ingredients::Migration), + Box::new(m20230618_163416_recipe::Migration), ] } } diff --git a/migration/src/m20230529_184433_ingredients.rs b/migration/src/m20230529_184433_ingredients.rs index bdac400..d161c2c 100644 --- a/migration/src/m20230529_184433_ingredients.rs +++ b/migration/src/m20230529_184433_ingredients.rs @@ -6,7 +6,7 @@ use crate::m20230520_203638_household::Household; pub struct Migration; #[derive(Iden)] -enum Ingredient { +pub enum Ingredient { Table, Household, Id, diff --git a/migration/src/m20230618_163416_recipe.rs b/migration/src/m20230618_163416_recipe.rs new file mode 100644 index 0000000..2f68ea7 --- /dev/null +++ b/migration/src/m20230618_163416_recipe.rs @@ -0,0 +1,145 @@ +use sea_orm_migration::prelude::*; + +use crate::m20230529_184433_ingredients::Ingredient; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[derive(Iden)] +enum Recipe { + Table, + Id, + Name, + Ranking, +} + +#[derive(Iden)] +enum RecipeIngerdients { + Table, + RecipeId, + IngredientId, + Amount, +} + +#[derive(Iden)] +enum RecipeSteps { + Table, + RecipeId, + Num, + Text, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Recipe::Table) + .if_not_exists() + .col( + ColumnDef::new(Recipe::Id) + .big_integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Recipe::Name).text().not_null()) + .col(ColumnDef::new(Recipe::Ranking).integer().not_null()) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(RecipeSteps::Table) + .if_not_exists() + .col( + ColumnDef::new(RecipeSteps::RecipeId) + .big_integer() + .not_null(), + ) + .col(ColumnDef::new(RecipeSteps::Num).integer().not_null()) + .col(ColumnDef::new(RecipeSteps::Text).text().not_null()) + .primary_key( + Index::create() + .col(RecipeSteps::Num) + .col(RecipeSteps::RecipeId), + ) + .foreign_key( + ForeignKey::create() + .name("FK_recipe_steps") + .from(RecipeSteps::Table, RecipeSteps::RecipeId) + .to(Recipe::Table, Recipe::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(RecipeIngerdients::Table) + .if_not_exists() + .col( + ColumnDef::new(RecipeIngerdients::RecipeId) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(RecipeIngerdients::IngredientId) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(RecipeIngerdients::Amount) + .double() + .not_null(), + ) + .primary_key( + Index::create() + .col(RecipeIngerdients::IngredientId) + .col(RecipeIngerdients::RecipeId), + ) + .foreign_key( + ForeignKey::create() + .name("FK_recipe_ingredients_recipe") + .from(RecipeIngerdients::Table, RecipeIngerdients::RecipeId) + .to(Recipe::Table, Recipe::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .foreign_key( + ForeignKey::create() + .name("FK_recipe_ingredients_ingredient") + .from(RecipeIngerdients::Table, RecipeIngerdients::IngredientId) + .to(Ingredient::Table, Ingredient::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(RecipeIngerdients::Table).to_owned()) + .await?; + + manager + .drop_table(Table::drop().table(RecipeSteps::Table).to_owned()) + .await?; + + manager + .drop_table(Table::drop().table(Recipe::Table).to_owned()) + .await?; + + Ok(()) + } +} diff --git a/src/entity/ingredient.rs b/src/entity/ingredient.rs index 81ddb71..4e37cff 100644 --- a/src/entity/ingredient.rs +++ b/src/entity/ingredient.rs @@ -30,4 +30,13 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + super::recipe_ingerdients::Relation::Recipe.def() + } + fn via() -> Option { + Some(super::recipe_ingerdients::Relation::Ingredient.def().rev()) + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/mod.rs b/src/entity/mod.rs index aa018ec..950eb7e 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -5,4 +5,7 @@ pub mod prelude; pub mod household; pub mod household_members; pub mod ingredient; +pub mod recipe; +pub mod recipe_ingerdients; +pub mod recipe_steps; pub mod user; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 654e5b9..f516608 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -3,4 +3,7 @@ pub use super::household::Entity as Household; pub use super::household_members::Entity as HouseholdMembers; pub use super::ingredient::Entity as Ingredient; +pub use super::recipe::Entity as Recipe; +pub use super::recipe_ingerdients::Entity as RecipeIngerdients; +pub use super::recipe_steps::Entity as RecipeSteps; pub use super::user::Entity as User; diff --git a/src/entity/recipe.rs b/src/entity/recipe.rs new file mode 100644 index 0000000..35182b6 --- /dev/null +++ b/src/entity/recipe.rs @@ -0,0 +1,36 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "recipe")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i64, + #[sea_orm(column_type = "Text")] + pub name: String, + pub ranking: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::recipe_steps::Entity")] + RecipeSteps, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::RecipeSteps.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::recipe_ingerdients::Relation::Ingredient.def() + } + fn via() -> Option { + Some(super::recipe_ingerdients::Relation::Recipe.def().rev()) + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/recipe_ingerdients.rs b/src/entity/recipe_ingerdients.rs new file mode 100644 index 0000000..7398349 --- /dev/null +++ b/src/entity/recipe_ingerdients.rs @@ -0,0 +1,48 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "recipe_ingerdients")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub recipe_id: i64, + #[sea_orm(primary_key, auto_increment = false)] + pub ingredient_id: i64, + #[sea_orm(column_type = "Double")] + pub amount: f64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::ingredient::Entity", + from = "Column::IngredientId", + to = "super::ingredient::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + Ingredient, + #[sea_orm( + belongs_to = "super::recipe::Entity", + from = "Column::RecipeId", + to = "super::recipe::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + Recipe, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Ingredient.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Recipe.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/recipe_steps.rs b/src/entity/recipe_steps.rs new file mode 100644 index 0000000..01e8e00 --- /dev/null +++ b/src/entity/recipe_steps.rs @@ -0,0 +1,34 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "recipe_steps")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub recipe_id: i64, + #[sea_orm(primary_key, auto_increment = false)] + pub num: i32, + #[sea_orm(column_type = "Text")] + pub text: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::recipe::Entity", + from = "Column::RecipeId", + to = "super::recipe::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + Recipe, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Recipe.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}