Move to new design

This commit is contained in:
eraden 2023-07-26 21:49:34 +02:00
parent 87f4ee8c45
commit 29f8a421c4
8 changed files with 118 additions and 17 deletions

View File

@ -1,7 +1,7 @@
use sea_orm::{EnumIter, Iterable}; use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::create_enum; use crate::{create_enum, drop_enum};
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;
@ -65,8 +65,6 @@ impl MigrationTrait for Migration {
.if_not_exists() .if_not_exists()
.to_owned(); .to_owned();
eprintln!("{}", table.to_string(PostgresQueryBuilder::default()));
m.create_table(table).await?; m.create_table(table).await?;
Ok(()) Ok(())
@ -74,7 +72,10 @@ impl MigrationTrait for Migration {
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(Account::Accounts).to_owned()) m.drop_table(Table::drop().table(Account::Accounts).to_owned())
.await .await?;
drop_enum::<Role>(m).await?;
Ok(())
} }
} }
@ -89,9 +90,8 @@ pub enum Role {
Admin, Admin,
} }
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
enum Account { pub enum Account {
Accounts, Accounts,
Id, Id,
Login, Login,

View File

@ -1,7 +1,8 @@
use sea_orm::{EnumIter, Iterable}; use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::create_enum; use crate::{create_enum, drop_enum};
use crate::m20220101_000001_create_table::Account;
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;
@ -46,17 +47,33 @@ impl MigrationTrait for Migration {
.if_not_exists() .if_not_exists()
.to_owned(), .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> { async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(Image::Images).to_owned()) m.drop_table(Table::drop().table(Image::Images).to_owned())
.await .await?;
drop_enum::<ImageState>(m).await?;
Ok(())
} }
} }
#[derive(Iden)] #[derive(Iden)]
enum Image { pub enum Image {
Images, Images,
Id, Id,
LocalPath, LocalPath,
@ -68,7 +85,7 @@ enum Image {
} }
#[derive(Iden, EnumIter)] #[derive(Iden, EnumIter)]
enum ImageState { pub enum ImageState {
ImageState, ImageState,
Pending, Pending,
Approved, Approved,

View File

@ -1,6 +1,7 @@
use sea_orm::{EnumIter, Iterable}; use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::m20220101_000001_create_table::Account;
use crate::{create_enum, drop_enum}; use crate::{create_enum, drop_enum};
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
@ -32,6 +33,8 @@ impl MigrationTrait for Migration {
.default(ParkingSpaceState::Pending.to_string()) .default(ParkingSpaceState::Pending.to_string())
.not_null(), .not_null(),
) )
.col(ColumnDef::new(ParkingSpace::Location).string().not_null())
.col(ColumnDef::new(ParkingSpace::AccountId).integer().not_null())
.col( .col(
ColumnDef::new(ParkingSpace::CreatedAt) ColumnDef::new(ParkingSpace::CreatedAt)
.timestamp() .timestamp()
@ -49,6 +52,16 @@ impl MigrationTrait for Migration {
) )
.await?; .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( m.create_table(
Table::create() Table::create()
.table(ParkingSpaceRent::ParkingSpaceRents) .table(ParkingSpaceRent::ParkingSpaceRents)
@ -61,6 +74,17 @@ impl MigrationTrait for Migration {
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(ParkingSpaceRent::Price).integer().not_null()) .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( .col(
ColumnDef::new(ParkingSpaceRent::CreatedAt) ColumnDef::new(ParkingSpaceRent::CreatedAt)
.timestamp() .timestamp()
@ -77,6 +101,15 @@ impl MigrationTrait for Migration {
.to_owned(), .to_owned(),
) )
.await?; .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(()) Ok(())
} }
@ -99,7 +132,7 @@ impl MigrationTrait for Migration {
} }
#[derive(Iden)] #[derive(Iden)]
enum ParkingSpace { pub enum ParkingSpace {
ParkingSpaces, ParkingSpaces,
Id, Id,
State, State,
@ -110,10 +143,12 @@ enum ParkingSpace {
} }
#[derive(Iden)] #[derive(Iden)]
enum ParkingSpaceRent { pub enum ParkingSpaceRent {
ParkingSpaceRents, ParkingSpaceRents,
Id, Id,
Price, Price,
Available,
ParkingSpaceId,
CreatedAt, CreatedAt,
UpdatedAt, UpdatedAt,
} }

View File

@ -52,7 +52,10 @@ impl PrimaryKeyTrait for PrimaryKey {
} }
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {} pub enum Relation {
Images,
ParkingSpaces,
}
impl ColumnTrait for Column { impl ColumnTrait for Column {
type EntityName = Entity; type EntityName = Entity;
@ -73,7 +76,22 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { 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<super::images::Entity> for Entity {
fn to() -> RelationDef {
Relation::Images.def()
}
}
impl Related<super::parking_spaces::Entity> for Entity {
fn to() -> RelationDef {
Relation::ParkingSpaces.def()
} }
} }

View File

@ -48,7 +48,9 @@ impl PrimaryKeyTrait for PrimaryKey {
} }
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {} pub enum Relation {
Accounts,
}
impl ColumnTrait for Column { impl ColumnTrait for Column {
type EntityName = Entity; type EntityName = Entity;
@ -67,7 +69,18 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { 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<super::accounts::Entity> for Entity {
fn to() -> RelationDef {
Relation::Accounts.def()
} }
} }

View File

@ -4,4 +4,6 @@ pub mod prelude;
pub mod accounts; pub mod accounts;
pub mod images; pub mod images;
pub mod parking_space_rents;
pub mod parking_spaces;
pub mod sea_orm_active_enums; pub mod sea_orm_active_enums;

View File

@ -2,3 +2,5 @@
pub use super::accounts::Entity as Accounts; pub use super::accounts::Entity as Accounts;
pub use super::images::Entity as Images; pub use super::images::Entity as Images;
pub use super::parking_space_rents::Entity as ParkingSpaceRents;
pub use super::parking_spaces::Entity as ParkingSpaces;

View File

@ -13,6 +13,20 @@ pub enum ImageState {
Pending, Pending,
} }
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] #[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")] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "userrole")]
pub enum Userrole { pub enum Userrole {
#[sea_orm(string_value = "Admin")] #[sea_orm(string_value = "Admin")]