Move to new design

This commit is contained in:
eraden 2023-07-26 11:16:29 +02:00
parent 62dc015b99
commit d6cd82bf8e
15 changed files with 488 additions and 0 deletions

View File

@ -0,0 +1,18 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "migration"
path = "src/lib.rs"
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
tokio = { version = "1.29.1", features = ["full"] }
oswilno-contract = { path = "../oswilno-contract" }
[dependencies.sea-orm-migration]
version = "0.11.0"
features = ["sqlx-postgres", "runtime-actix-rustls", "sea-orm-cli"]

View File

@ -0,0 +1,41 @@
# Running Migrator CLI
- Generate a new migration file
```sh
cargo run -- migrate generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```

View File

@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
}
}

View File

@ -0,0 +1,88 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.create_enum_from_entity(oswilno_contract::Role)?;
manager
.create_table(
Table::create()
.table(Account::Accounts)
.if_not_exists()
.col(
ColumnDef::new(Account::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Account::Login).string().not_null())
.col(ColumnDef::new(Account::PassHash).string().not_null())
.col(ColumnDef::new(Account::Role).enumeration().not_null())
.col(
ColumnDef::new(Account::Banned)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Account::Confirmed)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Account::Verified)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(Account::CreatedAt)
.timestamp()
.default()
.not_null(),
)
.col(
ColumnDef::new(Account::UpdatedAt)
.timestamp()
.default()
.not_null(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Account::Accounts).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Role {
Role,
User,
Admin,
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Account {
Accounts,
Id,
Login,
PassHash,
Role,
Banned,
Confirmed,
Verified,
CreatedAt,
UpdatedAt,
}

View File

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}

View File

@ -0,0 +1,13 @@
[package]
name = "oswilno-admin"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-admin = "0.5.0"
actix-web = "4.3.1"
actix-web-grants = "3.0.2"
askama = "0.12.0"
chrono = "0.4.26"
tera = "1.19.0"
uuid = { version = "1.4.1", features = ["v4"] }

View File

@ -0,0 +1,25 @@
use actix_admin::prelude::*;
use actix_web::web::{Data, ServiceConfig};
pub fn mount(config: &mut ServiceConfig) {
let actix_admin_builder = create_actix_admin_builder();
config
.app_data(Data::new(actix_admin_builder.get_actix_admin()))
.service(actix_admin_builder.get_scope());
}
fn create_actix_admin_builder() -> ActixAdminBuilder {
let configuration = ActixAdminConfiguration {
enable_auth: false,
user_is_logged_in: None,
login_link: None,
logout_link: None,
file_upload_directory: "./file_uploads",
navbar_title: "oswilno - admin",
};
let mut admin_builder = ActixAdminBuilder::new(configuration);
admin_builder
}

View File

@ -0,0 +1,10 @@
[package]
name = "oswilno-config"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.175", features = ["derive"] }
serde_json = "1.0.103"
toml = "0.7.6"

View File

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
bind: Option<String>,
port: Option<u16>,
}

View File

@ -0,0 +1,15 @@
[package]
name = "oswilno-contract"
version = "0.1.0"
edition = "2021"
[dependencies]
actix = "0.13.0"
actix-admin = "0.5.0"
actix-rt = { version = "2.8.0", features = [] }
chrono = "0.4.26"
regex = "1.9.1"
sea-orm = { version = "0.11.3", features = ["postgres-array", "runtime-actix-rustls", "sqlx-postgres", "macros", "sqlx"] }
serde = { version = "1.0.175", features = ["derive"] }
tera = "1.19.0"
uuid = { version = "1.4.1", features = ["v4"] }

View File

