diff --git a/src/routes/item/demand.rs b/src/routes/item/demand.rs new file mode 100644 index 0000000..32ff883 --- /dev/null +++ b/src/routes/item/demand.rs @@ -0,0 +1,52 @@ +use rocket::serde::json::Json; +use rocket::{get, post, State}; +use serde::Deserialize; +use serde_json::json; + +use crate::variant::Variant; +use crate::{ + db::ItemDB, + routes::{api_error, FallibleApiResponse}, +}; + +use super::{item_does_not_exist_error, variant_does_not_exist_error}; + +#[derive(Debug, Deserialize)] +pub struct DemandForm { + uuid: String, + destination: String, + price: String, +} + +#[post("/demand", data = "")] +pub async fn demand_route(f: Json) -> 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})) +} + +#[get("/item///demand")] +pub async fn demand_log_route( + item_id: &str, + variant_id: &str, + itemdb: &State, +) -> 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)) +} diff --git a/src/routes/item/error.rs b/src/routes/item/error.rs new file mode 100644 index 0000000..7d76a1a --- /dev/null +++ b/src/routes/item/error.rs @@ -0,0 +1,9 @@ +use crate::routes::{api_error, ApiError}; + +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") +} diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 8e76cdb..310dcb2 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -1,77 +1,22 @@ -use rocket::serde::json::Json; +mod demand; +mod error; +mod supply; + +pub use demand::*; +pub use error::*; +pub use supply::*; + +use rocket::get; use rocket::State; -use rocket::{get, post}; -use serde::Deserialize; use serde_json::json; use crate::db::ItemDB; use crate::transaction::Transaction; -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 = "")] -pub async fn demand_route(f: Json) -> 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, - price: String, - origin: String, -} - -#[post("/supply", data = "
")] -pub async fn supply_route(form: Json, itemdb: &State) -> 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})) -} +use super::api_error; +use crate::routes::FallibleApiResponse; +/// Returns a JSON response with all items in the database. #[get("/items")] pub fn get_items_route(itemdb: &State) -> serde_json::Value { let items = itemdb.items(); @@ -99,40 +44,6 @@ pub fn item_variants_page(item_id: &str, itemdb: &State) -> FallibleApiR })) } -#[get("/item///supply")] -pub async fn supply_log_route( - item_id: &str, - variant_id: &str, - itemdb: &State, -) -> 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///demand")] -pub async fn demand_log_route( - item_id: &str, - variant_id: &str, - itemdb: &State, -) -> 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)) -} - #[get("/transaction/")] pub async fn transaction_route(transaction: &str) -> FallibleApiResponse { let t = Transaction::get(transaction) diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs new file mode 100644 index 0000000..0a4682f --- /dev/null +++ b/src/routes/item/supply.rs @@ -0,0 +1,60 @@ +use rocket::serde::json::Json; +use rocket::{get, post, State}; +use serde::Deserialize; +use serde_json::json; + +use crate::{ + db::ItemDB, + routes::{api_error, FallibleApiResponse}, +}; + +use super::{item_does_not_exist_error, variant_does_not_exist_error}; + +#[derive(Deserialize, Debug)] +pub struct SupplyForm { + item: String, + variant: String, + amount: Option, + price: String, + origin: String, +} + +#[post("/supply", data = "")] +pub async fn supply_route(form: Json, itemdb: &State) -> 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("/item///supply")] +pub async fn supply_log_route( + item_id: &str, + variant_id: &str, + itemdb: &State, +) -> 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)) +}