server: Link recipe to households

This commit is contained in:
traxys 2023-06-22 23:03:50 +02:00
parent cec3ef214e
commit 69197a3852
4 changed files with 35 additions and 1 deletions

View file

@ -1,6 +1,6 @@
use sea_orm_migration::prelude::*;
use crate::m20230529_184433_ingredients::Ingredient;
use crate::{m20230520_203638_household::Household, m20230529_184433_ingredients::Ingredient};
#[derive(DeriveMigrationName)]
pub struct Migration;
@ -9,6 +9,7 @@ pub struct Migration;
enum Recipe {
Table,
Id,
Household,
Name,
Ranking,
}
@ -46,6 +47,15 @@ impl MigrationTrait for Migration {
)
.col(ColumnDef::new(Recipe::Name).text().not_null())
.col(ColumnDef::new(Recipe::Ranking).integer().not_null())
.col(ColumnDef::new(Recipe::Household).uuid().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_recipe_household")
.from(Recipe::Table, Recipe::Household)
.to(Household::Table, Household::Id)
.on_delete(ForeignKeyAction::Restrict)
.on_update(ForeignKeyAction::Restrict),
)
.to_owned(),
)
.await?;

View file

@ -14,6 +14,8 @@ pub struct Model {
pub enum Relation {
#[sea_orm(has_many = "super::ingredient::Entity")]
Ingredient,
#[sea_orm(has_many = "super::recipe::Entity")]
Recipe,
}
impl Related<super::ingredient::Entity> for Entity {
@ -22,6 +24,12 @@ impl Related<super::ingredient::Entity> for Entity {
}
}
impl Related<super::recipe::Entity> for Entity {
fn to() -> RelationDef {
Relation::Recipe.def()
}
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
super::household_members::Relation::User.def()

View file

@ -10,14 +10,29 @@ pub struct Model {
#[sea_orm(column_type = "Text")]
pub name: String,
pub ranking: i32,
pub household: Uuid,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::household::Entity",
from = "Column::Household",
to = "super::household::Column::Id",
on_update = "Restrict",
on_delete = "Restrict"
)]
Household,
#[sea_orm(has_many = "super::recipe_steps::Entity")]
RecipeSteps,
}
impl Related<super::household::Entity> for Entity {
fn to() -> RelationDef {
Relation::Household.def()
}
}
impl Related<super::recipe_steps::Entity> for Entity {
fn to() -> RelationDef {
Relation::RecipeSteps.def()

View file

@ -18,6 +18,7 @@ pub(super) async fn create_recipe(
let model = recipe::ActiveModel {
name: ActiveValue::Set(request.name),
ranking: ActiveValue::Set(request.rating as i32),
household: ActiveValue::Set(household.id),
..Default::default()
};