about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--crates/rocie-server/src/api/set/barcode.rs15
-rw-r--r--crates/rocie-server/tests/products/barcode.rs37
-rw-r--r--crates/rocie-server/tests/products/mod.rs15
3 files changed, 43 insertions, 24 deletions
diff --git a/crates/rocie-server/src/api/set/barcode.rs b/crates/rocie-server/src/api/set/barcode.rs
index 29eac9e..bb84bbf 100644
--- a/crates/rocie-server/src/api/set/barcode.rs
+++ b/crates/rocie-server/src/api/set/barcode.rs
@@ -28,21 +28,26 @@ use crate::{
         )
     ),
     params(
-        ("id" = BarcodeId, description = "The numeric value of the barcode"),
+        ("barcode_id" = BarcodeId, description = "The numeric value of the barcode"),
+        ("times" = u16, description = "How often to buy the barcode"),
     )
 )]
-#[post("/barcode/{id}/buy")]
+#[post("/barcode/{barcode_id}/buy/{times}")]
 pub(crate) async fn buy_barcode(
     app: web::Data<App>,
-    barcode_id: web::Path<BarcodeIdStub>,
+    path: web::Path<(BarcodeIdStub, u16)>,
 ) -> Result<impl Responder> {
+    let (barcode_id, times) = path.into_inner();
+
     let mut ops = Operations::new("buy barcode unit");
 
-    let barcode = Barcode::from_id(&app, barcode_id.into_inner().into()).await?;
+    let barcode = Barcode::from_id(&app, barcode_id.into()).await?;
 
     match barcode {
         Some(barcode) => {
-            barcode.buy(&mut ops);
+            for _ in 0..times {
+                barcode.buy(&mut ops);
+            }
 
             ops.apply(&app).await?;
 
diff --git a/crates/rocie-server/tests/products/barcode.rs b/crates/rocie-server/tests/products/barcode.rs
index 8e1bf42..5335eb7 100644
--- a/crates/rocie-server/tests/products/barcode.rs
+++ b/crates/rocie-server/tests/products/barcode.rs
@@ -25,7 +25,7 @@ async fn test_barcode_buy() {
     let (unit_id, product_id) =
         create_associated_barcode(&env, "Liter", "Milk", "Volume", unit_value, barcode_id).await;
 
-    request!(env, buy_barcode(BarcodeId { value: barcode_id }));
+    request!(env, buy_barcode(BarcodeId { value: barcode_id }, 1));
 
     let product_amount = request!(env, amount_by_id(product_id));
 
@@ -35,6 +35,25 @@ async fn test_barcode_buy() {
 }
 
 #[tokio::test]
+async fn test_barcode_buy_multiple() {
+    let env = TestEnv::new(function_name!());
+
+    let barcode_id = 23;
+    let unit_value = 1;
+
+    let (unit_id, product_id) =
+        create_associated_barcode(&env, "Liter", "Milk", "Volume", unit_value, barcode_id).await;
+
+    request!(env, buy_barcode(BarcodeId { value: barcode_id }, 5));
+
+    let product_amount = request!(env, amount_by_id(product_id));
+
+    assert_eq!(product_amount.product_id, product_id);
+    assert_eq!(product_amount.amount.unit, unit_id);
+    assert_eq!(product_amount.amount.value, unit_value * 5);
+}
+
+#[tokio::test]
 async fn test_barcode_consume() {
     let env = TestEnv::new(function_name!());
 
@@ -51,7 +70,7 @@ async fn test_barcode_consume() {
     )
     .await;
 
-    request!(env, buy_barcode(barcode_id));
+    request!(env, buy_barcode(barcode_id, 1));
 
     request!(
         env,
@@ -88,7 +107,7 @@ async fn test_barcode_consume_error() {
     )
     .await;
 
-    request!(env, buy_barcode(barcode_id));
+    request!(env, buy_barcode(barcode_id, 1));
 
     request!(
         @expect_error "We should not be able to consume more than available."
@@ -127,7 +146,7 @@ async fn test_barcode_consume_error_other() {
     )
     .await;
 
-    request!(env, buy_barcode(barcode_id));
+    request!(env, buy_barcode(barcode_id, 1));
 
     request!(
         env,
@@ -177,9 +196,9 @@ async fn test_barcode_multiple_buy_and_consume() {
     )
     .await;
 
-    request!(env, buy_barcode(barcode_id));
+    request!(env, buy_barcode(barcode_id, 1));
 
-    request!(env, buy_barcode(barcode_id));
+    request!(env, buy_barcode(barcode_id, 1));
 
     env.log("Bought both barcodes");
 
@@ -228,9 +247,9 @@ async fn test_barcode_fill_up() {
     let _ = create_associated_barcode(&env, "Kilogram", "Bread", "Mass", 2, bread_id.value).await;
     let _ = create_associated_barcode(&env, "Piece", "Nut", "Quantity", 2, nuts_id.value).await;
 
-    request!(env, buy_barcode(milk_id));
-    request!(env, buy_barcode(bread_id));
-    request!(env, buy_barcode(nuts_id));
+    request!(env, buy_barcode(milk_id, 1));
+    request!(env, buy_barcode(bread_id, 1));
+    request!(env, buy_barcode(nuts_id, 1));
 }
 
 #[tokio::test]
diff --git a/crates/rocie-server/tests/products/mod.rs b/crates/rocie-server/tests/products/mod.rs
index b39a07c..886e5b7 100644
--- a/crates/rocie-server/tests/products/mod.rs
+++ b/crates/rocie-server/tests/products/mod.rs
@@ -13,14 +13,11 @@ use rocie_client::{
 use crate::testenv::{TestEnv, log::request};
 
 mod barcode;
+mod query;
 mod register;
 
-async fn create_product(
-    env: &TestEnv,
-    unit_property: UnitPropertyId,
-    name: &str,
-) -> (ProductId, UnitPropertyId) {
-    let product_id = request!(
+async fn create_product(env: &TestEnv, unit_property: UnitPropertyId, name: &str) -> ProductId {
+    request!(
         env,
         register_product(ProductStub {
             description: Some(None),
@@ -28,9 +25,7 @@ async fn create_product(
             parent: None,
             unit_property
         })
-    );
-
-    (product_id, unit_property)
+    )
 }
 async fn create_unit(env: &TestEnv, name: &str, unit_property: UnitPropertyId) -> UnitId {
     request!(
@@ -61,7 +56,7 @@ async fn create_associated_barcode(
         })
     );
 
-    let (product_id, unit_property) = create_product(env, unit_property, product_name).await;
+    let product_id = create_product(env, unit_property, product_name).await;
     let unit_id = create_unit(env, unit_name, unit_property).await;
     let barcode_id = BarcodeId { value: barcode_id };