move to rocket + refactor
This commit is contained in:
parent
b0e80dd3e8
commit
b8caa43972
11 changed files with 686 additions and 886 deletions
|
@ -1,24 +1,20 @@
|
|||
use actix_web::post;
|
||||
use actix_web::{get, HttpRequest, HttpResponse, Responder};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::State;
|
||||
use rocket::{get, post};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::item;
|
||||
use crate::routes::bad_req;
|
||||
use crate::db::ItemDB;
|
||||
use crate::variant::Variant;
|
||||
|
||||
macro_rules! get_itemdb {
|
||||
($req:expr) => {{
|
||||
let itemdb: &actix_web::web::Data<crate::db::ItemDB> = $req.app_data().unwrap();
|
||||
itemdb
|
||||
}};
|
||||
use super::{api_error, ApiError, FallibleApiResponse};
|
||||
|
||||
pub fn item_does_not_exist_error() -> ApiError {
|
||||
api_error("The item does not exist")
|
||||
}
|
||||
|
||||
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")
|
||||
pub fn variant_does_not_exist_error() -> ApiError {
|
||||
api_error("The item does not exist")
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -28,24 +24,20 @@ pub struct DemandForm {
|
|||
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);
|
||||
#[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(|_| bad_req("Price malformed"))?,
|
||||
.map_err(|()| api_error("Price malformed"))?,
|
||||
&f.destination,
|
||||
)
|
||||
.await
|
||||
.ok_or_else(|| bad_req("Demand failed"))?;
|
||||
.ok_or_else(|| api_error("Demand failed"))?;
|
||||
|
||||
Ok(actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": uuid})))
|
||||
Ok(json!({"uuid": uuid}))
|
||||
}
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SupplyForm {
|
||||
|
@ -56,18 +48,13 @@ pub struct SupplyForm {
|
|||
origin: String,
|
||||
}
|
||||
|
||||
#[post("/supply")]
|
||||
pub async fn supply_route(
|
||||
req: HttpRequest,
|
||||
form: actix_web::web::Form<SupplyForm>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let itemdb = get_itemdb!(req);
|
||||
#[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)
|
||||
.await
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transaction_id = variant
|
||||
|
@ -76,72 +63,63 @@ pub async fn supply_route(
|
|||
form.price
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| bad_req("Price malformed"))?,
|
||||
.map_err(|()| api_error("Price malformed"))?,
|
||||
&form.origin,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id})))
|
||||
Ok(json!({"uuid": transaction_id}))
|
||||
}
|
||||
|
||||
#[get("/items")]
|
||||
pub async fn get_items_route(r: HttpRequest) -> impl Responder {
|
||||
let itemdb = get_itemdb!(r);
|
||||
pub fn get_items_route(itemdb: &State<ItemDB>) -> serde_json::Value {
|
||||
let items = itemdb.items();
|
||||
actix_web::HttpResponse::Ok().json(serde_json::json!({"items": items}))
|
||||
json!({"items": items})
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct AddVariantForm {
|
||||
pub variant: String,
|
||||
pub amount: usize,
|
||||
}
|
||||
#[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();
|
||||
|
||||
#[get("/item/{item_id}/variants")]
|
||||
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(item_does_not_exist_error)?;
|
||||
let variants = item.get_variants().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"item": id,
|
||||
Ok(json!({
|
||||
"item": item_id,
|
||||
"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");
|
||||
|
||||
#[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)
|
||||
.await
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.supply_log().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!(transactions)))
|
||||
Ok(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");
|
||||
|
||||
#[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)
|
||||
.await
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.demand_log().await;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!(transactions)))
|
||||
Ok(json!(transactions))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue