New transactions endpoint, split up handlers file

This commit is contained in:
Tyler Hallada 2020-10-30 00:22:26 -04:00
parent fb5c78ac4f
commit c87c35021e
12 changed files with 982 additions and 650 deletions

View File

@ -16,10 +16,13 @@ pub struct Caches {
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 merchandise_list: Cache<i32, CachedResponse>,
pub transaction: 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>, pub list_merchandise_lists: Cache<ListParams, CachedResponse>,
pub list_transactions: Cache<ListParams, CachedResponse>,
pub list_transactions_by_shop_id: Cache<(i32, ListParams), CachedResponse>,
pub interior_ref_list_by_shop_id: Cache<i32, CachedResponse>, pub interior_ref_list_by_shop_id: Cache<i32, CachedResponse>,
pub merchandise_list_by_shop_id: Cache<i32, CachedResponse>, pub merchandise_list_by_shop_id: Cache<i32, CachedResponse>,
} }
@ -32,10 +35,13 @@ impl Caches {
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), merchandise_list: Cache::new("merchandise_list", 100),
transaction: Cache::new("transaction", 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), list_merchandise_lists: Cache::new("list_merchandise_lists", 100),
list_transactions: Cache::new("list_transaction", 100),
list_transactions_by_shop_id: Cache::new("list_transaction_by_shop_id", 100),
interior_ref_list_by_shop_id: Cache::new("interior_ref_list_by_shop_id", 100), interior_ref_list_by_shop_id: Cache::new("interior_ref_list_by_shop_id", 100),
merchandise_list_by_shop_id: Cache::new("merchandise_list_by_shop_id", 100), merchandise_list_by_shop_id: Cache::new("merchandise_list_by_shop_id", 100),
} }

View File

@ -34,3 +34,15 @@ CREATE TABLE "merchandise_lists" (
"created_at" timestamp(3) NOT NULL, "created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL "updated_at" timestamp(3) NOT NULL
); );
CREATE TABLE "transactions" (
"id" SERIAL PRIMARY KEY NOT NULL,
"shop_id" INTEGER REFERENCES "shops"(id) NOT NULL UNIQUE,
"owner_id" INTEGER REFERENCES "owners"(id) NOT NULL,
"mod_name" VARCHAR(260) NOT NULL,
"local_form_id" INTEGER NOT NULL,
"is_sell" BOOLEAN NOT NULL,
"quantity" INTEGER NOT NULL,
"amount" INTEGER NOT NULL,
"created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL
);

View File

