server: Allow to store recipes

This commit is contained in:
traxys 2023-06-22 22:29:22 +02:00
parent 1c4d4a71e5
commit 82b466f52c
9 changed files with 281 additions and 1 deletions

View file

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

View file

@ -6,7 +6,7 @@ use crate::m20230520_203638_household::Household;
pub struct Migration;
#[derive(Iden)]
enum Ingredient {
pub enum Ingredient {
Table,
Household,
Id,

View file

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