about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/set/barcode.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-23 08:33:06 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-23 08:34:45 +0200
commit2dc74d621399be454abbbff892fb46204ddc6e7b (patch)
treef9525527fc09c465d4e2e4a4f665bfd444b889f8 /crates/rocie-server/src/api/set/barcode.rs
parentfeat: Provide basic barcode handling support (diff)
downloadserver-2dc74d621399be454abbbff892fb46204ddc6e7b.zip
feat(treewide): Add tests and barcode buying/consuming
Diffstat (limited to 'crates/rocie-server/src/api/set/barcode.rs')
-rw-r--r--crates/rocie-server/src/api/set/barcode.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/rocie-server/src/api/set/barcode.rs b/crates/rocie-server/src/api/set/barcode.rs
new file mode 100644
index 0000000..a89bf4f
--- /dev/null
+++ b/crates/rocie-server/src/api/set/barcode.rs
@@ -0,0 +1,97 @@
+use actix_web::{HttpResponse, Responder, Result, post, web};
+use log::debug;
+
+use crate::{
+    app::App,
+    storage::sql::{
+        barcode::{Barcode, BarcodeId, UnitAmount},
+        insert::Operations,
+    },
+};
+
+/// Buy an barcode
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Barcode successfully bought",
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Barcode id was not found",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String,
+        )
+    ),
+    params(
+        ("id" = BarcodeId, description = "The numeric value of the barcode"),
+    )
+)]
+#[post("/barcode/{id}/buy")]
+pub(crate) async fn buy_barcode(
+    app: web::Data<App>,
+    barcode_id: web::Path<BarcodeId>,
+) -> Result<impl Responder> {
+    let mut ops = Operations::new("buy barcode unit");
+
+    let barcode = Barcode::from_id(&app, barcode_id.into_inner()).await?;
+
+    match barcode {
+        Some(barcode) => {
+            barcode.buy(&mut ops);
+
+            ops.apply(&app).await?;
+
+            Ok(HttpResponse::Ok().finish())
+        }
+        None => Ok(HttpResponse::NotFound().finish()),
+    }
+}
+
+/// Consume an barcode
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Barcode successfully consumed",
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Barcode id was not found",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String,
+        )
+    ),
+    params(
+        ("id" = BarcodeId, description = "The numeric value of the barcode"),
+    ),
+    request_body = UnitAmount,
+)]
+#[post("/barcode/{id}/consume")]
+pub(crate) async fn consume_barcode(
+    app: web::Data<App>,
+    barcode_id: web::Path<BarcodeId>,
+    unit_amount: web::Json<UnitAmount>,
+) -> Result<impl Responder> {
+    let mut ops = Operations::new("consume barcode unit");
+
+    let barcode = Barcode::from_id(&app, barcode_id.into_inner()).await?;
+    debug!("Starting consume for barcode: {barcode:?}");
+
+    match barcode {
+        Some(barcode) => {
+            barcode.consume(&app, unit_amount.into_inner(), &mut ops).await?;
+
+            ops.apply(&app).await?;
+
+            Ok(HttpResponse::Ok().finish())
+        }
+        None => Ok(HttpResponse::NotFound().finish()),
+    }
+}