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

View file

@ -30,4 +30,13 @@ impl Related<super::household::Entity> for Entity {
}
}
impl Related<super::recipe::Entity> for Entity {
fn to() -> RelationDef {
super::recipe_ingerdients::Relation::Recipe.def()
}
fn via() -> Option<RelationDef> {
Some(super::recipe_ingerdients::Relation::Ingredient.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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;

View file

@ -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;

36
src/entity/recipe.rs Normal file
View file

@ -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<super::recipe_steps::Entity> for Entity {
fn to() -> RelationDef {
Relation::RecipeSteps.def()
}
}
impl Related<super::ingredient::Entity> for Entity {
fn to() -> RelationDef {
super::recipe_ingerdients::Relation::Ingredient.def()
}
fn via() -> Option<RelationDef> {
Some(super::recipe_ingerdients::Relation::Recipe.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<super::ingredient::Entity> for Entity {
fn to() -> RelationDef {
Relation::Ingredient.def()
}
}
impl Related<super::recipe::Entity> for Entity {
fn to() -> RelationDef {
Relation::Recipe.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<super::recipe::Entity> for Entity {
fn to() -> RelationDef {
Relation::Recipe.def()
}
}
impl ActiveModelBehavior for ActiveModel {}