YES
This commit is contained in:
@@ -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
27
backend/src/main.rs
Normal 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
10
backend/src/pog.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user