A few changes to the trait :D

This commit is contained in:
LinlyBoi
2022-12-31 17:55:19 +02:00
parent 7fab1257a9
commit ebe885e02d
5 changed files with 14 additions and 2651 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
/target
.env
backend/target/sqlx/backend
Cargo.lock

2636
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -1,7 +1,7 @@
use crate::pog::SqlStruct;
use async_trait::async_trait;
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use sqlx::types::chrono::NaiveDate;
use sqlx::{types::Json, PgPool};
#[derive(Serialize, Deserialize, Debug, Clone, sqlx::FromRow)]
@@ -9,9 +9,7 @@ pub struct Driver {
pub id: i32,
pub name: String,
pub address: String,
#[serde_as(as = "Json<NaiveDate>")]
pub reg_date: NaiveDate,
#[serde_as(as = "Json<NaiveDate>")]
pub birthdate: NaiveDate,
}
@@ -20,20 +18,19 @@ pub struct Row {
pub person: Json<Driver>,
}
#[async_trait]
impl SqlStruct for Driver {
impl SqlStruct<i32> 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,
self.id as i32,
self.name,
self.address,
self.reg_date,
self.birthdate,
)
.fetch_one(pool)
.await?;

View File

@@ -1,10 +1,9 @@
use anyhow::Result;
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);
pub trait SqlStruct<T> {
async fn add(&self, pool: &PgPool) -> Result<T>;
}