From 29f8a421c41ef80bb917e59b9603d598edbfeed5 Mon Sep 17 00:00:00 2001 From: eraden Date: Wed, 26 Jul 2023 21:49:34 +0200 Subject: [PATCH] Move to new design --- .../src/m20220101_000001_create_table.rs | 12 +++--- .../migration/src/m20230726_124452_images.rs | 27 ++++++++++--- .../src/m20230726_135630_parking_spaces.rs | 39 ++++++++++++++++++- crates/oswilno-contract/src/accounts.rs | 22 ++++++++++- crates/oswilno-contract/src/images.rs | 17 +++++++- crates/oswilno-contract/src/lib.rs | 2 + crates/oswilno-contract/src/prelude.rs | 2 + .../src/sea_orm_active_enums.rs | 14 +++++++ 8 files changed, 118 insertions(+), 17 deletions(-) diff --git a/crates/migration/src/m20220101_000001_create_table.rs b/crates/migration/src/m20220101_000001_create_table.rs index 02f560b..b7fb96a 100644 --- a/crates/migration/src/m20220101_000001_create_table.rs +++ b/crates/migration/src/m20220101_000001_create_table.rs @@ -1,7 +1,7 @@ use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::*; -use crate::create_enum; +use crate::{create_enum, drop_enum}; #[derive(DeriveMigrationName)] pub struct Migration; @@ -65,8 +65,6 @@ impl MigrationTrait for Migration { .if_not_exists() .to_owned(); - eprintln!("{}", table.to_string(PostgresQueryBuilder::default())); - m.create_table(table).await?; Ok(()) @@ -74,7 +72,10 @@ impl MigrationTrait for Migration { async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { m.drop_table(Table::drop().table(Account::Accounts).to_owned()) - .await + .await?; + + drop_enum::(m).await?; + Ok(()) } } @@ -89,9 +90,8 @@ pub enum Role { Admin, } -/// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] -enum Account { +pub enum Account { Accounts, Id, Login, diff --git a/crates/migration/src/m20230726_124452_images.rs b/crates/migration/src/m20230726_124452_images.rs index eca7684..0fb13fb 100644 --- a/crates/migration/src/m20230726_124452_images.rs +++ b/crates/migration/src/m20230726_124452_images.rs @@ -1,7 +1,8 @@ use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::*; -use crate::create_enum; +use crate::{create_enum, drop_enum}; +use crate::m20220101_000001_create_table::Account; #[derive(DeriveMigrationName)] pub struct Migration; @@ -46,17 +47,33 @@ impl MigrationTrait for Migration { .if_not_exists() .to_owned(), ) - .await + .await?; + + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(Image::Images) + .from_col(Image::AccountId) + .to_tbl(Account::Accounts) + .to_col(Account::Id) + .to_owned(), + ) + .await?; + + Ok(()) } async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { m.drop_table(Table::drop().table(Image::Images).to_owned()) - .await + .await?; + + drop_enum::(m).await?; + + Ok(()) } } #[derive(Iden)] -enum Image { +pub enum Image { Images, Id, LocalPath, @@ -68,7 +85,7 @@ enum Image { } #[derive(Iden, EnumIter)] -enum ImageState { +pub enum ImageState { ImageState, Pending, Approved, diff --git a/crates/migration/src/m20230726_135630_parking_spaces.rs b/crates/migration/src/m20230726_135630_parking_spaces.rs index 8aac854..fb1467b 100644 --- a/crates/migration/src/m20230726_135630_parking_spaces.rs +++ b/crates/migration/src/m20230726_135630_parking_spaces.rs @@ -1,6 +1,7 @@ use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::*; +use crate::m20220101_000001_create_table::Account; use crate::{create_enum, drop_enum}; #[derive(DeriveMigrationName)] @@ -32,6 +33,8 @@ impl MigrationTrait for Migration { .default(ParkingSpaceState::Pending.to_string()) .not_null(), ) + .col(ColumnDef::new(ParkingSpace::Location).string().not_null()) + .col(ColumnDef::new(ParkingSpace::AccountId).integer().not_null()) .col( ColumnDef::new(ParkingSpace::CreatedAt) .timestamp() @@ -49,6 +52,16 @@ impl MigrationTrait for Migration { ) .await?; + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(ParkingSpace::ParkingSpaces) + .from_col(ParkingSpace::AccountId) + .to_tbl(Account::Accounts) + .to_col(Account::Id) + .to_owned(), + ) + .await?; + m.create_table( Table::create() .table(ParkingSpaceRent::ParkingSpaceRents) @@ -61,6 +74,17 @@ impl MigrationTrait for Migration { .primary_key(), ) .col(ColumnDef::new(ParkingSpaceRent::Price).integer().not_null()) + .col( + ColumnDef::new(ParkingSpaceRent::ParkingSpaceId) + .integer() + .not_null(), + ) + .col( + ColumnDef::new(ParkingSpaceRent::Available) + .boolean() + .default(true) + .not_null(), + ) .col( ColumnDef::new(ParkingSpaceRent::CreatedAt) .timestamp() @@ -77,6 +101,15 @@ impl MigrationTrait for Migration { .to_owned(), ) .await?; + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(ParkingSpaceRent::ParkingSpaceRents) + .from_col(ParkingSpaceRent::ParkingSpaceId) + .to_tbl(ParkingSpace::ParkingSpaces) + .to_col(ParkingSpace::Id) + .to_owned(), + ) + .await?; Ok(()) } @@ -99,7 +132,7 @@ impl MigrationTrait for Migration { } #[derive(Iden)] -enum ParkingSpace { +pub enum ParkingSpace { ParkingSpaces, Id, State, @@ -110,10 +143,12 @@ enum ParkingSpace { } #[derive(Iden)] -enum ParkingSpaceRent { +pub enum ParkingSpaceRent { ParkingSpaceRents, Id, Price, + Available, + ParkingSpaceId, CreatedAt, UpdatedAt, } diff --git a/crates/oswilno-contract/src/accounts.rs b/crates/oswilno-contract/src/accounts.rs index c251815..3ca28f6 100644 --- a/crates/oswilno-contract/src/accounts.rs +++ b/crates/oswilno-contract/src/accounts.rs @@ -52,7 +52,10 @@ impl PrimaryKeyTrait for PrimaryKey { } #[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation {} +pub enum Relation { + Images, + ParkingSpaces, +} impl ColumnTrait for Column { type EntityName = Entity; @@ -73,7 +76,22 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { - panic!("No RelationDef") + match self { + Self::Images => Entity::has_many(super::images::Entity).into(), + Self::ParkingSpaces => Entity::has_many(super::parking_spaces::Entity).into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Images.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ParkingSpaces.def() } } diff --git a/crates/oswilno-contract/src/images.rs b/crates/oswilno-contract/src/images.rs index 2c3a210..397c2b4 100644 --- a/crates/oswilno-contract/src/images.rs +++ b/crates/oswilno-contract/src/images.rs @@ -48,7 +48,9 @@ impl PrimaryKeyTrait for PrimaryKey { } #[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation {} +pub enum Relation { + Accounts, +} impl ColumnTrait for Column { type EntityName = Entity; @@ -67,7 +69,18 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { - panic!("No RelationDef") + match self { + Self::Accounts => Entity::belongs_to(super::accounts::Entity) + .from(Column::AccountId) + .to(super::accounts::Column::Id) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Accounts.def() } } diff --git a/crates/oswilno-contract/src/lib.rs b/crates/oswilno-contract/src/lib.rs index 04c68ef..a25dda7 100644 --- a/crates/oswilno-contract/src/lib.rs +++ b/crates/oswilno-contract/src/lib.rs @@ -4,4 +4,6 @@ pub mod prelude; pub mod accounts; pub mod images; +pub mod parking_space_rents; +pub mod parking_spaces; pub mod sea_orm_active_enums; diff --git a/crates/oswilno-contract/src/prelude.rs b/crates/oswilno-contract/src/prelude.rs index fc6026c..1caa932 100644 --- a/crates/oswilno-contract/src/prelude.rs +++ b/crates/oswilno-contract/src/prelude.rs @@ -2,3 +2,5 @@ pub use super::accounts::Entity as Accounts; pub use super::images::Entity as Images; +pub use super::parking_space_rents::Entity as ParkingSpaceRents; +pub use super::parking_spaces::Entity as ParkingSpaces; diff --git a/crates/oswilno-contract/src/sea_orm_active_enums.rs b/crates/oswilno-contract/src/sea_orm_active_enums.rs index f5daad6..82a1e49 100644 --- a/crates/oswilno-contract/src/sea_orm_active_enums.rs +++ b/crates/oswilno-contract/src/sea_orm_active_enums.rs @@ -13,6 +13,20 @@ pub enum ImageState { Pending, } #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm( + rs_type = "String", + db_type = "Enum", + enum_name = "parking_space_state" +)] +pub enum ParkingSpaceState { + #[sea_orm(string_value = "Banned")] + Banned, + #[sea_orm(string_value = "Pending")] + Pending, + #[sea_orm(string_value = "Verified")] + Verified, +} +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "userrole")] pub enum Userrole { #[sea_orm(string_value = "Admin")]