diff --git a/migrations/01_init_db.sql b/migrations/01_init_db.sql new file mode 100644 index 0000000..a6e9d5d --- /dev/null +++ b/migrations/01_init_db.sql @@ -0,0 +1,70 @@ +CREATE TABLE company ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + street VARCHAR, + house_number VARCHAR, + zip_code VARCHAR, + city VARCHAR +); + +CREATE TABLE "user" ( + id SERIAL PRIMARY KEY, + login VARCHAR NOT NULL, + password VARCHAR NOT NULL, + full_name VARCHAR, + email VARCHAR, + admin bool, + get_emails bool +); + +CREATE TABLE property ( + id SERIAL PRIMARY KEY, + name VARCHAR NOT NULL, + description VARCHAR, + price NUMERIC(9, 2) NOT NULL +); + +CREATE TYPE message_type AS ENUM ('NewReservation', 'NewReservationCust', 'ReservationApp', 'ReservationCanceled'); + +CREATE TABLE message ( + id SERIAL PRIMARY KEY, + msg_type message_type NOT NULL , + subject VARCHAR NOT NULL , + "text" TEXT NOT NULL +); + +CREATE TABLE opening_hour ( + id SERIAL PRIMARY KEY, + day INTEGER NOT NULL , + "from" TIME NOT NULL , + "to" TIME NOT NULL, + discount INTEGER +); + +CREATE TABLE customer ( + id BIGSERIAL PRIMARY KEY, + full_name VARCHAR NOT NULL, + email VARCHAR NOT NULL, + phone VARCHAR, + discount INTEGER +); + +CREATE TYPE reservation_state AS ENUM ('New', 'Approved', 'Canceled'); + +CREATE TABLE reservation_sum ( + id BIGSERIAL PRIMARY KEY, + uuid uuid NOT NULL, + date DATE NOT NULL, + customer BIGINT REFERENCES customer(id) NOT NULL, + price NUMERIC(9, 2) NOT NULL, + state reservation_state +); + + +CREATE TABLE reservation ( + id BIGSERIAL PRIMARY KEY, + "from" TIME NOT NULL, + "to" TIME NOT NULL, + property INTEGER REFERENCES property(id) NOT NULL, + summary BIGINT REFERENCES reservation_sum(id) NOT NULL +); diff --git a/src/backend/data.rs b/src/backend/data.rs new file mode 100644 index 0000000..c39a26d --- /dev/null +++ b/src/backend/data.rs @@ -0,0 +1,82 @@ +use chrono::{NaiveDate, NaiveTime, Weekday}; +use rust_decimal::Decimal; +use uuid::Uuid; + +pub struct Company { + id: u16, + name: String, + street: String, + house_number: String, + zip_code: String, + city: String, +} + +pub struct User { + id: u16, + login: String, + password: String, + full_name: String, + email: String, + admin: bool, + get_emails: bool, +} + +pub struct Property { + id: u16, + name: String, + description: String, + price: Decimal +} + +pub enum MessageType { + NewReservation, + NewReservationCust, + ReservationApp, + ReservationCanceled, +} + +pub struct Message { + id: u16, + msg_type: MessageType, + subject: String, + text: String, +} + +pub struct OpeningHour { + id: u16, + day: Weekday, + from: NaiveTime, + to: NaiveTime, + discount: u8 +} + +pub struct Customer { + id: u128, + full_name: String, + email: String, + phone: String, + discount: u8 +} + +pub enum ReservationState { + New, + Approved, + Canceled, +} + +pub struct Reservation { + id: u128, + from: NaiveTime, + to: NaiveTime, + property: Property, +} + +pub struct ReservationSum { + id: u128, + uuid: Uuid, + date: NaiveDate, + items: Vec, + customer: Customer, + price: Decimal, + state: ReservationState, +} \ No newline at end of file diff --git a/src/backend/mod.rs b/src/backend/mod.rs new file mode 100644 index 0000000..07ff7c0 --- /dev/null +++ b/src/backend/mod.rs @@ -0,0 +1,26 @@ +use cfg_if::cfg_if; + +pub mod data; + +cfg_if!{ + if #[cfg(feature = "ssr")] { + use sqlx::PgPool; + + #[derive(Clone)] + pub struct AppData { + db_pool: PgPool + } + + impl AppData { + pub fn new(db_pool: PgPool) -> Self { + Self { + db_pool + } + } + + pub fn db_pool(&self) -> &PgPool { + &self.db_pool + } + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 0d1eb83..4bb1993 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod modal_box; pub mod home_page; pub mod server_fn; pub mod locales; +pub mod backend; use cfg_if::cfg_if; diff --git a/src/main.rs b/src/main.rs index 14c47f7..9f0ea5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use sqlx::migrate; +use sqlx::postgres::PgPoolOptions; + #[cfg(feature = "ssr")] #[actix_web::main] async fn main() -> std::io::Result<()> { @@ -6,10 +9,11 @@ async fn main() -> std::io::Result<()> { use leptos::*; use leptos_actix::{generate_route_list, LeptosRoutes}; use rezervator::app::*; - //use rezervator::server_fn::*; + use rezervator::backend::*; use actix_session::storage::CookieSessionStore; use actix_session::SessionMiddleware; use actix_web::cookie::Key; + use actix_web::web::Data; let conf = get_configuration(None).await.unwrap(); let addr = conf.leptos_options.site_addr; @@ -17,14 +21,18 @@ async fn main() -> std::io::Result<()> { let routes = generate_route_list(|cx| view! { cx, }); let key = Key::generate(); - //SetSession::register(); - //GetSession::register(); + let pool = PgPoolOptions::new() + .max_connections(10) + .connect("postgres://pepa:mkoijn@localhost/rezervator").await.unwrap(); + + migrate!().run(&pool).await.expect("could not run SQLx migrations"); HttpServer::new(move || { let leptos_options = &conf.leptos_options; let site_root = &leptos_options.site_root; App::new() + .app_data(Data::new(AppData::new(pool.clone()))) .wrap(SessionMiddleware::new( CookieSessionStore::default(), key.clone() diff --git a/src/server_fn.rs b/src/server_fn.rs index 6dd7502..677ab88 100644 --- a/src/server_fn.rs +++ b/src/server_fn.rs @@ -4,6 +4,8 @@ use leptos::*; pub async fn set_session(cx: Scope) -> Result<(), ServerFnError> { use leptos_actix::extract; use actix_session::*; + use actix_web::web::Data; + //use crate::DataPok; extract(cx, |session: Session| async move { leptos::log!("extract");