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

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