server: Link recipe to households
This commit is contained in:
parent
cec3ef214e
commit
69197a3852
4 changed files with 35 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm_migration::prelude::*;
|
use sea_orm_migration::prelude::*;
|
||||||
|
|
||||||
use crate::m20230529_184433_ingredients::Ingredient;
|
use crate::{m20230520_203638_household::Household, m20230529_184433_ingredients::Ingredient};
|
||||||
|
|
||||||
#[derive(DeriveMigrationName)]
|
#[derive(DeriveMigrationName)]
|
||||||
pub struct Migration;
|
pub struct Migration;
|
||||||
|
|
@ -9,6 +9,7 @@ pub struct Migration;
|
||||||
enum Recipe {
|
enum Recipe {
|
||||||
Table,
|
Table,
|
||||||
Id,
|
Id,
|
||||||
|
Household,
|
||||||
Name,
|
Name,
|
||||||
Ranking,
|
Ranking,
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +47,15 @@ impl MigrationTrait for Migration {
|
||||||
)
|
)
|
||||||
.col(ColumnDef::new(Recipe::Name).text().not_null())
|
.col(ColumnDef::new(Recipe::Name).text().not_null())
|
||||||
.col(ColumnDef::new(Recipe::Ranking).integer().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(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ pub struct Model {
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
#[sea_orm(has_many = "super::ingredient::Entity")]
|
#[sea_orm(has_many = "super::ingredient::Entity")]
|
||||||
Ingredient,
|
Ingredient,
|
||||||
|
#[sea_orm(has_many = "super::recipe::Entity")]
|
||||||
|
Recipe,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Related<super::ingredient::Entity> for Entity {
|
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 {
|
impl Related<super::user::Entity> for Entity {
|
||||||
fn to() -> RelationDef {
|
fn to() -> RelationDef {
|
||||||
super::household_members::Relation::User.def()
|
super::household_members::Relation::User.def()
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,29 @@ pub struct Model {
|
||||||
#[sea_orm(column_type = "Text")]
|
#[sea_orm(column_type = "Text")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub ranking: i32,
|
pub ranking: i32,
|
||||||
|
pub household: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {
|
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")]
|
#[sea_orm(has_many = "super::recipe_steps::Entity")]
|
||||||
RecipeSteps,
|
RecipeSteps,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::household::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Household.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Related<super::recipe_steps::Entity> for Entity {
|
impl Related<super::recipe_steps::Entity> for Entity {
|
||||||
fn to() -> RelationDef {
|
fn to() -> RelationDef {
|
||||||
Relation::RecipeSteps.def()
|
Relation::RecipeSteps.def()
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ pub(super) async fn create_recipe(
|
||||||
let model = recipe::ActiveModel {
|
let model = recipe::ActiveModel {
|
||||||
name: ActiveValue::Set(request.name),
|
name: ActiveValue::Set(request.name),
|
||||||
ranking: ActiveValue::Set(request.rating as i32),
|
ranking: ActiveValue::Set(request.rating as i32),
|
||||||
|
household: ActiveValue::Set(household.id),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue