work
This commit is contained in:
parent
985296d366
commit
9d5ec6d1b3
4 changed files with 135 additions and 7 deletions
|
@ -4,6 +4,7 @@ use serde::Deserialize;
|
|||
|
||||
use crate::item;
|
||||
use crate::routes::bad_req;
|
||||
use crate::variant::Variant;
|
||||
|
||||
macro_rules! get_itemdb {
|
||||
($req:expr) => {{
|
||||
|
@ -12,6 +13,40 @@ macro_rules! get_itemdb {
|
|||
}};
|
||||
}
|
||||
|
||||
pub fn item_does_not_exist_error() -> actix_web::Error {
|
||||
bad_req("The item does not exist")
|
||||
}
|
||||
|
||||
pub fn variant_does_not_exist_error() -> actix_web::Error {
|
||||
bad_req("The item does not exist")
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct DemandForm {
|
||||
uuid: String,
|
||||
destination: String,
|
||||
price: String,
|
||||
}
|
||||
|
||||
#[post("/demand")]
|
||||
pub async fn demand_route(
|
||||
req: HttpRequest,
|
||||
f: actix_web::web::Form<DemandForm>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let itemdb = get_itemdb!(req);
|
||||
let uuid = Variant::demand(
|
||||
&f.uuid,
|
||||
f.price
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| bad_req("Price malformed"))?,
|
||||
&f.destination,
|
||||
)
|
||||
.await
|
||||
.ok_or_else(|| bad_req("Demand failed"))?;
|
||||
|
||||
Ok(actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": uuid})))
|
||||
}
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SupplyForm {
|
||||
item: String,
|
||||
|
@ -30,10 +65,10 @@ pub async fn supply_route(
|
|||
println!("{form:?}");
|
||||
let variant = itemdb
|
||||
.get_item(&form.item)
|
||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))?
|
||||
.ok_or_else(item_does_not_exist_error)?
|
||||
.variant(&form.variant)
|
||||
.await
|
||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transaction_id = variant
|
||||
.supply(
|
||||
|
@ -45,6 +80,7 @@ pub async fn supply_route(
|
|||
&form.origin,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id})))
|
||||
}
|
||||
|
||||
|
@ -65,9 +101,7 @@ pub struct AddVariantForm {
|
|||
pub async fn item_variants_page(r: HttpRequest) -> actix_web::Result<impl Responder> {
|
||||
let id = r.match_info().query("item_id");
|
||||
let itemdb = get_itemdb!(r);
|
||||
let item = itemdb
|
||||
.get_item(id)
|
||||
.ok_or_else(|| bad_req("The item does not exist"))?;
|
||||
let item = itemdb.get_item(id).ok_or_else(item_does_not_exist_error)?;
|
||||
let variants = item.get_variants().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
|
@ -75,3 +109,39 @@ pub async fn item_variants_page(r: HttpRequest) -> actix_web::Result<impl Respon
|
|||
"variants": variants
|
||||
})))
|
||||
}
|
||||
|
||||
#[get("/item/{item_id}/{variant_id}/supply")]
|
||||
pub async fn supply_log_route(r: HttpRequest) -> actix_web::Result<impl Responder> {
|
||||
let itemdb = get_itemdb!(r);
|
||||
let item_id = r.match_info().query("item_id");
|
||||
let variant_id = r.match_info().query("variant_id");
|
||||
|
||||
let variant = itemdb
|
||||
.get_item(item_id)
|
||||
.ok_or_else(item_does_not_exist_error)?
|
||||
.variant(variant_id)
|
||||
.await
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.supply_log().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!(transactions)))
|
||||
}
|
||||
|
||||
#[get("/item/{item_id}/{variant_id}/demand")]
|
||||
pub async fn demand_log_route(r: HttpRequest) -> actix_web::Result<impl Responder> {
|
||||
let itemdb = get_itemdb!(r);
|
||||
let item_id = r.match_info().query("item_id");
|
||||
let variant_id = r.match_info().query("variant_id");
|
||||
|
||||
let variant = itemdb
|
||||
.get_item(item_id)
|
||||
.ok_or_else(item_does_not_exist_error)?
|
||||
.variant(variant_id)
|
||||
.await
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.demand_log().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!(transactions)))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue