From 2dc74d621399be454abbbff892fb46204ddc6e7b Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 23 Sep 2025 08:33:06 +0200 Subject: feat(treewide): Add tests and barcode buying/consuming --- crates/rocie-server/src/api/set/barcode.rs | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 crates/rocie-server/src/api/set/barcode.rs (limited to 'crates/rocie-server/src/api/set/barcode.rs') 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, + barcode_id: web::Path, +) -> Result { + 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, + barcode_id: web::Path, + unit_amount: web::Json, +) -> Result { + 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()), + } +} -- cgit 1.4.1