This commit is contained in:
LinlyBoi
2022-12-31 12:43:55 +02:00
parent a4ac6f474b
commit 8ebea56925
6 changed files with 76 additions and 45 deletions

View File

@@ -14,6 +14,6 @@ anyhow = "1.0"
futures = "0.3" futures = "0.3"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
sqlx = {features = ["postgres", "json", "runtime-actix-rustls", "chrono", "decimal", "uuid"] } sqlx = {version = "0.6", features = ["postgres", "json", "runtime-actix-rustls", "chrono", "decimal", "uuid"] }
async-trait = {} async-trait = "0.1.60"
tokio = { version = "1.20.0", features = ["macros"]} actix-web = "4"

View File

@@ -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();

View File

@@ -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;

View File

@@ -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<Driver>,
}
#[async_trait]
impl SqlStruct for Driver {
async fn add(&self, pool: &PgPool) -> anyhow::Result<i32> {
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)
}
}

27
backend/src/main.rs Normal file
View File

@@ -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(())
}

10
backend/src/pog.rs Normal file
View File

@@ -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);
}