@ -0,0 +1,171 @@
use anyhow::Result;
use http::StatusCode;
use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use crate::models::{InteriorRefList, ListParams, Model, UpdateableModel};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::authenticate;
pub async fn get(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.interior_ref_list
.get_response(id, || async {
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply = json(&interior_ref_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn get_by_shop_id(shop_id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.interior_ref_list_by_shop_id
.get_response(shop_id, || async {
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
let reply = json(&interior_ref_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list(list_params: ListParams, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.list_interior_ref_lists
.get_response(list_params.clone(), || async {
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply = json(&interior_ref_lists);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create(
interior_ref_list: InteriorRefList,
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 = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&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_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_interior_ref_lists.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
Ok(reply)
}
pub async fn update(
id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.interior_ref_list.delete_response(id).await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
env.caches.list_interior_ref_lists.clear().await;
Ok(reply)
}
pub async fn update_by_shop_id(
shop_id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.interior_ref_list
.delete_response(
updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id"),
)
.await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
env.caches.list_interior_ref_lists.clear().await;
Ok(reply)
}
pub async fn delete(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list = InteriorRefList::get(&env.db, id)
.await
.map_err(reject_anyhow)?;
InteriorRefList::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.interior_ref_list.delete_response(id).await;
env.caches.list_interior_ref_lists.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(interior_ref_list.shop_id)
.await;
Ok(StatusCode::NO_CONTENT)
}

View File

@ -0,0 +1,210 @@
use anyhow::Result;
use http::StatusCode;
use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use crate::models::{ListParams, MerchandiseList, MerchandiseParams, Model, UpdateableModel};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::authenticate;
pub async fn get(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 get_by_shop_id(shop_id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.merchandise_list_by_shop_id
.get_response(shop_id, || async {
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = json(&merchandise_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list(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: 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
.create(&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;
env.caches
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
Ok(reply)
}
pub async fn update(
id: i32,
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 merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.merchandise_list.delete_response(id).await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(reply)
}
pub async fn update_by_shop_id(
shop_id: i32,
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 merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.merchandise_list
.delete_response(
updated_merchandise_list
.id
.expect("saved merchandise_list has no id"),
)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(reply)
}
pub async fn delete(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list = MerchandiseList::get(&env.db, id)
.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;
env.caches
.merchandise_list_by_shop_id
.delete_response(merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(StatusCode::NO_CONTENT)
}
pub async fn buy_merchandise(
shop_id: i32,
merchandise_params: MerchandiseParams,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let _owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
// TODO: create transaction
let updated_merchandise_list = MerchandiseList::update_merchandise_quantity(
&env.db,
shop_id,
&(merchandise_params.mod_name),
merchandise_params.local_form_id,
merchandise_params.quantity_delta,
)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.merchandise_list
.delete_response(
updated_merchandise_list
.id
.expect("saved merchandise_list has no id"),
)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(reply)
}

View File

@ -1,18 +1,14 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use http::StatusCode;
use ipnetwork::IpNetwork;
use sqlx::types::Json;
use std::net::SocketAddr;
use tracing::instrument; use tracing::instrument;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use super::models::{ pub mod interior_ref_list;
InteriorRefList, ListParams, MerchandiseList, MerchandiseParams, Model, Owner, Shop, pub mod merchandise_list;
UpdateableModel, pub mod owner;
}; pub mod shop;
use super::problem::{reject_anyhow, unauthorized_no_api_key, unauthorized_no_owner}; pub mod transaction;
use super::problem::{unauthorized_no_api_key, unauthorized_no_owner};
use super::Environment; use super::Environment;
#[instrument(level = "debug", skip(env, api_key))] #[instrument(level = "debug", skip(env, api_key))]
@ -39,612 +35,3 @@ pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i3
Err(unauthorized_no_api_key()) Err(unauthorized_no_api_key())
} }
} }
pub async fn get_shop(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.shop
.get_response(id, || async {
let shop = Shop::get(&env.db, id).await?;
let reply = json(&shop);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list_shops(
list_params: ListParams,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.list_shops
.get_response(list_params.clone(), || async {
let shops = Shop::list(&env.db, &list_params).await?;
let reply = json(&shops);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create_shop(
shop: Shop,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let shop_with_owner_id = Shop {
owner_id: Some(owner_id),
..shop
};
let saved_shop = shop_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
// also save empty interior_ref_list and merchandise_list rows
if let Some(shop_id) = saved_shop.id {
let interior_ref_list = InteriorRefList {
id: None,
shop_id,
owner_id: Some(owner_id),
ref_list: Json::default(),
created_at: None,
updated_at: None,
};
interior_ref_list
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let merchandise_list = MerchandiseList {
id: None,
shop_id,
owner_id: Some(owner_id),
form_list: Json::default(),
created_at: None,
updated_at: None,
};
merchandise_list
.create(&env.db)
.await
.map_err(reject_anyhow)?;
}
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_shop);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_shops.clear().await;
Ok(reply)
}
pub async fn update_shop(
id: i32,
shop: Shop,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let shop_with_id_and_owner_id = if shop.owner_id.is_some() {
// allows an owner to transfer ownership of shop to another owner
Shop {
id: Some(id),
..shop
}
} else {
Shop {
id: Some(id),
owner_id: Some(owner_id),
..shop
}
};
let updated_shop = shop_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_shop);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.shop.delete_response(id).await;
env.caches.list_shops.clear().await;
Ok(reply)
}
pub async fn delete_shop(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
Shop::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.shop.delete_response(id).await;
env.caches.list_shops.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(id)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(id)
.await;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_owner(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.owner
.get_response(id, || async {
let owner = Owner::get(&env.db, id).await?;
let reply = json(&owner);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list_owners(
list_params: ListParams,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.list_owners
.get_response(list_params.clone(), || async {
let owners = Owner::list(&env.db, &list_params).await?;
let reply = json(&owners);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create_owner(
owner: Owner,
remote_addr: Option<SocketAddr>,
api_key: Option<Uuid>,
real_ip: Option<IpNetwork>,
env: Environment,
) -> Result<impl Reply, Rejection> {
if let Some(api_key) = api_key {
let owner_with_ip_and_key = match remote_addr {
Some(addr) => Owner {
api_key: Some(api_key),
ip_address: Some(IpNetwork::from(addr.ip())),
..owner
},
None => Owner {
api_key: Some(api_key),
ip_address: real_ip,
..owner
},
};
let saved_owner = owner_with_ip_and_key
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_owner);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_owners.clear().await;
Ok(reply)
} else {
Err(reject_anyhow(unauthorized_no_api_key()))
}
}
pub async fn update_owner(
id: i32,
owner: Owner,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let owner_with_id = Owner {
id: Some(id),
..owner
};
let updated_owner = owner_with_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_owner);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.owner.delete_response(id).await;
env.caches.list_owners.clear().await;
Ok(reply)
}
pub async fn delete_owner(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
Owner::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.owner.delete_response(id).await;
env.caches
.owner_ids_by_api_key
.delete(api_key.expect("api-key has been validated during authenticate"))
.await;
env.caches.list_owners.clear().await;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.interior_ref_list
.get_response(id, || async {
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply = json(&interior_ref_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list_interior_ref_lists(
list_params: ListParams,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.list_interior_ref_lists
.get_response(list_params.clone(), || async {
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply = json(&interior_ref_lists);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create_interior_ref_list(
interior_ref_list: InteriorRefList,
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 = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&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_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_interior_ref_lists.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
Ok(reply)
}
pub async fn update_interior_ref_list(
id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.interior_ref_list.delete_response(id).await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
env.caches.list_interior_ref_lists.clear().await;
Ok(reply)
}
pub async fn update_interior_ref_list_by_shop_id(
shop_id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.interior_ref_list
.delete_response(
updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id"),
)
.await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
env.caches.list_interior_ref_lists.clear().await;
Ok(reply)
}
pub async fn delete_interior_ref_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)?;
let interior_ref_list = InteriorRefList::get(&env.db, id)
.await
.map_err(reject_anyhow)?;
InteriorRefList::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.interior_ref_list.delete_response(id).await;
env.caches.list_interior_ref_lists.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(interior_ref_list.shop_id)
.await;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_interior_ref_list_by_shop_id(
shop_id: i32,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.interior_ref_list_by_shop_id
.get_response(shop_id, || async {
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
let reply = json(&interior_ref_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
// 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
.create(&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;
env.caches
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
Ok(reply)
}
pub async fn update_merchandise_list(
id: i32,
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 merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.merchandise_list.delete_response(id).await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(reply)
}
pub async fn update_merchandise_list_by_shop_id(
shop_id: i32,
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 merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.merchandise_list
.delete_response(
updated_merchandise_list
.id
.expect("saved merchandise_list has no id"),
)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
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)?;
let merchandise_list = MerchandiseList::get(&env.db, id)
.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;
env.caches
.merchandise_list_by_shop_id
.delete_response(merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(StatusCode::NO_CONTENT)
}
pub async fn get_merchandise_list_by_shop_id(
shop_id: i32,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.merchandise_list_by_shop_id
.get_response(shop_id, || async {
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = json(&merchandise_list);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn buy_merchandise(
shop_id: i32,
merchandise_params: MerchandiseParams,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
// TODO: create transaction
let updated_merchandise_list = MerchandiseList::update_merchandise_quantity(
&env.db,
shop_id,
&(merchandise_params.mod_name),
merchandise_params.local_form_id,
merchandise_params.quantity_delta,
)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches
.merchandise_list
.delete_response(
updated_merchandise_list
.id
.expect("saved merchandise_list has no id"),
)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
env.caches.list_merchandise_lists.clear().await;
Ok(reply)
}

114
src/handlers/owner.rs Normal file
View File

@ -0,0 +1,114 @@
use anyhow::Result;
use http::StatusCode;
use ipnetwork::IpNetwork;
use std::net::SocketAddr;
use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use crate::models::{ListParams, Model, Owner, UpdateableModel};
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
use crate::Environment;
use super::authenticate;
pub async fn get(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.owner
.get_response(id, || async {
let owner = Owner::get(&env.db, id).await?;
let reply = json(&owner);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list(list_params: ListParams, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.list_owners
.get_response(list_params.clone(), || async {
let owners = Owner::list(&env.db, &list_params).await?;
let reply = json(&owners);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create(
owner: Owner,
remote_addr: Option<SocketAddr>,
api_key: Option<Uuid>,
real_ip: Option<IpNetwork>,
env: Environment,
) -> Result<impl Reply, Rejection> {
if let Some(api_key) = api_key {
let owner_with_ip_and_key = match remote_addr {
Some(addr) => Owner {
api_key: Some(api_key),
ip_address: Some(IpNetwork::from(addr.ip())),
..owner
},
None => Owner {
api_key: Some(api_key),
ip_address: real_ip,
..owner
},
};
let saved_owner = owner_with_ip_and_key
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_owner);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_owners.clear().await;
Ok(reply)
} else {
Err(reject_anyhow(unauthorized_no_api_key()))
}
}
pub async fn update(
id: i32,
owner: Owner,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let owner_with_id = Owner {
id: Some(id),
..owner
};
let updated_owner = owner_with_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_owner);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.owner.delete_response(id).await;
env.caches.list_owners.clear().await;
Ok(reply)
}
pub async fn delete(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
Owner::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.owner.delete_response(id).await;
env.caches
.owner_ids_by_api_key
.delete(api_key.expect("api-key has been validated during authenticate"))
.await;
env.caches.list_owners.clear().await;
Ok(StatusCode::NO_CONTENT)
}

142
src/handlers/shop.rs Normal file
View File

@ -0,0 +1,142 @@
use anyhow::Result;
use http::StatusCode;
use sqlx::types::Json;
use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Model, Shop, UpdateableModel};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::authenticate;
pub async fn get(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.shop
.get_response(id, || async {
let shop = Shop::get(&env.db, id).await?;
let reply = json(&shop);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list(list_params: ListParams, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.list_shops
.get_response(list_params.clone(), || async {
let shops = Shop::list(&env.db, &list_params).await?;
let reply = json(&shops);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create(
shop: Shop,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let shop_with_owner_id = Shop {
owner_id: Some(owner_id),
..shop
};
let saved_shop = shop_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
// also save empty interior_ref_list and merchandise_list rows
if let Some(shop_id) = saved_shop.id {
let interior_ref_list = InteriorRefList {
id: None,
shop_id,
owner_id: Some(owner_id),
ref_list: Json::default(),
created_at: None,
updated_at: None,
};
interior_ref_list
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let merchandise_list = MerchandiseList {
id: None,
shop_id,
owner_id: Some(owner_id),
form_list: Json::default(),
created_at: None,
updated_at: None,
};
merchandise_list
.create(&env.db)
.await
.map_err(reject_anyhow)?;
}
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_shop);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_shops.clear().await;
Ok(reply)
}
pub async fn update(
id: i32,
shop: Shop,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let shop_with_id_and_owner_id = if shop.owner_id.is_some() {
// allows an owner to transfer ownership of shop to another owner
Shop {
id: Some(id),
..shop
}
} else {
Shop {
id: Some(id),
owner_id: Some(owner_id),
..shop
}
};
let updated_shop = shop_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_shop);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
env.caches.shop.delete_response(id).await;
env.caches.list_shops.clear().await;
Ok(reply)
}
pub async fn delete(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
Shop::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.shop.delete_response(id).await;
env.caches.list_shops.clear().await;
env.caches
.interior_ref_list_by_shop_id
.delete_response(id)
.await;
env.caches
.merchandise_list_by_shop_id
.delete_response(id)
.await;
Ok(StatusCode::NO_CONTENT)
}

View File

@ -0,0 +1,91 @@
use anyhow::Result;
use http::StatusCode;
use uuid::Uuid;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
use crate::models::{ListParams, Model, Transaction};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::authenticate;
pub async fn get(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.transaction
.get_response(id, || async {
let transaction = Transaction::get(&env.db, id).await?;
let reply = json(&transaction);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list(list_params: ListParams, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.list_transactions
.get_response(list_params.clone(), || async {
let transactions = Transaction::list(&env.db, &list_params).await?;
let reply = json(&transactions);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn list_by_shop_id(
shop_id: i32,
list_params: ListParams,
env: Environment,
) -> Result<impl Reply, Rejection> {
env.caches
.list_transactions_by_shop_id
.get_response((shop_id, list_params.clone()), || async {
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
let reply = json(&transactions);
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
}
pub async fn create(
transaction: Transaction,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let transaction_with_owner_id = Transaction {
owner_id: Some(owner_id),
..transaction
};
let saved_transaction = transaction_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_transaction);
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
// TODO: will this make these caches effectively useless?
env.caches.list_transactions.clear().await;
env.caches.list_transactions_by_shop_id.clear().await;
Ok(reply)
}
pub async fn delete(
id: i32,
api_key: Option<Uuid>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let transaction = Transaction::get(&env.db, id).await.map_err(reject_anyhow)?;
Transaction::delete(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
env.caches.transaction.delete_response(id).await;
env.caches.list_transactions.clear().await;
env.caches.list_transactions_by_shop_id.clear().await;
Ok(StatusCode::NO_CONTENT)
}

View File

@ -19,11 +19,9 @@ mod models;
mod problem; mod problem;
use caches::Caches; use caches::Caches;
use models::interior_ref_list::InteriorRefList; use models::{
use models::merchandise_list::{MerchandiseList, MerchandiseParams}; InteriorRefList, ListParams, MerchandiseList, MerchandiseParams, Owner, Shop, Transaction,
use models::owner::Owner; };
use models::shop::Shop;
use models::ListParams;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Environment { pub struct Environment {
@ -86,7 +84,7 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_owner), .and_then(handlers::owner::get),
); );
let create_owner_handler = warp::path("owners").and( let create_owner_handler = warp::path("owners").and(
warp::path::end() warp::path::end()
@ -96,7 +94,7 @@ async fn main() -> Result<()> {
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(warp::header::optional("x-real-ip")) .and(warp::header::optional("x-real-ip"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::create_owner), .and_then(handlers::owner::create),
); );
let delete_owner_handler = warp::path("owners").and( let delete_owner_handler = warp::path("owners").and(
warp::path::param() warp::path::param()
@ -104,7 +102,7 @@ async fn main() -> Result<()> {
.and(warp::delete()) .and(warp::delete())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::delete_owner), .and_then(handlers::owner::delete),
); );
let update_owner_handler = warp::path("owners").and( let update_owner_handler = warp::path("owners").and(
warp::path::param() warp::path::param()
@ -113,21 +111,21 @@ async fn main() -> Result<()> {
.and(json_body::<Owner>()) .and(json_body::<Owner>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_owner), .and_then(handlers::owner::update),
); );
let list_owners_handler = warp::path("owners").and( let list_owners_handler = warp::path("owners").and(
warp::path::end() warp::path::end()
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::list_owners), .and_then(handlers::owner::list),
); );
let get_shop_handler = warp::path("shops").and( let get_shop_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_shop), .and_then(handlers::shop::get),
); );
let create_shop_handler = warp::path("shops").and( let create_shop_handler = warp::path("shops").and(
warp::path::end() warp::path::end()
@ -135,7 +133,7 @@ async fn main() -> Result<()> {
.and(json_body::<Shop>()) .and(json_body::<Shop>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::create_shop), .and_then(handlers::shop::create),
); );
let delete_shop_handler = warp::path("shops").and( let delete_shop_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -143,7 +141,7 @@ async fn main() -> Result<()> {
.and(warp::delete()) .and(warp::delete())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::delete_shop), .and_then(handlers::shop::delete),
); );
let update_shop_handler = warp::path("shops").and( let update_shop_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -152,21 +150,21 @@ async fn main() -> Result<()> {
.and(json_body::<Shop>()) .and(json_body::<Shop>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_shop), .and_then(handlers::shop::update),
); );
let list_shops_handler = warp::path("shops").and( let list_shops_handler = warp::path("shops").and(
warp::path::end() warp::path::end()
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::list_shops), .and_then(handlers::shop::list),
); );
let get_interior_ref_list_handler = warp::path("interior_ref_lists").and( let get_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::param() warp::path::param()
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_interior_ref_list), .and_then(handlers::interior_ref_list::get),
); );
let create_interior_ref_list_handler = warp::path("interior_ref_lists").and( let create_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::end() warp::path::end()
@ -174,7 +172,7 @@ async fn main() -> Result<()> {
.and(json_body::<InteriorRefList>()) .and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::create_interior_ref_list), .and_then(handlers::interior_ref_list::create),
); );
let delete_interior_ref_list_handler = warp::path("interior_ref_lists").and( let delete_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::param() warp::path::param()
@ -182,7 +180,7 @@ async fn main() -> Result<()> {
.and(warp::delete()) .and(warp::delete())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::delete_interior_ref_list), .and_then(handlers::interior_ref_list::delete),
); );
let update_interior_ref_list_handler = warp::path("interior_ref_lists").and( let update_interior_ref_list_handler = warp::path("interior_ref_lists").and(
warp::path::param() warp::path::param()
@ -191,7 +189,7 @@ async fn main() -> Result<()> {
.and(json_body::<InteriorRefList>()) .and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_interior_ref_list), .and_then(handlers::interior_ref_list::update),
); );
let update_interior_ref_list_by_shop_id_handler = warp::path("shops").and( let update_interior_ref_list_by_shop_id_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -201,14 +199,14 @@ async fn main() -> Result<()> {
.and(json_body::<InteriorRefList>()) .and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_interior_ref_list_by_shop_id), .and_then(handlers::interior_ref_list::update_by_shop_id),
); );
let list_interior_ref_lists_handler = warp::path("interior_ref_lists").and( let list_interior_ref_lists_handler = warp::path("interior_ref_lists").and(
warp::path::end() warp::path::end()
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::list_interior_ref_lists), .and_then(handlers::interior_ref_list::list),
); );
let get_interior_ref_list_by_shop_id_handler = warp::path("shops").and( let get_interior_ref_list_by_shop_id_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -216,14 +214,14 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_interior_ref_list_by_shop_id), .and_then(handlers::interior_ref_list::get_by_shop_id),
); );
let get_merchandise_list_handler = warp::path("merchandise_lists").and( let get_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::param() warp::path::param()
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_merchandise_list), .and_then(handlers::merchandise_list::get),
); );
let create_merchandise_list_handler = warp::path("merchandise_lists").and( let create_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::end() warp::path::end()
@ -231,7 +229,7 @@ async fn main() -> Result<()> {
.and(json_body::<MerchandiseList>()) .and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::create_merchandise_list), .and_then(handlers::merchandise_list::create),
); );
let delete_merchandise_list_handler = warp::path("merchandise_lists").and( let delete_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::param() warp::path::param()
@ -239,7 +237,7 @@ async fn main() -> Result<()> {
.and(warp::delete()) .and(warp::delete())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::delete_merchandise_list), .and_then(handlers::merchandise_list::delete),
); );
let update_merchandise_list_handler = warp::path("merchandise_lists").and( let update_merchandise_list_handler = warp::path("merchandise_lists").and(
warp::path::param() warp::path::param()
@ -248,7 +246,7 @@ async fn main() -> Result<()> {
.and(json_body::<MerchandiseList>()) .and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_merchandise_list), .and_then(handlers::merchandise_list::update),
); );
let update_merchandise_list_by_shop_id_handler = warp::path("shops").and( let update_merchandise_list_by_shop_id_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -258,14 +256,14 @@ async fn main() -> Result<()> {
.and(json_body::<MerchandiseList>()) .and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::update_merchandise_list_by_shop_id), .and_then(handlers::merchandise_list::update_by_shop_id),
); );
let list_merchandise_lists_handler = warp::path("merchandise_lists").and( let list_merchandise_lists_handler = warp::path("merchandise_lists").and(
warp::path::end() warp::path::end()
.and(warp::get()) .and(warp::get())
.and(warp::query::<ListParams>()) .and(warp::query::<ListParams>())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::list_merchandise_lists), .and_then(handlers::merchandise_list::list),
); );
let get_merchandise_list_by_shop_id_handler = warp::path("shops").and( let get_merchandise_list_by_shop_id_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -273,7 +271,7 @@ async fn main() -> Result<()> {
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::get_merchandise_list_by_shop_id), .and_then(handlers::merchandise_list::get_by_shop_id),
); );
let buy_merchandise_handler = warp::path("shops").and( let buy_merchandise_handler = warp::path("shops").and(
warp::path::param() warp::path::param()
@ -283,7 +281,46 @@ async fn main() -> Result<()> {
.and(warp::query::<MerchandiseParams>()) .and(warp::query::<MerchandiseParams>())
.and(warp::header::optional("api-key")) .and(warp::header::optional("api-key"))
.and(with_env(env.clone())) .and(with_env(env.clone()))
.and_then(handlers::buy_merchandise), .and_then(handlers::merchandise_list::buy_merchandise),
);
let get_transaction_handler = warp::path("transactions").and(
warp::path::param()
.and(warp::path::end())
.and(warp::get())
.and(with_env(env.clone()))
.and_then(handlers::transaction::get),
);
let create_transaction_handler = warp::path("transactions").and(
warp::path::end()
.and(warp::post())
.and(json_body::<Transaction>())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::transaction::create),
);
let delete_transaction_handler = warp::path("transactions").and(
warp::path::param()
.and(warp::path::end())
.and(warp::delete())
.and(warp::header::optional("api-key"))
.and(with_env(env.clone()))
.and_then(handlers::transaction::delete),
);
let list_transactions_handler = warp::path("transactions").and(
warp::path::end()
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::transaction::list),
);
let list_transactions_by_shop_id_handler = warp::path("shops").and(
warp::path::param()
.and(warp::path("transactions"))
.and(warp::path::end())
.and(warp::get())
.and(warp::query::<ListParams>())
.and(with_env(env.clone()))
.and_then(handlers::transaction::list_by_shop_id),
); );
let routes = warp::path("v1") let routes = warp::path("v1")
@ -303,6 +340,7 @@ async fn main() -> Result<()> {
get_merchandise_list_by_shop_id_handler, get_merchandise_list_by_shop_id_handler,
update_interior_ref_list_by_shop_id_handler, update_interior_ref_list_by_shop_id_handler,
update_merchandise_list_by_shop_id_handler, update_merchandise_list_by_shop_id_handler,
list_transactions_by_shop_id_handler,
buy_merchandise_handler, buy_merchandise_handler,
get_interior_ref_list_handler, get_interior_ref_list_handler,
delete_interior_ref_list_handler, delete_interior_ref_list_handler,
@ -314,6 +352,10 @@ async fn main() -> Result<()> {
update_merchandise_list_handler, update_merchandise_list_handler,
create_merchandise_list_handler, create_merchandise_list_handler,
list_merchandise_lists_handler, list_merchandise_lists_handler,
get_transaction_handler,
delete_transaction_handler,
create_transaction_handler,
list_transactions_handler,
// warp::any().map(|| StatusCode::NOT_FOUND), // warp::any().map(|| StatusCode::NOT_FOUND),
)) ))
.recover(problem::unpack_problem) .recover(problem::unpack_problem)

View File

@ -7,12 +7,14 @@ pub mod merchandise_list;
pub mod model; pub mod model;
pub mod owner; pub mod owner;
pub mod shop; pub mod shop;
pub mod transaction;
pub use interior_ref_list::InteriorRefList; pub use interior_ref_list::InteriorRefList;
pub use merchandise_list::{MerchandiseList, MerchandiseParams}; pub use merchandise_list::{MerchandiseList, MerchandiseParams};
pub use model::{Model, UpdateableModel}; pub use model::{Model, UpdateableModel};
pub use owner::Owner; pub use owner::Owner;
pub use shop::Shop; pub use shop::Shop;
pub use transaction::Transaction;
#[derive(Debug, Eq, PartialEq, Hash, Clone, Deserialize)] #[derive(Debug, Eq, PartialEq, Hash, Clone, Deserialize)]
pub enum Order { pub enum Order {

147
src/models/transaction.rs Normal file
View File

@ -0,0 +1,147 @@
use anyhow::{Error, Result};
use async_trait::async_trait;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;
use tracing::instrument;
use super::ListParams;
use super::Model;
use crate::problem::forbidden_permission;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Transaction {
pub id: Option<i32>,
pub shop_id: i32,
pub owner_id: Option<i32>,
pub mod_name: String,
pub local_form_id: i32,
pub is_sell: bool,
pub quantity: i32,
pub amount: i32,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
#[async_trait]
impl Model for Transaction {
fn resource_name() -> &'static str {
"transaction"
}
fn pk(&self) -> Option<i32> {
self.id
}
#[instrument(level = "debug", skip(db))]
async fn get(db: &PgPool, id: i32) -> Result<Self> {
sqlx::query_as!(Self, "SELECT * FROM transactions WHERE id = $1", id)
.fetch_one(db)
.await
.map_err(Error::new)
}
#[instrument(level = "debug", skip(db))]
async fn create(self, db: &PgPool) -> Result<Self> {
Ok(sqlx::query_as!(
Self,
"INSERT INTO transactions
(shop_id, owner_id, mod_name, local_form_id, is_sell, quantity, amount, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())
RETURNING *",
self.shop_id,
self.owner_id,
self.mod_name,
self.local_form_id,
self.is_sell,
self.quantity,
self.amount,
)
.fetch_one(db)
.await?)
}
#[instrument(level = "debug", skip(db))]
async fn delete(db: &PgPool, owner_id: i32, id: i32) -> Result<u64> {
let transaction = sqlx::query!("SELECT owner_id FROM transactions WHERE id = $1", id)
.fetch_one(db)
.await?;
if transaction.owner_id == owner_id {
return Ok(sqlx::query!("DELETE FROM transactions 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!(
Self,
"SELECT * FROM transactions
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!(
Self,
"SELECT * FROM transactions
LIMIT $1
OFFSET $2",
list_params.limit.unwrap_or(10),
list_params.offset.unwrap_or(0),
)
.fetch_all(db)
.await?
};
Ok(result)
}
}
impl Transaction {
#[instrument(level = "debug", skip(db))]
pub async fn list_by_shop_id(
db: &PgPool,
shop_id: i32,
list_params: &ListParams,
) -> Result<Vec<Self>> {
let result = if let Some(order_by) = list_params.get_order_by() {
sqlx::query_as!(
Self,
"SELECT * FROM transactions
WHERE shop_id = $1
ORDER BY $2
LIMIT $3
OFFSET $4",
shop_id,
order_by,
list_params.limit.unwrap_or(10),
list_params.offset.unwrap_or(0),
)
.fetch_all(db)
.await?
} else {
sqlx::query_as!(
Self,
"SELECT * FROM transactions
WHERE shop_id = $1
LIMIT $2
OFFSET $3",
shop_id,
list_params.limit.unwrap_or(10),
list_params.offset.unwrap_or(0),
)
.fetch_all(db)
.await?
};
Ok(result)
}
}

View File

@ -0,0 +1,8 @@
{
"shop_id": 1,
"mod_name": "Skyrim.esm",
"local_form_id": 1,
"is_sell": false,
"quantity": 1,
"amount": 100
}