Add merchandise_lists endpoint
This commit is contained in:
parent
8d5fe7f75d
commit
fc057e86dd
@ -15,9 +15,11 @@ pub struct Caches {
|
|||||||
pub shop: Cache<i32, CachedResponse>,
|
pub shop: Cache<i32, CachedResponse>,
|
||||||
pub owner: Cache<i32, CachedResponse>,
|
pub owner: Cache<i32, CachedResponse>,
|
||||||
pub interior_ref_list: Cache<i32, CachedResponse>,
|
pub interior_ref_list: Cache<i32, CachedResponse>,
|
||||||
|
pub merchandise_list: Cache<i32, CachedResponse>,
|
||||||
pub list_shops: Cache<ListParams, CachedResponse>,
|
pub list_shops: Cache<ListParams, CachedResponse>,
|
||||||
pub list_owners: Cache<ListParams, CachedResponse>,
|
pub list_owners: Cache<ListParams, CachedResponse>,
|
||||||
pub list_interior_ref_lists: Cache<ListParams, CachedResponse>,
|
pub list_interior_ref_lists: Cache<ListParams, CachedResponse>,
|
||||||
|
pub list_merchandise_lists: Cache<ListParams, CachedResponse>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Caches {
|
impl Caches {
|
||||||
@ -27,9 +29,11 @@ impl Caches {
|
|||||||
shop: Cache::new("shop", 100),
|
shop: Cache::new("shop", 100),
|
||||||
owner: Cache::new("owner", 100),
|
owner: Cache::new("owner", 100),
|
||||||
interior_ref_list: Cache::new("interior_ref_list", 100),
|
interior_ref_list: Cache::new("interior_ref_list", 100),
|
||||||
|
merchandise_list: Cache::new("merchandise_list", 100),
|
||||||
list_shops: Cache::new("list_shops", 100),
|
list_shops: Cache::new("list_shops", 100),
|
||||||
list_owners: Cache::new("list_owners", 100),
|
list_owners: Cache::new("list_owners", 100),
|
||||||
list_interior_ref_lists: Cache::new("list_interior_ref_lists", 100),
|
list_interior_ref_lists: Cache::new("list_interior_ref_lists", 100),
|
||||||
|
list_merchandise_lists: Cache::new("list_merchandise_lists", 100),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,37 +35,30 @@ pub fn migration() -> String {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
m.create_table("merchandise", |t| {
|
m.create_table("merchandise_lists", |t| {
|
||||||
t.add_column("id", types::primary().indexed(true));
|
t.add_column("id", types::primary().indexed(true));
|
||||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||||
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
||||||
t.add_column("mod_name", types::varchar(255));
|
t.add_column("form_list", types::custom("jsonb"));
|
||||||
t.add_column("local_form_id", types::integer());
|
|
||||||
t.add_column("quantity", types::integer());
|
|
||||||
t.add_column("created_at", types::custom("timestamp(3)"));
|
t.add_column("created_at", types::custom("timestamp(3)"));
|
||||||
t.add_column("updated_at", types::custom("timestamp(3)"));
|
t.add_column("updated_at", types::custom("timestamp(3)"));
|
||||||
t.add_index(
|
|
||||||
"merchandise_unique_mod_shop_id_name_and_local_form_id",
|
|
||||||
types::index(vec!["shop_id", "mod_name", "local_form_id"]).unique(true),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
m.create_table("transactions", |t| {
|
// m.create_table("transactions", |t| {
|
||||||
t.add_column("id", types::primary().indexed(true));
|
// t.add_column("id", types::primary().indexed(true));
|
||||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
// t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||||
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
// t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
||||||
t.add_column("merchandise_id", types::foreign("merchandise", "id"));
|
// t.add_column("merchandise_list_id", types::foreign("merchandise_lists", "id"));
|
||||||
t.add_column("customer_name", types::varchar(255));
|
// t.add_column("customer_name", types::varchar(255));
|
||||||
t.add_column("is_customer_npc", types::boolean());
|
// t.add_column("is_customer_npc", types::boolean());
|
||||||
t.add_column("is_customer_buying", types::boolean());
|
// t.add_column("is_customer_buying", types::boolean());
|
||||||
t.add_column("quantity", types::integer());
|
// t.add_column("quantity", types::integer());
|
||||||
t.add_column("is_void", types::boolean());
|
// t.add_column("is_void", types::boolean());
|
||||||
t.add_column("created_at", types::custom("timestamp(3)"));
|
// t.add_column("created_at", types::custom("timestamp(3)"));
|
||||||
});
|
// });
|
||||||
|
|
||||||
m.create_table("interior_ref_lists", |t| {
|
m.create_table("interior_ref_lists", |t| {
|
||||||
t.add_column("id", types::primary().indexed(true));
|
t.add_column("id", types::primary().indexed(true));
|
||||||
// TODO make shop_id unique, recover unique_violation
|
|
||||||
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
t.add_column("shop_id", types::foreign("shops", "id").indexed(true));
|
||||||
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
|
||||||
t.add_column("ref_list", types::custom("jsonb"));
|
t.add_column("ref_list", types::custom("jsonb"));
|
||||||
|
@ -4,7 +4,7 @@ use std::convert::Infallible;
|
|||||||
use warp::{Filter, Rejection, Reply};
|
use warp::{Filter, Rejection, Reply};
|
||||||
|
|
||||||
use super::handlers;
|
use super::handlers;
|
||||||
use super::models::{InteriorRefList, ListParams, Owner, Shop};
|
use super::models::{InteriorRefList, ListParams, MerchandiseList, Owner, Shop};
|
||||||
use super::Environment;
|
use super::Environment;
|
||||||
|
|
||||||
pub fn status() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
pub fn status() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
@ -42,6 +42,17 @@ pub fn interior_ref_lists(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn merchandise_lists(
|
||||||
|
env: Environment,
|
||||||
|
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path("merchandise_lists").and(
|
||||||
|
get_merchandise_list(env.clone())
|
||||||
|
.or(delete_merchandise_list(env.clone()))
|
||||||
|
.or(create_merchandise_list(env.clone()))
|
||||||
|
.or(list_merchandise_lists(env)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
pub fn get_shop(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
warp::path::param()
|
warp::path::param()
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
@ -160,6 +171,46 @@ pub fn list_interior_ref_lists(
|
|||||||
.and_then(handlers::list_interior_ref_lists)
|
.and_then(handlers::list_interior_ref_lists)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_merchandise_list(
|
||||||
|
env: Environment,
|
||||||
|
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path::param()
|
||||||
|
.and(warp::get())
|
||||||
|
.and(with_env(env))
|
||||||
|
.and_then(handlers::get_merchandise_list)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_merchandise_list(
|
||||||
|
env: Environment,
|
||||||
|
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path::end()
|
||||||
|
.and(warp::post())
|
||||||
|
.and(json_body::<MerchandiseList>())
|
||||||
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(with_env(env))
|
||||||
|
.and_then(handlers::create_merchandise_list)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_merchandise_list(
|
||||||
|
env: Environment,
|
||||||
|
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path::param()
|
||||||
|
.and(warp::delete())
|
||||||
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(with_env(env))
|
||||||
|
.and_then(handlers::delete_merchandise_list)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_merchandise_lists(
|
||||||
|
env: Environment,
|
||||||
|
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
|
||||||
|
warp::path::end()
|
||||||
|
.and(warp::get())
|
||||||
|
.and(warp::query::<ListParams>())
|
||||||
|
.and(with_env(env))
|
||||||
|
.and_then(handlers::list_merchandise_lists)
|
||||||
|
}
|
||||||
|
|
||||||
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
|
fn with_env(env: Environment) -> impl Filter<Extract = (Environment,), Error = Infallible> + Clone {
|
||||||
warp::any().map(move || env.clone())
|
warp::any().map(move || env.clone())
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{json, with_header, with_status};
|
use warp::reply::{json, with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
use super::models::{InteriorRefList, ListParams, Model, Owner, Shop};
|
use super::models::{InteriorRefList, ListParams, Model, Owner, Shop, MerchandiseList};
|
||||||
use super::problem::{reject_anyhow, unauthorized_no_api_key, unauthorized_no_owner};
|
use super::problem::{reject_anyhow, unauthorized_no_api_key, unauthorized_no_owner};
|
||||||
use super::Environment;
|
use super::Environment;
|
||||||
|
|
||||||
@ -257,3 +257,73 @@ pub async fn delete_interior_ref_list(
|
|||||||
env.caches.list_interior_ref_lists.clear().await;
|
env.caches.list_interior_ref_lists.clear().await;
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: probably need a way to get by shop id instead
|
||||||
|
pub async fn get_merchandise_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
|
env.caches
|
||||||
|
.merchandise_list
|
||||||
|
.get_response(id, || async {
|
||||||
|
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
|
||||||
|
let reply = json(&merchandise_list);
|
||||||
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
|
Ok(reply)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_merchandise_lists(
|
||||||
|
list_params: ListParams,
|
||||||
|
env: Environment,
|
||||||
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
env.caches
|
||||||
|
.list_merchandise_lists
|
||||||
|
.get_response(list_params.clone(), || async {
|
||||||
|
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
|
||||||
|
let reply = json(&merchandise_lists);
|
||||||
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
|
Ok(reply)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_merchandise_list(
|
||||||
|
merchandise_list: MerchandiseList,
|
||||||
|
api_key: Option<Uuid>,
|
||||||
|
env: Environment,
|
||||||
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
|
let ref_list_with_owner_id = MerchandiseList {
|
||||||
|
owner_id: Some(owner_id),
|
||||||
|
..merchandise_list
|
||||||
|
};
|
||||||
|
let saved_merchandise_list = ref_list_with_owner_id
|
||||||
|
.save(&env.db)
|
||||||
|
.await
|
||||||
|
.map_err(reject_anyhow)?;
|
||||||
|
let url = saved_merchandise_list
|
||||||
|
.url(&env.api_url)
|
||||||
|
.map_err(reject_anyhow)?;
|
||||||
|
let reply = json(&saved_merchandise_list);
|
||||||
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
|
env.caches.list_merchandise_lists.clear().await;
|
||||||
|
Ok(reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_merchandise_list(
|
||||||
|
id: i32,
|
||||||
|
api_key: Option<Uuid>,
|
||||||
|
env: Environment,
|
||||||
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
|
MerchandiseList::delete(&env.db, owner_id, id)
|
||||||
|
.await
|
||||||
|
.map_err(reject_anyhow)?;
|
||||||
|
env.caches
|
||||||
|
.merchandise_list
|
||||||
|
.delete_response(id)
|
||||||
|
.await
|
||||||
|
.map_err(reject_anyhow)?;
|
||||||
|
env.caches.list_merchandise_lists.clear().await;
|
||||||
|
Ok(StatusCode::NO_CONTENT)
|
||||||
|
}
|
||||||
|
@ -81,7 +81,8 @@ async fn main() -> Result<()> {
|
|||||||
.or(base.and(
|
.or(base.and(
|
||||||
filters::shops(env.clone())
|
filters::shops(env.clone())
|
||||||
.or(filters::owners(env.clone()))
|
.or(filters::owners(env.clone()))
|
||||||
.or(filters::interior_ref_lists(env.clone())),
|
.or(filters::interior_ref_lists(env.clone()))
|
||||||
|
.or(filters::merchandise_lists(env.clone())),
|
||||||
))
|
))
|
||||||
.recover(problem::unpack_problem)
|
.recover(problem::unpack_problem)
|
||||||
.with(warp::compression::gzip())
|
.with(warp::compression::gzip())
|
||||||
|
119
src/models/merchandise_list.rs
Normal file
119
src/models/merchandise_list.rs
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
use anyhow::{Error, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use chrono::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::postgres::PgPool;
|
||||||
|
use sqlx::types::Json;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
|
use super::ListParams;
|
||||||
|
use super::Model;
|
||||||
|
use crate::problem::forbidden_permission;
|
||||||
|
|
||||||
|
// sqlx queries for this model need to be `query_as_unchecked!` because `query_as!` does not
|
||||||
|
// support user-defined types (`form_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 Merchandise {
|
||||||
|
pub mod_name: String,
|
||||||
|
pub local_form_id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub quantity: i32,
|
||||||
|
pub form_type: i32,
|
||||||
|
pub is_food: bool,
|
||||||
|
pub price: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
pub struct MerchandiseList {
|
||||||
|
pub id: Option<i32>,
|
||||||
|
pub shop_id: i32,
|
||||||
|
pub owner_id: Option<i32>,
|
||||||
|
pub form_list: Json<Vec<Merchandise>>,
|
||||||
|
pub created_at: Option<NaiveDateTime>,
|
||||||
|
pub updated_at: Option<NaiveDateTime>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Model for MerchandiseList {
|
||||||
|
fn resource_name() -> &'static str {
|
||||||
|
"merchandise_list"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pk(&self) -> Option<i32> {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(db))]
|
||||||
|
async fn get(db: &PgPool, id: i32) -> Result<Self> {
|
||||||
|
sqlx::query_as_unchecked!(Self, "SELECT * FROM merchandise_lists WHERE id = $1", id)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await
|
||||||
|
.map_err(Error::new)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(self, db))]
|
||||||
|
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||||
|
Ok(sqlx::query_as_unchecked!(
|
||||||
|
Self,
|
||||||
|
"INSERT INTO merchandise_lists
|
||||||
|
(shop_id, owner_id, form_list, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, now(), now())
|
||||||
|
RETURNING *",
|
||||||
|
self.shop_id,
|
||||||
|
self.owner_id,
|
||||||
|
self.form_list,
|
||||||
|
)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(db))]
|
||||||
|
async fn delete(db: &PgPool, owner_id: i32, id: i32) -> Result<u64> {
|
||||||
|
let merchandise_list =
|
||||||
|
sqlx::query!("SELECT owner_id FROM merchandise_lists WHERE id = $1", id)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await?;
|
||||||
|
if merchandise_list.owner_id == owner_id {
|
||||||
|
return Ok(
|
||||||
|
sqlx::query!("DELETE FROM merchandise_lists WHERE id = $1", id)
|
||||||
|
.execute(db)
|
||||||
|
.await?,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return Err(forbidden_permission());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "debug", skip(db))]
|
||||||
|
async fn list(db: &PgPool, list_params: &ListParams) -> Result<Vec<Self>> {
|
||||||
|
let result = if let Some(order_by) = list_params.get_order_by() {
|
||||||
|
sqlx::query_as_unchecked!(
|
||||||
|
Self,
|
||||||
|
"SELECT * FROM merchandise_lists
|
||||||
|
ORDER BY $1
|
||||||
|
LIMIT $2
|
||||||
|
OFFSET $3",
|
||||||
|
order_by,
|
||||||
|
list_params.limit.unwrap_or(10),
|
||||||
|
list_params.offset.unwrap_or(0),
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
sqlx::query_as_unchecked!(
|
||||||
|
Self,
|
||||||
|
"SELECT * FROM merchandise_lists
|
||||||
|
LIMIT $1
|
||||||
|
OFFSET $2",
|
||||||
|
list_params.limit.unwrap_or(10),
|
||||||
|
list_params.offset.unwrap_or(0),
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ use std::fmt;
|
|||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
pub mod interior_ref_list;
|
pub mod interior_ref_list;
|
||||||
|
pub mod merchandise_list;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod owner;
|
pub mod owner;
|
||||||
pub mod shop;
|
pub mod shop;
|
||||||
@ -11,6 +12,7 @@ pub use interior_ref_list::InteriorRefList;
|
|||||||
pub use model::Model;
|
pub use model::Model;
|
||||||
pub use owner::Owner;
|
pub use owner::Owner;
|
||||||
pub use shop::Shop;
|
pub use shop::Shop;
|
||||||
|
pub use merchandise_list::MerchandiseList;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Hash, Clone, Deserialize)]
|
#[derive(Debug, Eq, PartialEq, Hash, Clone, Deserialize)]
|
||||||
pub enum Order {
|
pub enum Order {
|
||||||
|
42
test_data/merchandise.json
Normal file
42
test_data/merchandise.json
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"shop_id": 1,
|
||||||
|
"owner_id": 1,
|
||||||
|
"form_list": [
|
||||||
|
{
|
||||||
|
"mod_name": "Skyrim.esm",
|
||||||
|
"local_form_id": 1,
|
||||||
|
"name": "Misc Item",
|
||||||
|
"quantity": 1,
|
||||||
|
"form_type": 32,
|
||||||
|
"is_food": false,
|
||||||
|
"price": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mod_name": "Skyrim.esm",
|
||||||
|
"local_form_id": 2,
|
||||||
|
"name": "Scroll",
|
||||||
|
"quantity": 2,
|
||||||
|
"form_type": 23,
|
||||||
|
"is_food": false,
|
||||||
|
"price": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mod_name": "Skyrim.esm",
|
||||||
|
"local_form_id": 3,
|
||||||
|
"name": "Alchemy Item",
|
||||||
|
"quantity": 3,
|
||||||
|
"form_type": 46,
|
||||||
|
"is_food": true,
|
||||||
|
"price": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mod_name": "Skyrim.esm",
|
||||||
|
"local_form_id": 4,
|
||||||
|
"name": "Weapon",
|
||||||
|
"quantity": 4,
|
||||||
|
"form_type": 41,
|
||||||
|
"is_food": false,
|
||||||
|
"price": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user