cdb/src/routes/item/mod.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

2024-04-29 13:11:27 +02:00
use actix_web::post;
use actix_web::{get, HttpRequest, HttpResponse, Responder};
2024-01-16 09:02:23 +01:00
use maud::html;
2024-04-29 13:11:27 +02:00
use serde::Deserialize;
2024-01-16 09:02:23 +01:00
2024-04-04 07:41:13 +02:00
use crate::item;
2024-01-16 09:02:23 +01:00
2024-04-29 13:11:27 +02:00
macro_rules! get_itemdb {
($req:expr) => {{
let itemdb: &actix_web::web::Data<item::ItemDB> = $req.app_data().unwrap();
itemdb
}};
}
#[derive(Deserialize, Debug)]
pub struct SupplyForm {
item: String,
variant: String,
amount: Option<usize>,
price: String,
origin: String,
}
#[post("/supply")]
pub async fn supply_route(
req: HttpRequest,
form: actix_web::web::Form<SupplyForm>,
) -> impl Responder {
let itemdb = get_itemdb!(req);
println!("{form:?}");
let variant = itemdb
.get_item(&form.item)
.unwrap()
.variant(&form.variant)
.unwrap();
let transaction_id = variant
.supply(
form.amount.unwrap_or(1),
form.price.clone().into(),
&form.origin,
2024-01-16 09:02:23 +01:00
)
2024-04-29 13:11:27 +02:00
.await;
actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id}))
}
#[get("/item/{item_id}/variants")]
pub async fn item_variants_page(r: HttpRequest) -> impl Responder {
let id = r.match_info().query("item_id");
let itemdb = get_itemdb!(r);
let item = itemdb.get_item(id);
let variants = item.unwrap().get_variants().await;
2024-01-16 09:02:23 +01:00
2024-04-29 13:11:27 +02:00
HttpResponse::Ok().json(serde_json::json!({
"item": id,
"variants": variants
}))
2024-01-16 09:02:23 +01:00
}