server: Add a table for ingredients

This commit is contained in:
traxys 2023-05-29 21:00:40 +02:00
parent f88aecd306
commit d5f3edc33f
7 changed files with 101 additions and 2 deletions

View file

@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod m20220101_000001_account;
mod m20230520_203638_household;
mod m20230529_184433_ingredients;
pub struct Migrator;
@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20220101_000001_account::Migration),
Box::new(m20230520_203638_household::Migration),
Box::new(m20230529_184433_ingredients::Migration),
]
}
}

View file

@ -6,7 +6,7 @@ use super::m20220101_000001_account::User;
pub struct Migration;
#[derive(Iden)]
enum Household {
pub enum Household {
Table,
Name,
Id,

View file

@ -0,0 +1,53 @@
use sea_orm_migration::prelude::*;
use crate::m20230520_203638_household::Household;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(Iden)]
enum Ingredient {
Table,
Household,
Id,
Name,
Unit,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Ingredient::Table)
.if_not_exists()
.col(
ColumnDef::new(Ingredient::Id)
.not_null()
.auto_increment()
.big_integer()
.primary_key(),
)
.col(ColumnDef::new(Ingredient::Household).uuid().not_null())
.col(ColumnDef::new(Ingredient::Name).string().not_null())
.col(ColumnDef::new(Ingredient::Unit).string())
.foreign_key(
ForeignKey::create()
.name("FK_ingredients_household")
.from(Ingredient::Table, Ingredient::Household)
.to(Household::Table, Household::Id)
.on_delete(ForeignKeyAction::Restrict)
.on_update(ForeignKeyAction::Restrict),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Ingredient::Table).to_owned())
.await
}
}

View file

@ -11,7 +11,16 @@ pub struct Model {
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::ingredient::Entity")]
Ingredient,
}
impl Related<super::ingredient::Entity> for Entity {
fn to() -> RelationDef {
Relation::Ingredient.def()
}
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {

33
src/entity/ingredient.rs Normal file
View file

@ -0,0 +1,33 @@
//! `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 = "ingredient")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
pub household: Uuid,
pub name: String,
pub unit: Option<String>,
}
#[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,
}
impl Related<super::household::Entity> for Entity {
fn to() -> RelationDef {
Relation::Household.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -4,4 +4,5 @@ pub mod prelude;
pub mod household;
pub mod household_members;
pub mod ingredient;
pub mod user;

View file

@ -2,4 +2,5 @@
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::user::Entity as User;