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 /target
.env .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" futures = "0.3"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" 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" async-trait = "0.1.60"
actix-web = "4" actix-web = "4"
chrono = {features = ["serde"]}

View File

@@ -1,7 +1,7 @@
use crate::pog::SqlStruct; use crate::pog::SqlStruct;
use async_trait::async_trait; use async_trait::async_trait;
use chrono::NaiveDate;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::types::chrono::NaiveDate;
use sqlx::{types::Json, PgPool}; use sqlx::{types::Json, PgPool};
#[derive(Serialize, Deserialize, Debug, Clone, sqlx::FromRow)] #[derive(Serialize, Deserialize, Debug, Clone, sqlx::FromRow)]
@@ -9,9 +9,7 @@ pub struct Driver {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub address: String, pub address: String,
#[serde_as(as = "Json<NaiveDate>")]
pub reg_date: NaiveDate, pub reg_date: NaiveDate,
#[serde_as(as = "Json<NaiveDate>")]
pub birthdate: NaiveDate, pub birthdate: NaiveDate,
} }
@@ -20,20 +18,19 @@ pub struct Row {
pub person: Json<Driver>, pub person: Json<Driver>,
} }
#[async_trait] #[async_trait]
impl SqlStruct for Driver { impl SqlStruct<i32> for Driver {
async fn add(&self, pool: &PgPool) -> anyhow::Result<i32> { async fn add(&self, pool: &PgPool) -> anyhow::Result<i32> {
let driver = self.clone();
let rec = sqlx::query!( let rec = sqlx::query!(
r#" r#"
INSERT INTO drivers ( id, name, address, reg_date, birthdate ) INSERT INTO drivers ( id, name, address, reg_date, birthdate )
VALUES ( $1, $2, $3, $4, $5 ) VALUES ( $1, $2, $3, $4, $5 )
RETURNING id RETURNING id
"#, "#,
driver.id as i32, self.id as i32,
driver.name, self.name,
driver.address, self.address,
driver.reg_date, self.reg_date,
driver.birthdate, self.birthdate,
) )
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;

View File

@@ -1,10 +1,9 @@
use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use sqlx::PgPool; use sqlx::PgPool;
//Nuke happened here but we got POOLS :DDDDDD //Nuke happened here but we got POOLS :DDDDDD
#[async_trait] #[async_trait]
pub trait SqlStruct { pub trait SqlStruct<T> {
async fn add(&self, pool: &PgPool); async fn add(&self, pool: &PgPool) -> Result<T>;
async fn delete(&self, pool: &PgPool);
async fn listall(&self, pool: &PgPool);
} }