A few changes to the trait :D
This commit is contained in:
@@ -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"]}
|
||||
|
||||
@@ -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?;
|
||||
|
||||
@@ -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>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user