Encode/decode InteriorRef struct from jsonb column
This commit is contained in:
parent
29eadabc8a
commit
fb99afdc26
@ -16,8 +16,7 @@ listenfd = "0.3"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
tokio = { version = "0.2", features = ["macros"] }
|
tokio = { version = "0.2", features = ["macros"] }
|
||||||
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono",
|
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono", "uuid", "ipnetwork", "json" ] }
|
||||||
"uuid", "ipnetwork", "json" ] }
|
|
||||||
warp = { version = "0.2", features = ["compression"] }
|
warp = { version = "0.2", features = ["compression"] }
|
||||||
refinery = { version = "0.3.0", features = [ "tokio-postgres", "tokio" ] }
|
refinery = { version = "0.3.0", features = [ "tokio-postgres", "tokio" ] }
|
||||||
barrel = { version = "0.6.5", features = [ "pg" ] }
|
barrel = { version = "0.6.5", features = [ "pg" ] }
|
||||||
|
@ -3,15 +3,34 @@ use async_trait::async_trait;
|
|||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
use sqlx::types::Json;
|
||||||
|
|
||||||
use super::ListParams;
|
use super::ListParams;
|
||||||
use super::Model;
|
use super::Model;
|
||||||
|
|
||||||
|
// sqlx queries for this model need to be `query_as_unchecked!` because `query_as!` does not
|
||||||
|
// support user-defined types (`ref_list` Json field).
|
||||||
|
// See for more info: https://github.com/thallada/rust_sqlx_bug/blob/master/src/main.rs
|
||||||
|
// This may be fixed in sqlx 0.4.
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct InteriorRef {
|
||||||
|
pub mod_name: String,
|
||||||
|
pub local_form_id: i32,
|
||||||
|
pub position_x: f64,
|
||||||
|
pub position_y: f64,
|
||||||
|
pub position_z: f64,
|
||||||
|
pub angle_x: f64,
|
||||||
|
pub angle_y: f64,
|
||||||
|
pub angle_z: f64,
|
||||||
|
pub scale: f64,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct InteriorRefList {
|
pub struct InteriorRefList {
|
||||||
pub id: Option<i32>,
|
pub id: Option<i32>,
|
||||||
pub shop_id: i32,
|
pub shop_id: i32,
|
||||||
pub ref_list: serde_json::value::Value,
|
pub ref_list: Json<Vec<InteriorRef>>,
|
||||||
pub created_at: Option<NaiveDateTime>,
|
pub created_at: Option<NaiveDateTime>,
|
||||||
pub updated_at: Option<NaiveDateTime>,
|
pub updated_at: Option<NaiveDateTime>,
|
||||||
}
|
}
|
||||||
@ -28,7 +47,8 @@ impl Model for InteriorRefList {
|
|||||||
|
|
||||||
async fn get(db: &PgPool, id: i32) -> Result<Self> {
|
async fn get(db: &PgPool, id: i32) -> Result<Self> {
|
||||||
let timer = std::time::Instant::now();
|
let timer = std::time::Instant::now();
|
||||||
let result = sqlx::query_as!(Self, "SELECT * FROM interior_ref_lists WHERE id = $1", id)
|
let result =
|
||||||
|
sqlx::query_as_unchecked!(Self, "SELECT * FROM interior_ref_lists WHERE id = $1", id)
|
||||||
.fetch_one(db)
|
.fetch_one(db)
|
||||||
.await?;
|
.await?;
|
||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
@ -41,7 +61,8 @@ impl Model for InteriorRefList {
|
|||||||
// TODO:
|
// TODO:
|
||||||
// * Decide if I'll need to make the same changes to merchandise and transactions
|
// * Decide if I'll need to make the same changes to merchandise and transactions
|
||||||
// - answer depends on how many rows of each I expect to insert in one go
|
// - answer depends on how many rows of each I expect to insert in one go
|
||||||
let result = sqlx::query_as!(
|
// * should probably omit ref_list from response
|
||||||
|
let result = sqlx::query_as_unchecked!(
|
||||||
Self,
|
Self,
|
||||||
"INSERT INTO interior_ref_lists
|
"INSERT INTO interior_ref_lists
|
||||||
(shop_id, ref_list, created_at, updated_at)
|
(shop_id, ref_list, created_at, updated_at)
|
||||||
@ -60,7 +81,7 @@ impl Model for InteriorRefList {
|
|||||||
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
|
||||||
let timer = std::time::Instant::now();
|
let timer = std::time::Instant::now();
|
||||||
let result = if let Some(order_by) = list_params.get_order_by() {
|
let result = if let Some(order_by) = list_params.get_order_by() {
|
||||||
sqlx::query_as!(
|
sqlx::query_as_unchecked!(
|
||||||
Self,
|
Self,
|
||||||
"SELECT * FROM interior_ref_lists
|
"SELECT * FROM interior_ref_lists
|
||||||
ORDER BY $1
|
ORDER BY $1
|
||||||
@ -73,7 +94,7 @@ impl Model for InteriorRefList {
|
|||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
.await?
|
.await?
|
||||||
} else {
|
} else {
|
||||||
sqlx::query_as!(
|
sqlx::query_as_unchecked!(
|
||||||
Self,
|
Self,
|
||||||
"SELECT * FROM interior_ref_lists
|
"SELECT * FROM interior_ref_lists
|
||||||
LIMIT $1
|
LIMIT $1
|
||||||
|
Loading…
Reference in New Issue
Block a user