@ -0,0 +1,197 @@
use std::str::FromStr;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Default, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "roles")]
pub enum Role {
#[default]
#[sea_orm(string_value = "User")]
User = 0,
#[sea_orm(string_value = "Admin")]
Admin = 255,
}
impl FromStr for Role {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"Admin" => Self::Admin,
_ => Self::User,
})
}
}
#[derive(
Debug, Clone, Default, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize,
)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "sides")]
pub enum Side {
#[default]
#[sea_orm(string_value = "Left")]
Left,
#[sea_orm(string_value = "Right")]
Right,
#[sea_orm(string_value = "Front")]
Front,
}
pub mod image {
use actix_admin::prelude::*;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(
Clone,
Debug,
PartialEq,
DeriveEntityModel,
Deserialize,
Serialize,
DeriveActixAdmin,
DeriveActixAdminModel,
DeriveActixAdminViewModel,
)]
#[sea_orm(table_name = "images")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub account_id: i32,
pub inner_path: String,
pub public_path: String,
pub banned: bool,
pub verified: bool,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::account::Entity",
from = "Column::AccountId",
to = "super::account::Column::Id"
)]
Account,
}
// Custom Validation Functions
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
// Custom Search Filters
impl ActixAdminModelFilterTrait<Entity> for Entity {}
}
pub mod account {
use actix_admin::prelude::*;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(
Clone,
Debug,
PartialEq,
DeriveEntityModel,
Deserialize,
Serialize,
DeriveActixAdmin,
DeriveActixAdminModel,
DeriveActixAdminViewModel,
)]
#[sea_orm(table_name = "accounts")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub login: String,
pub pass_hash: String,
pub role: super::Role,
pub banned: bool,
pub confirmed: bool,
pub verified: bool,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Image,
ParkingSpace,
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Image => Relation::Image.def(),
Self::ParkingSpace => Relation::ParkingSpace.def(),
}
}
}
// Custom Validation Functions
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
// Custom Search Filters
impl ActixAdminModelFilterTrait<Entity> for Entity {}
}
pub mod parking_space {
use actix_admin::prelude::*;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(
Clone,
Debug,
PartialEq,
DeriveEntityModel,
Deserialize,
Serialize,
DeriveActixAdmin,
DeriveActixAdminModel,
DeriveActixAdminViewModel,
)]
#[sea_orm(table_name = "parking_spaces")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub account_id: i32,
/// stage, street etc
pub address: String,
pub neighbours: Vec<super::Side>,
pub columns: Vec<super::Side>,
pub price: Option<i32>,
pub available: bool,
pub verified: bool,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::account::Entity",
from = "Column::AccountId",
to = "super::account::Column::Id"
)]
Account,
}
// Custom Validation Functions
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
// Custom Search Filters
impl ActixAdminModelFilterTrait<Entity> for Entity {}
}

View File

@ -0,0 +1,11 @@
[package]
name = "oswilno-parking-space"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix = "0.13.0"
actix-web = "4.3.1"
askama = { version = "0.12.0", features = ["serde", "with-actix-web", "comrak", "mime"] }
tokio = { version = "1.29.1", features = ["full"] }

View File

@ -0,0 +1,3 @@
use actix_web::web::ServiceConfig;
pub fn mount(config: &mut ServiceConfig) {}

View File

@ -0,0 +1,24 @@
[package]
name = "oswilno"
version = "0.1.0"
edition = "2021"
[dependencies]
actix = "0.13.0"
actix-rt = { version = "2.8.0", features = [] }
actix-web = "4.3.1"
actix-web-grants = "3.0.2"
askama = { version = "0.12.0", features = ["serde", "with-actix-web", "comrak", "mime"] }
futures = { version = "0.3.28", features = ["futures-executor"] }
oswilno-config = { path = "../oswilno-config" }
oswilno-parking-space = { path = "../oswilno-parking-space" }
oswilno-admin = { path = "../oswilno-admin" }
sea-orm = { version = "0.11.3", features = ["postgres-array", "runtime-actix-rustls", "sqlx-postgres"] }
serde = { version = "1.0.175", features = ["derive"] }
serde_json = "1.0.103"
tokio = { version = "1.29.1", features = ["full"] }
toml = "0.7.6"
[dev-dependencies]
insta = { version = "1.31.0", features = ["ron"] }
ron = "0.8.0"

View File

@ -0,0 +1,18 @@
use actix_web::{web, App, HttpServer, Responder};
async fn index() -> impl Responder {
"Hello world!"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(web::scope("/app").route("/index.html", web::get().to(index)))
.configure(oswilno_parking_space::mount)
.configure(oswilno_admin::mount)
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}