Add spot to parking space

This commit is contained in:
Adrian Woźniak 2023-08-09 14:56:46 +02:00
parent 76090d4266
commit c31776e46d
4 changed files with 184 additions and 2 deletions

View File

@ -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),
]
}
}

View File

@ -137,6 +137,7 @@ pub enum ParkingSpace {
Id,
State,
Location,
Spot,
AccountId,
CreatedAt,
UpdatedAt,

View File

@ -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,
}

View File

@ -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<DatabaseConnection>) -> AllParkingSpace {
parking_space_by_id,
}
}
#[derive(Debug,serde::Deserialize)]
struct CreateParkingSpace {
location: String,
spot: Option<u32>,
}
#[post("/parking-spaces")]
async fn create(db: Data<sea_orm::DatabaseConnection>, p: Form<CreateParkingSpace>) -> HttpResponse {
let p = p.into_inner();
unreachable!()
}