From c76557135ef1dcef13a2a72e75785d2e1f57b926 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Sun, 27 Jun 2021 15:28:04 +1000 Subject: [PATCH] Create Lineitem --- tests/bakery_chain/lineitem.rs | 80 ++++++++++++++++++++++++++++++++++ tests/bakery_chain/mod.rs | 1 + tests/bakery_chain/order.rs | 14 ++++++ tests/bakery_chain_tests.rs | 32 ++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 tests/bakery_chain/lineitem.rs diff --git a/tests/bakery_chain/lineitem.rs b/tests/bakery_chain/lineitem.rs new file mode 100644 index 00000000..261de494 --- /dev/null +++ b/tests/bakery_chain/lineitem.rs @@ -0,0 +1,80 @@ +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "lineitem" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] +pub struct Model { + pub id: i32, + pub price: f32, + pub quantity: i32, + pub order_id: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + Price, + Quantity, + OrderId, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Order, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::Price => ColumnType::Money(Some((19, 4))).def(), + Self::Quantity => ColumnType::Integer.def(), + Self::OrderId => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Order => Entity::belongs_to(super::order::Entity) + .from(Column::Id) + .to(super::order::Column::CustomerId) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Order.def() + } +} + +impl Model { + pub fn find_orders(&self) -> Select { + Entity::find_related().belongs_to::(self) + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/tests/bakery_chain/mod.rs b/tests/bakery_chain/mod.rs index 22f46f7f..4c557e26 100644 --- a/tests/bakery_chain/mod.rs +++ b/tests/bakery_chain/mod.rs @@ -1,4 +1,5 @@ pub mod baker; pub mod bakery; pub mod customer; +pub mod lineitem; pub mod order; diff --git a/tests/bakery_chain/order.rs b/tests/bakery_chain/order.rs index e3218018..ff655699 100644 --- a/tests/bakery_chain/order.rs +++ b/tests/bakery_chain/order.rs @@ -42,6 +42,7 @@ impl PrimaryKeyTrait for PrimaryKey { pub enum Relation { Bakery, Customer, + Lineitem, } impl ColumnTrait for Column { @@ -69,6 +70,7 @@ impl RelationTrait for Relation { .from(Column::CustomerId) .to(super::customer::Column::Id) .into(), + Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(), } } } @@ -85,4 +87,16 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Lineitem.def() + } +} + +impl Model { + pub fn find_lineitems(&self) -> Select { + Entity::find_related().belongs_to::(self) + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/tests/bakery_chain_tests.rs b/tests/bakery_chain_tests.rs index 66a7c985..afcaa1fa 100644 --- a/tests/bakery_chain_tests.rs +++ b/tests/bakery_chain_tests.rs @@ -17,6 +17,7 @@ async fn setup_schema(db: &DbConn) { assert!(create_baker_table(db).await.is_ok()); assert!(create_customer_table(db).await.is_ok()); assert!(create_order_table(db).await.is_ok()); + assert!(create_lineitem_table(db).await.is_ok()); } async fn create_bakery_table(db: &DbConn) -> Result { @@ -124,3 +125,34 @@ async fn create_order_table(db: &DbConn) -> Result { db.execute(stmt.into()).await } + +async fn create_lineitem_table(db: &DbConn) -> Result { + let stmt = sea_query::Table::create() + .table(lineitem::Entity) + .if_not_exists() + .col( + ColumnDef::new(lineitem::Column::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(lineitem::Column::Price).float()) + .col(ColumnDef::new(lineitem::Column::Quantity).integer()) + .col( + ColumnDef::new(lineitem::Column::OrderId) + .integer() + .not_null(), + ) + .foreign_key( + ForeignKey::create() + .name("FK_lineitem_order") + .from(lineitem::Entity, lineitem::Column::OrderId) + .to(order::Entity, order::Column::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .build(SqliteQueryBuilder); + + db.execute(stmt.into()).await +}