interior_refs -> interior_ref_lists, finish insert
This commit is contained in:
@@ -2,25 +2,24 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use chrono::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
use super::ListParams;
|
||||
use super::Model;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct InteriorRef {
|
||||
pub struct InteriorRefList {
|
||||
pub id: Option<i32>,
|
||||
pub shop_id: i32,
|
||||
pub references: serde_json::value::Value,
|
||||
pub ref_list: serde_json::value::Value,
|
||||
pub created_at: Option<NaiveDateTime>,
|
||||
pub updated_at: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Model for InteriorRef {
|
||||
impl Model for InteriorRefList {
|
||||
fn resource_name() -> &'static str {
|
||||
"interior_ref"
|
||||
"interior_ref_list"
|
||||
}
|
||||
|
||||
fn pk(&self) -> Option<i32> {
|
||||
@@ -29,32 +28,32 @@ impl Model for InteriorRef {
|
||||
|
||||
async fn get(db: &PgPool, id: i32) -> Result<Self> {
|
||||
let timer = std::time::Instant::now();
|
||||
let result = sqlx::query_as!(Self, "SELECT * FROM interior_refs WHERE id = $1", id)
|
||||
let result = sqlx::query_as!(Self, "SELECT * FROM interior_ref_lists WHERE id = $1", id)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
let elapsed = timer.elapsed();
|
||||
debug!("SELECT * FROM interior_refs ... {:.3?}", elapsed);
|
||||
debug!("SELECT * FROM interior_ref_lists ... {:.3?}", elapsed);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||
let timer = std::time::Instant::now();
|
||||
// TODO:
|
||||
// * Actually save the references list to the jsonb column
|
||||
// * 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
|
||||
let result = sqlx::query_as!(
|
||||
Self,
|
||||
"INSERT INTO interior_refs
|
||||
(shop_id, created_at, updated_at)
|
||||
VALUES ($1, now(), now())
|
||||
"INSERT INTO interior_ref_lists
|
||||
(shop_id, ref_list, created_at, updated_at)
|
||||
VALUES ($1, $2, now(), now())
|
||||
RETURNING *",
|
||||
self.shop_id,
|
||||
self.ref_list,
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
let elapsed = timer.elapsed();
|
||||
debug!("INSERT INTO interior_refs ... {:.3?}", elapsed);
|
||||
debug!("INSERT INTO interior_ref_lists ... {:.3?}", elapsed);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -63,7 +62,7 @@ impl Model for InteriorRef {
|
||||
let result = if let Some(order_by) = list_params.get_order_by() {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"SELECT * FROM interior_refs
|
||||
"SELECT * FROM interior_ref_lists
|
||||
ORDER BY $1
|
||||
LIMIT $2
|
||||
OFFSET $3",
|
||||
@@ -76,7 +75,7 @@ impl Model for InteriorRef {
|
||||
} else {
|
||||
sqlx::query_as!(
|
||||
Self,
|
||||
"SELECT * FROM interior_refs
|
||||
"SELECT * FROM interior_ref_lists
|
||||
LIMIT $1
|
||||
OFFSET $2",
|
||||
list_params.limit.unwrap_or(10),
|
||||
@@ -86,7 +85,7 @@ impl Model for InteriorRef {
|
||||
.await?
|
||||
};
|
||||
let elapsed = timer.elapsed();
|
||||
debug!("SELECT * FROM interior_refs ... {:.3?}", elapsed);
|
||||
debug!("SELECT * FROM interior_ref_lists ... {:.3?}", elapsed);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
use serde::Deserialize;
|
||||
use std::fmt;
|
||||
|
||||
pub mod interior_ref_list;
|
||||
pub mod model;
|
||||
pub mod owner;
|
||||
pub mod shop;
|
||||
pub mod interior_ref;
|
||||
|
||||
pub use interior_ref_list::InteriorRefList;
|
||||
pub use model::Model;
|
||||
pub use owner::Owner;
|
||||
pub use shop::Shop;
|
||||
pub use interior_ref::InteriorRef;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub enum Order {
|
||||
|
||||
@@ -2,7 +2,6 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use chrono::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
use super::ListParams;
|
||||
|
||||
Reference in New Issue
Block a user