diff --git a/crates/migration/src/m20230919_162830_create_rent_requests.rs b/crates/migration/src/m20230919_162830_create_rent_requests.rs new file mode 100644 index 0000000..18a0547 --- /dev/null +++ b/crates/migration/src/m20230919_162830_create_rent_requests.rs @@ -0,0 +1,92 @@ +use sea_orm_migration::prelude::*; + +use crate::m20220101_000001_create_table::Account; +use crate::m20230726_135630_parking_spaces::ParkingSpace; +use crate::m20230726_135630_parking_spaces::ParkingSpaceRent; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(RentRequest::RentRequests) + .if_not_exists() + .col( + ColumnDef::new(RentRequest::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(RentRequest::ParkingSpaceId).integer()) + .col(ColumnDef::new(RentRequest::ParkingSpaceRentId).integer()) + .col(ColumnDef::new(RentRequest::AccountId).integer()) + .col( + ColumnDef::new(RentRequest::CreatedAt) + .timestamp() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .col( + ColumnDef::new(RentRequest::UpdatedAt) + .timestamp() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .if_not_exists() + .to_owned(), + ) + .await?; + + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(RentRequest::RentRequests) + .from_col(RentRequest::ParkingSpaceId) + .to_tbl(ParkingSpace::ParkingSpaces) + .to_col(ParkingSpace::Id) + .to_owned(), + ) + .await?; + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(RentRequest::RentRequests) + .from_col(RentRequest::ParkingSpaceId) + .to_tbl(ParkingSpaceRent::ParkingSpaceRents) + .to_col(ParkingSpaceRent::Id) + .to_owned(), + ) + .await?; + m.create_foreign_key( + ForeignKeyCreateStatement::new() + .from_tbl(RentRequest::RentRequests) + .from_col(RentRequest::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(RentRequest::RentRequests).to_owned()) + .await?; + + Ok(()) + } +} + +#[derive(Iden)] +pub enum RentRequest { + RentRequests, + Id, + ParkingSpaceId, + ParkingSpaceRentId, + AccountId, + CreatedAt, + UpdatedAt, +} diff --git a/crates/oswilno-contract/src/rent_requests.rs b/crates/oswilno-contract/src/rent_requests.rs new file mode 100644 index 0000000..a802fd5 --- /dev/null +++ b/crates/oswilno-contract/src/rent_requests.rs @@ -0,0 +1,123 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.2 + +use actix_admin::prelude::*; +use sea_orm::entity::prelude::*; +#[allow(unused_imports)] +use sea_orm::Iterable; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl ActixAdminModelValidationTrait for Entity {} +impl ActixAdminModelFilterTrait for Entity {} +impl EntityName for Entity { + fn table_name(&self) -> &str { + "rent_requests" + } +} + +#[derive( + Clone, + Debug, + PartialEq, + DeriveModel, + DeriveActiveModel, + Eq, + DeriveActixAdmin, + DeriveActixAdminModel, + DeriveActixAdminViewModel, +)] +pub struct Model { + #[actix_admin(primary_key)] + pub id: i32, + #[actix_admin(select_list=crate::parking_spaces::Entity)] + pub parking_space_id: Option, + pub parking_space_rent_id: Option, + pub account_id: Option, + #[actix_admin(list_hide_column, column_type = "NaiveDateTime")] + pub created_at: DateTime, + #[actix_admin(list_hide_column, column_type = "NaiveDateTime")] + pub updated_at: DateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + ParkingSpaceId, + ParkingSpaceRentId, + AccountId, + CreatedAt, + UpdatedAt, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Accounts, + ParkingSpaceRents, + ParkingSpaces, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::ParkingSpaceId => ColumnType::Integer.def().null(), + Self::ParkingSpaceRentId => ColumnType::Integer.def().null(), + Self::AccountId => ColumnType::Integer.def().null(), + Self::CreatedAt => ColumnType::DateTime.def(), + Self::UpdatedAt => ColumnType::DateTime.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Accounts => Entity::belongs_to(super::accounts::Entity) + .from(Column::AccountId) + .to(super::accounts::Column::Id) + .into(), + Self::ParkingSpaceRents => Entity::belongs_to(super::parking_space_rents::Entity) + .from(Column::ParkingSpaceId) + .to(super::parking_space_rents::Column::Id) + .into(), + Self::ParkingSpaces => Entity::belongs_to(super::parking_spaces::Entity) + .from(Column::ParkingSpaceId) + .to(super::parking_spaces::Column::Id) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Accounts.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ParkingSpaceRents.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::ParkingSpaces.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}