diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index f4d23d8..4a81461 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -6,6 +6,7 @@ mod m20220101_000001_create_table; mod m20230726_124452_images; mod m20230726_135630_parking_spaces; mod m20230805_000001_add_email; +mod m20230809_135630_add_spot; pub struct Migrator; @@ -17,6 +18,7 @@ impl MigratorTrait for Migrator { Box::new(m20230726_124452_images::Migration), Box::new(m20230726_135630_parking_spaces::Migration), Box::new(m20230805_000001_add_email::Migration), + Box::new(m20230809_135630_add_spot::Migration), ] } } diff --git a/crates/migration/src/m20230726_135630_parking_spaces.rs b/crates/migration/src/m20230726_135630_parking_spaces.rs index fb1467b..1fd0207 100644 --- a/crates/migration/src/m20230726_135630_parking_spaces.rs +++ b/crates/migration/src/m20230726_135630_parking_spaces.rs @@ -137,6 +137,7 @@ pub enum ParkingSpace { Id, State, Location, + Spot, AccountId, CreatedAt, UpdatedAt, diff --git a/crates/migration/src/m20230809_135630_add_spot.rs b/crates/migration/src/m20230809_135630_add_spot.rs new file mode 100644 index 0000000..db67ae6 --- /dev/null +++ b/crates/migration/src/m20230809_135630_add_spot.rs @@ -0,0 +1,167 @@ +use sea_orm_migration::prelude::*; + +use crate::m20220101_000001_create_table::Account; +use crate::{create_enum, drop_enum}; + +#[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(ParkingSpace::ParkingSpaces) + .if_not_exists() + .col( + ColumnDef::new(ParkingSpace::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(ParkingSpace::State) + .enumeration( + ParkingSpaceState::ParkingSpaceState, + ParkingSpaceState::iter().skip(1), + ) + .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() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .col( + ColumnDef::new(ParkingSpace::UpdatedAt) + .timestamp() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .if_not_exists() + .to_owned(), + ) + .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) + .if_not_exists() + .col( + ColumnDef::new(ParkingSpaceRent::Id) + .integer() + .not_null() + .auto_increment() + .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() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .col( + ColumnDef::new(ParkingSpaceRent::UpdatedAt) + .timestamp() + .default(SimpleExpr::Custom("NOW()".to_owned())) + .not_null(), + ) + .if_not_exists() + .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(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + m.alter_table( + Table::alter_table() + .table(ParkingSpaceRent::ParkingSpaceRents) + .to_owned(), + ) + .await?; + + Ok(()) + } +} + +#[derive(Iden)] +pub enum ParkingSpace { + ParkingSpaces, + Id, + State, + Location, + AccountId, + CreatedAt, + UpdatedAt, +} + +#[derive(Iden)] +pub enum ParkingSpaceRent { + ParkingSpaceRents, + Id, + Price, + Available, + ParkingSpaceId, + CreatedAt, + UpdatedAt, +} + +#[derive(Iden, EnumIter)] +pub enum ParkingSpaceState { + ParkingSpaceState, + #[iden(rename = "Pending")] + Pending, + #[iden(rename = "Verified")] + Verified, + #[iden(rename = "Banned")] + Banned, +} + +#[derive(Iden, EnumIter)] +pub enum Side { + Side, + #[iden(rename = "Left")] + Left, + #[iden(rename = "Right")] + Right, + #[iden(rename = "Front")] + Front, +} diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index 3849062..86ff2fc 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -1,5 +1,5 @@ -use actix_web::get; -use actix_web::web::{scope, Data, ServiceConfig}; +use actix_web::web::{scope, Data, ServiceConfig, Form}; +use actix_web::{get, post, HttpResponse}; use askama_actix::Template; use oswilno_contract::accounts; use oswilno_contract::parking_space_rents; @@ -100,3 +100,15 @@ async fn load_parking_spaces(db: Arc) -> AllParkingSpace { parking_space_by_id, } } + +#[derive(Debug,serde::Deserialize)] +struct CreateParkingSpace { + location: String, + spot: Option, +} + +#[post("/parking-spaces")] +async fn create(db: Data, p: Form) -> HttpResponse { + let p = p.into_inner(); + unreachable!() +}