interior_refs -> interior_ref_lists, finish insert
This commit is contained in:
parent
0275143559
commit
29eadabc8a
@ -20,7 +20,7 @@ pub fn migration() -> String {
|
||||
m.create_table("shops", |t| {
|
||||
t.add_column("id", types::primary().indexed(true));
|
||||
t.add_column("name", types::varchar(255));
|
||||
t.add_column("owner_id", types::foreign("owners", "id"));
|
||||
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
||||
t.add_column("description", types::text().nullable(true));
|
||||
t.add_column("is_not_sell_buy", types::boolean().default(true));
|
||||
t.add_column("sell_buy_list_id", types::integer().default(0));
|
||||
@ -36,7 +36,7 @@ pub fn migration() -> String {
|
||||
|
||||
m.create_table("merchandise", |t| {
|
||||
t.add_column("id", types::primary().indexed(true));
|
||||
t.add_column("shop_id", types::foreign("shops", "id"));
|
||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||
t.add_column("mod_name", types::varchar(255));
|
||||
t.add_column("local_form_id", types::integer());
|
||||
t.add_column("quantity", types::integer());
|
||||
@ -50,7 +50,7 @@ pub fn migration() -> String {
|
||||
|
||||
m.create_table("transactions", |t| {
|
||||
t.add_column("id", types::primary().indexed(true));
|
||||
t.add_column("shop_id", types::foreign("shops", "id"));
|
||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||
t.add_column("merchandise_id", types::foreign("merchandise", "id"));
|
||||
t.add_column("customer_name", types::varchar(255));
|
||||
t.add_column("is_customer_npc", types::boolean());
|
||||
@ -60,12 +60,10 @@ pub fn migration() -> String {
|
||||
t.add_column("created_at", types::custom("timestamp(3)"));
|
||||
});
|
||||
|
||||
// TODO: rename to interior_reflist or similar?
|
||||
// TODO: index shop_id col
|
||||
m.create_table("interior_refs", |t| {
|
||||
m.create_table("interior_ref_lists", |t| {
|
||||
t.add_column("id", types::primary().indexed(true));
|
||||
t.add_column("shop_id", types::foreign("shops", "id"));
|
||||
t.add_column("references", types::custom("jsonb"));
|
||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||
t.add_column("ref_list", types::custom("jsonb"));
|
||||
t.add_column("created_at", types::custom("timestamp(3)"));
|
||||
t.add_column("updated_at", types::custom("timestamp(3)"));
|
||||
});
|
||||
|
@ -3,7 +3,7 @@ use std::convert::Infallible;
|
||||
use warp::{Filter, Rejection, Reply};
|
||||
|
||||
use super::handlers;
|
||||
use super::models::{InteriorRef, ListParams, Owner, Shop};
|
||||
use super::models::{InteriorRefList, ListParams, Owner, Shop};
|
||||
use super::Environment;
|
||||
|
||||
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
@ -61,43 +61,43 @@ pub fn list_owners(
|
||||
.and_then(handlers::list_owners)
|
||||
}
|
||||
|
||||
pub fn get_interior_ref(
|
||||
pub fn get_interior_ref_list(
|
||||
env: Environment,
|
||||
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
warp::path!("interior_refs" / i32)
|
||||
warp::path!("interior_ref_lists" / i32)
|
||||
.and(warp::get())
|
||||
.and(with_env(env))
|
||||
.and_then(handlers::get_interior_ref)
|
||||
.and_then(handlers::get_interior_ref_list)
|
||||
}
|
||||
|
||||
pub fn create_interior_ref(
|
||||
pub fn create_interior_ref_list(
|
||||
env: Environment,
|
||||
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
warp::path!("interior_refs")
|
||||
warp::path!("interior_ref_lists")
|
||||
.and(warp::post())
|
||||
.and(json_body::<InteriorRef>())
|
||||
.and(json_body::<InteriorRefList>())
|
||||
.and(with_env(env))
|
||||
.and_then(handlers::create_interior_ref)
|
||||
.and_then(handlers::create_interior_ref_list)
|
||||
}
|
||||
|
||||
pub fn list_interior_refs(
|
||||
pub fn list_interior_ref_lists(
|
||||
env: Environment,
|
||||
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
warp::path!("interior_refs")
|
||||
warp::path!("interior_ref_lists")
|
||||
.and(warp::get())
|
||||
.and(warp::query::<ListParams>())
|
||||
.and(with_env(env))
|
||||
.and_then(handlers::list_interior_refs)
|
||||
.and_then(handlers::list_interior_ref_lists)
|
||||
}
|
||||
|
||||
pub fn bulk_create_interior_refs(
|
||||
pub fn bulk_create_interior_ref_lists(
|
||||
env: Environment,
|
||||
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||
warp::path!("interior_refs" / "bulk")
|
||||
warp::path!("interior_ref_lists" / "bulk")
|
||||
.and(warp::post())
|
||||
.and(json_body::<Vec<InteriorRef>>())
|
||||
.and(json_body::<Vec<InteriorRefList>>())
|
||||
.and(with_env(env))
|
||||
.and_then(handlers::bulk_create_interior_refs)
|
||||
.and_then(handlers::bulk_create_interior_ref_lists)
|
||||
}
|
||||
|
||||
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
|
||||
|
@ -4,7 +4,7 @@ use warp::http::StatusCode;
|
||||
use warp::reply::{json, with_header, with_status};
|
||||
use warp::{Rejection, Reply};
|
||||
|
||||
use super::models::{InteriorRef, ListParams, Model, Owner, Shop};
|
||||
use super::models::{InteriorRefList, ListParams, Model, Owner, Shop};
|
||||
use super::problem::reject_anyhow;
|
||||
use super::Environment;
|
||||
|
||||
@ -75,44 +75,49 @@ pub async fn create_owner(
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
pub async fn get_interior_ref(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
|
||||
let interior_ref = InteriorRef::get(&env.db, id).await.map_err(reject_anyhow)?;
|
||||
let reply = json(&interior_ref);
|
||||
pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
|
||||
let interior_ref_list = InteriorRefList::get(&env.db, id)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let reply = json(&interior_ref_list);
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
pub async fn list_interior_refs(
|
||||
pub async fn list_interior_ref_lists(
|
||||
list_params: ListParams,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let interior_refs = InteriorRef::list(&env.db, list_params)
|
||||
let interior_ref_lists = InteriorRefList::list(&env.db, list_params)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let reply = json(&interior_refs);
|
||||
let reply = json(&interior_ref_lists);
|
||||
let reply = with_status(reply, StatusCode::OK);
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
pub async fn create_interior_ref(
|
||||
interior_ref: InteriorRef,
|
||||
pub async fn create_interior_ref_list(
|
||||
interior_ref_list: InteriorRefList,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
let saved_interior_ref = interior_ref.save(&env.db).await.map_err(reject_anyhow)?;
|
||||
let url = saved_interior_ref
|
||||
let saved_interior_ref_list = interior_ref_list
|
||||
.save(&env.db)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
let url = saved_interior_ref_list
|
||||
.url(&env.api_url)
|
||||
.map_err(reject_anyhow)?;
|
||||
let reply = json(&saved_interior_ref);
|
||||
let reply = json(&saved_interior_ref_list);
|
||||
let reply = with_header(reply, "Location", url.as_str());
|
||||
let reply = with_status(reply, StatusCode::CREATED);
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
pub async fn bulk_create_interior_refs(
|
||||
interior_refs: Vec<InteriorRef>,
|
||||
pub async fn bulk_create_interior_ref_lists(
|
||||
interior_ref_lists: Vec<InteriorRefList>,
|
||||
env: Environment,
|
||||
) -> Result<impl Reply, Rejection> {
|
||||
InteriorRef::bulk_save(&env.db, interior_refs)
|
||||
InteriorRefList::bulk_save(&env.db, interior_ref_lists)
|
||||
.await
|
||||
.map_err(reject_anyhow)?;
|
||||
Ok(StatusCode::CREATED)
|
||||
|
16
src/main.rs
16
src/main.rs
@ -77,10 +77,10 @@ async fn main() -> Result<()> {
|
||||
let get_owner = filters::get_owner(env.clone());
|
||||
let create_owner = filters::create_owner(env.clone());
|
||||
let list_owners = filters::list_owners(env.clone());
|
||||
let get_interior_ref = filters::get_interior_ref(env.clone());
|
||||
let create_interior_ref = filters::create_interior_ref(env.clone());
|
||||
let list_interior_refs = filters::list_interior_refs(env.clone());
|
||||
let bulk_create_interior_refs = filters::bulk_create_interior_refs(env.clone());
|
||||
let get_interior_ref_list = filters::get_interior_ref_list(env.clone());
|
||||
let create_interior_ref_list = filters::create_interior_ref_list(env.clone());
|
||||
let list_interior_ref_lists = filters::list_interior_ref_lists(env.clone());
|
||||
let bulk_create_interior_ref_lists = filters::bulk_create_interior_ref_lists(env.clone());
|
||||
let routes = base
|
||||
.and(
|
||||
create_shop
|
||||
@ -89,10 +89,10 @@ async fn main() -> Result<()> {
|
||||
.or(create_owner)
|
||||
.or(get_owner)
|
||||
.or(list_owners)
|
||||
.or(create_interior_ref)
|
||||
.or(get_interior_ref)
|
||||
.or(list_interior_refs)
|
||||
.or(bulk_create_interior_refs),
|
||||
.or(create_interior_ref_list)
|
||||
.or(get_interior_ref_list)
|
||||
.or(list_interior_ref_lists)
|
||||
.or(bulk_create_interior_ref_lists),
|
||||
)
|
||||
.recover(problem::unpack_problem)
|
||||
.with(warp::compression::gzip())
|
||||
|
@ -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;
|
||||
|
2205
test_data/interior_ref_lists.json
Normal file
2205
test_data/interior_ref_lists.json
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -5,6 +5,5 @@
|
||||
"is_not_sell_buy": true,
|
||||
"sell_buy_list_id": 1,
|
||||
"vendor_id": 1,
|
||||
"vendor_gold": 0,
|
||||
"interior_refs": []
|
||||
"vendor_gold": 0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user