From 8ebea56925861fcf470d8285a80888723eab8753 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 31 Dec 2022 12:43:55 +0200 Subject: [PATCH] YES --- backend/Cargo.toml | 6 ++-- .../down.sql | 6 ---- .../up.sql | 36 ------------------- backend/src/driver_data.rs | 36 +++++++++++++++++++ backend/src/main.rs | 27 ++++++++++++++ backend/src/pog.rs | 10 ++++++ 6 files changed, 76 insertions(+), 45 deletions(-) delete mode 100644 backend/migrations/00000000000000_diesel_initial_setup/down.sql delete mode 100644 backend/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 backend/src/main.rs create mode 100644 backend/src/pog.rs diff --git a/backend/Cargo.toml b/backend/Cargo.toml index e6442f2..b0fea81 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -14,6 +14,6 @@ anyhow = "1.0" futures = "0.3" serde = { version = "1", features = ["derive"] } serde_json = "1" -sqlx = {features = ["postgres", "json", "runtime-actix-rustls", "chrono", "decimal", "uuid"] } -async-trait = {} -tokio = { version = "1.20.0", features = ["macros"]} +sqlx = {version = "0.6", features = ["postgres", "json", "runtime-actix-rustls", "chrono", "decimal", "uuid"] } +async-trait = "0.1.60" +actix-web = "4" diff --git a/backend/migrations/00000000000000_diesel_initial_setup/down.sql b/backend/migrations/00000000000000_diesel_initial_setup/down.sql deleted file mode 100644 index a9f5260..0000000 --- a/backend/migrations/00000000000000_diesel_initial_setup/down.sql +++ /dev/null @@ -1,6 +0,0 @@ --- This file was automatically created by Diesel to setup helper functions --- and other internal bookkeeping. This file is safe to edit, any future --- changes will be added to existing projects as new migrations. - -DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); -DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/backend/migrations/00000000000000_diesel_initial_setup/up.sql b/backend/migrations/00000000000000_diesel_initial_setup/up.sql deleted file mode 100644 index d68895b..0000000 --- a/backend/migrations/00000000000000_diesel_initial_setup/up.sql +++ /dev/null @@ -1,36 +0,0 @@ --- This file was automatically created by Diesel to setup helper functions --- and other internal bookkeeping. This file is safe to edit, any future --- changes will be added to existing projects as new migrations. - - - - --- Sets up a trigger for the given table to automatically set a column called --- `updated_at` whenever the row is modified (unless `updated_at` was included --- in the modified columns) --- --- # Example --- --- ```sql --- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); --- --- SELECT diesel_manage_updated_at('users'); --- ``` -CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ -BEGIN - EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s - FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ -BEGIN - IF ( - NEW IS DISTINCT FROM OLD AND - NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at - ) THEN - NEW.updated_at := current_timestamp; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; diff --git a/backend/src/driver_data.rs b/backend/src/driver_data.rs index 139597f..0623d73 100644 --- a/backend/src/driver_data.rs +++ b/backend/src/driver_data.rs @@ -1,2 +1,38 @@ +use crate::pog::SqlStruct; +use async_trait::async_trait; +use chrono::NaiveDate; +use sqlx::{types::Json, PgPool}; +pub struct Driver { + pub id: i32, + pub name: String, + pub address: String, + pub reg_date: NaiveDate, + pub birthdate: NaiveDate, +} +pub struct Row { + pub id: i32, + pub person: Json, +} +#[async_trait] +impl SqlStruct for Driver { + async fn add(&self, pool: &PgPool) -> anyhow::Result { + let driver = self.clone(); + let rec = sqlx::query!( + r#" + INSERT INTO drivers ( id, name, address, reg_date, birthdate ) + VALUES ( $1, $2, $3, $4, $5 ) + RETURNING id + "#, + driver.id as i32, + driver.name, + driver.address, + driver.reg_date, + driver.birthdate, + ) + .fetch_one(pool) + .await?; + Ok(rec.id) + } +} diff --git a/backend/src/main.rs b/backend/src/main.rs new file mode 100644 index 0000000..e8d1455 --- /dev/null +++ b/backend/src/main.rs @@ -0,0 +1,27 @@ +use dotenv::dotenv; +use sqlx::postgres::PgPoolOptions; +use std::env; + +pub mod admin_data; +pub mod driver_data; +pub mod pog; +pub mod radar_data; +pub mod ticket_data; +pub mod vehicle_data; + +#[actix_web::main] +async fn main() -> Result<(), sqlx::Error> { + dotenv().ok(); + let database_url = env::var("DATABASE_URL").expect("Put a DB url in the .env file dumbass"); + let pool = PgPoolOptions::new() + .max_connections(10) + .connect(database_url.as_str()) + .await?; + let row: (i64,) = sqlx::query_as("SELECT $1 FROM drivers") + .bind(150_i64) + .fetch_one(&pool) + .await?; + assert_eq!(row.0, 150); + println!("my nuts {}", row.0); + Ok(()) +} diff --git a/backend/src/pog.rs b/backend/src/pog.rs new file mode 100644 index 0000000..b24089f --- /dev/null +++ b/backend/src/pog.rs @@ -0,0 +1,10 @@ +use async_trait::async_trait; +use sqlx::PgPool; + +//Nuke happened here but we got POOLS :DDDDDD +#[async_trait] +pub trait SqlStruct { + async fn add(&self, pool: &PgPool); + async fn delete(&self, pool: &PgPool); + async fn listall(&self, pool: &PgPool); +}