125 lines
3.1 KiB
Rust
125 lines
3.1 KiB
Rust
use rocket::serde::json::Json;
|
|
use rocket::State;
|
|
use rocket::{get, post};
|
|
use serde::Deserialize;
|
|
use serde_json::json;
|
|
|
|
use crate::db::ItemDB;
|
|
use crate::variant::Variant;
|
|
|
|
use super::{api_error, ApiError, FallibleApiResponse};
|
|
|
|
pub fn item_does_not_exist_error() -> ApiError {
|
|
api_error("The item does not exist")
|
|
}
|
|
|
|
pub fn variant_does_not_exist_error() -> ApiError {
|
|
api_error("The item does not exist")
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct DemandForm {
|
|
uuid: String,
|
|
destination: String,
|
|
price: String,
|
|
}
|
|
|
|
#[post("/demand", data = "<f>")]
|
|
pub async fn demand_route(f: Json<DemandForm>) -> FallibleApiResponse {
|
|
let uuid = Variant::demand(
|
|
&f.uuid,
|
|
f.price
|
|
.clone()
|
|
.try_into()
|
|
.map_err(|()| api_error("Price malformed"))?,
|
|
&f.destination,
|
|
)
|
|
.await
|
|
.ok_or_else(|| api_error("Demand failed"))?;
|
|
|
|
Ok(json!({"uuid": uuid}))
|
|
}
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct SupplyForm {
|
|
item: String,
|
|
variant: String,
|
|
amount: Option<usize>,
|
|
price: String,
|
|
origin: String,
|
|
}
|
|
|
|
#[post("/supply", data = "<form>")]
|
|
pub async fn supply_route(form: Json<SupplyForm>, itemdb: &State<ItemDB>) -> FallibleApiResponse {
|
|
println!("{form:?}");
|
|
let variant = itemdb
|
|
.get_item(&form.item)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(&form.variant)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transaction_id = variant
|
|
.supply(
|
|
form.amount.unwrap_or(1),
|
|
form.price
|
|
.clone()
|
|
.try_into()
|
|
.map_err(|()| api_error("Price malformed"))?,
|
|
&form.origin,
|
|
)
|
|
.await;
|
|
|
|
Ok(json!({"uuid": transaction_id}))
|
|
}
|
|
|
|
#[get("/items")]
|
|
pub fn get_items_route(itemdb: &State<ItemDB>) -> serde_json::Value {
|
|
let items = itemdb.items();
|
|
json!({"items": items})
|
|
}
|
|
|
|
#[get("/item/<item_id>/variants")]
|
|
pub fn item_variants_page(item_id: &str, itemdb: &State<ItemDB>) -> FallibleApiResponse {
|
|
let item = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?;
|
|
let variants = item.get_variants();
|
|
|
|
Ok(json!({
|
|
"item": item_id,
|
|
"variants": variants
|
|
}))
|
|
}
|
|
|
|
#[get("/item/<item_id>/<variant_id>/supply")]
|
|
pub async fn supply_log_route(
|
|
item_id: &str,
|
|
variant_id: &str,
|
|
itemdb: &State<ItemDB>,
|
|
) -> FallibleApiResponse {
|
|
let variant = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(variant_id)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transactions = variant.supply_log().await;
|
|
|
|
Ok(json!(transactions))
|
|
}
|
|
|
|
#[get("/item/<item_id>/<variant_id>/demand")]
|
|
pub async fn demand_log_route(
|
|
item_id: &str,
|
|
variant_id: &str,
|
|
itemdb: &State<ItemDB>,
|
|
) -> FallibleApiResponse {
|
|
let variant = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(variant_id)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transactions = variant.demand_log().await;
|
|
|
|
Ok(json!(transactions))
|
|
}
|