cdb/src/routes/item/mod.rs

126 lines
3.1 KiB
Rust
Raw Normal View History

2024-06-21 21:14:45 +02:00
use rocket::serde::json::Json;
use rocket::State;
use rocket::{get, post};
2024-04-29 13:11:27 +02:00
use serde::Deserialize;
2024-06-21 21:14:45 +02:00
use serde_json::json;
2024-01-16 09:02:23 +01:00
2024-06-21 21:14:45 +02:00
use crate::db::ItemDB;
2024-05-10 11:59:05 +02:00
use crate::variant::Variant;
2024-01-16 09:02:23 +01:00
2024-06-21 21:14:45 +02:00
use super::{api_error, ApiError, FallibleApiResponse};
2024-04-29 13:11:27 +02:00
2024-06-21 21:14:45 +02:00
pub fn item_does_not_exist_error() -> ApiError {
api_error("The item does not exist")
2024-05-10 11:59:05 +02:00
}
2024-06-21 21:14:45 +02:00
pub fn variant_does_not_exist_error() -> ApiError {
api_error("The item does not exist")
2024-05-10 11:59:05 +02:00
}
#[derive(Debug, Deserialize)]
pub struct DemandForm {
uuid: String,
destination: String,
price: String,
}
2024-06-21 21:14:45 +02:00
#[post("/demand", data = "<f>")]
pub async fn demand_route(f: Json<DemandForm>) -> FallibleApiResponse {
2024-05-10 11:59:05 +02:00
let uuid = Variant::demand(
&f.uuid,
f.price
.clone()
.try_into()
2024-06-21 21:14:45 +02:00
.map_err(|()| api_error("Price malformed"))?,
2024-05-10 11:59:05 +02:00
&f.destination,
)
.await
2024-06-21 21:14:45 +02:00
.ok_or_else(|| api_error("Demand failed"))?;
2024-05-10 11:59:05 +02:00
2024-06-21 21:14:45 +02:00
Ok(json!({"uuid": uuid}))
2024-05-10 11:59:05 +02:00
}
2024-04-29 13:11:27 +02:00
#[derive(Deserialize, Debug)]
pub struct SupplyForm {
item: String,
variant: String,
amount: Option<usize>,
price: String,
origin: String,
}
2024-06-21 21:14:45 +02:00
#[post("/supply", data = "<form>")]
pub async fn supply_route(form: Json<SupplyForm>, itemdb: &State<ItemDB>) -> FallibleApiResponse {
2024-04-29 13:11:27 +02:00
println!("{form:?}");
let variant = itemdb
.get_item(&form.item)
2024-05-10 11:59:05 +02:00
.ok_or_else(item_does_not_exist_error)?
2024-04-29 13:11:27 +02:00
.variant(&form.variant)
2024-05-10 11:59:05 +02:00
.ok_or_else(variant_does_not_exist_error)?;
2024-05-03 10:30:05 +02:00
2024-04-29 13:11:27 +02:00
let transaction_id = variant
.supply(
form.amount.unwrap_or(1),
2024-05-03 10:30:05 +02:00
form.price
.clone()
.try_into()
2024-06-21 21:14:45 +02:00
.map_err(|()| api_error("Price malformed"))?,
2024-04-29 13:11:27 +02:00
&form.origin,
2024-01-16 09:02:23 +01:00
)
2024-04-29 13:11:27 +02:00
.await;
2024-05-10 11:59:05 +02:00
2024-06-21 21:14:45 +02:00
Ok(json!({"uuid": transaction_id}))
2024-04-29 16:05:09 +02:00
}
#[get("/items")]
2024-06-21 21:14:45 +02:00
pub fn get_items_route(itemdb: &State<ItemDB>) -> serde_json::Value {
2024-04-29 16:05:09 +02:00
let items = itemdb.items();
2024-06-21 21:14:45 +02:00
json!({"items": items})
2024-05-02 16:13:31 +02:00
}
2024-06-21 21:14:45 +02:00
#[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();
2024-04-29 13:11:27 +02:00
2024-06-21 21:14:45 +02:00
Ok(json!({
"item": item_id,
2024-04-29 13:11:27 +02:00
"variants": variants
2024-06-21 21:14:45 +02:00
}))
2024-01-16 09:02:23 +01:00
}
2024-05-10 11:59:05 +02:00
2024-06-21 21:14:45 +02:00
#[get("/item/<item_id>/<variant_id>/supply")]
pub async fn supply_log_route(
item_id: &str,
variant_id: &str,
itemdb: &State<ItemDB>,
) -> FallibleApiResponse {
2024-05-10 11:59:05 +02:00
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;
2024-06-21 21:14:45 +02:00
Ok(json!(transactions))
2024-05-10 11:59:05 +02:00
}
2024-06-21 21:14:45 +02:00
#[get("/item/<item_id>/<variant_id>/demand")]
pub async fn demand_log_route(
item_id: &str,
variant_id: &str,
itemdb: &State<ItemDB>,
) -> FallibleApiResponse {
2024-05-10 11:59:05 +02:00
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;
2024-06-21 21:14:45 +02:00
Ok(json!(transactions))
2024-05-10 11:59:05 +02:00
}