diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-23 08:33:06 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-09-23 08:34:45 +0200 |
| commit | 2dc74d621399be454abbbff892fb46204ddc6e7b (patch) | |
| tree | f9525527fc09c465d4e2e4a4f665bfd444b889f8 /crates/rocie-cli | |
| parent | feat: Provide basic barcode handling support (diff) | |
| download | server-2dc74d621399be454abbbff892fb46204ddc6e7b.zip | |
feat(treewide): Add tests and barcode buying/consuming
Diffstat (limited to 'crates/rocie-cli')
| -rw-r--r-- | crates/rocie-cli/Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/rocie-cli/src/cli.rs | 32 | ||||
| -rw-r--r-- | crates/rocie-cli/src/handle/mod.rs | 84 | ||||
| -rw-r--r-- | crates/rocie-cli/src/main.rs | 1 |
4 files changed, 103 insertions, 16 deletions
diff --git a/crates/rocie-cli/Cargo.toml b/crates/rocie-cli/Cargo.toml index e68a7b4..5e878fd 100644 --- a/crates/rocie-cli/Cargo.toml +++ b/crates/rocie-cli/Cargo.toml @@ -30,7 +30,7 @@ workspace = true anyhow = "1.0.99" clap = { version = "4.5.47", features = ["derive"] } rocie-client.workspace = true -tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] } +tokio.workspace = true uuid = "1.18.1" diff --git a/crates/rocie-cli/src/cli.rs b/crates/rocie-cli/src/cli.rs index 3f2662b..29c284f 100644 --- a/crates/rocie-cli/src/cli.rs +++ b/crates/rocie-cli/src/cli.rs @@ -20,6 +20,12 @@ pub(crate) enum Command { #[command(subcommand)] command: UnitCommand, }, + + /// Deal with Barcodes + Barcode { + #[command(subcommand)] + command: BarcodeCommand, + }, } #[derive(Subcommand)] @@ -58,6 +64,32 @@ pub(crate) enum UnitCommand { } #[derive(Subcommand)] +#[expect(variant_size_differences, reason = "It's just a testing cli")] +pub(crate) enum BarcodeCommand { + /// Buy an barcode + Buy { + /// The numeric value of the barcode + #[arg(short, long)] + id: u32, + }, + + /// Consume an barcode + Consume { + /// The numeric value of the barcode + #[arg(short, long)] + id: u32, + + /// The amount to consume + #[arg(short, long)] + amount: u32, + + /// The unit of the consumed amount + #[arg(short, long)] + unit_id: Uuid, + }, +} + +#[derive(Subcommand)] pub(crate) enum ProductCommand { /// Register a new product Register { diff --git a/crates/rocie-cli/src/handle/mod.rs b/crates/rocie-cli/src/handle/mod.rs index 1d322f8..c39f7b1 100644 --- a/crates/rocie-cli/src/handle/mod.rs +++ b/crates/rocie-cli/src/handle/mod.rs @@ -1,10 +1,12 @@ -use crate::cli::{ProductCommand, UnitCommand}; +use crate::cli::{BarcodeCommand, ProductCommand, UnitCommand}; use anyhow::{Context, Result}; use rocie_client::{ apis::{ + api_get_inventory_api::amount_by_id, api_get_product_api::{product_by_id, products}, api_get_unit_api::{unit_by_id, units}, + api_set_barcode_api::{buy_barcode, consume_barcode}, api_set_product_api::{associate_barcode, register_product}, api_set_unit_api::register_unit, configuration::Configuration, @@ -46,19 +48,29 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> barcode_number, amount_value, amount_unit_id, - } => associate_barcode( - config, - product_id.to_string().as_str(), - Barcode { - id: i64::from(barcode_number), - amount: Box::new(UnitAmount { - unit: amount_unit_id, - value: i64::from(amount_value), - }), - }, - ) - .await - .context("Failed to associated barcode")?, + } => { + associate_barcode( + config, + product_id.to_string().as_str(), + Barcode { + id: i32::try_from(barcode_number).unwrap(), + amount: Box::new(UnitAmount { + unit: amount_unit_id, + value: i64::from(amount_value), + }), + }, + ) + .await + .context("Failed to associated barcode")?; + + let unit = unit_by_id(config, amount_unit_id.to_string().as_str()).await?; + let product = product_by_id(config, product_id.to_string().as_str()).await?; + + println!( + "Associated barcode ({barcode_number} - {amount_value} {}) with product: {} ", + unit.short_name, product.name + ); + } ProductCommand::List {} => { let all = products(config) @@ -68,6 +80,19 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> for product in all { print!("{}: {}", product.name, product.id); + { + let product_amount = amount_by_id(config, product.id.to_string().as_str()) + .await + .with_context(|| { + format!("Failed to get amount of product: {}", product.id) + })?; + + let unit = + unit_by_id(config, product_amount.amount.unit.to_string().as_str()).await?; + + print!(" available: {} {}", product_amount.amount.value, unit.short_name); + } + if let Some(description) = product .description .expect("Superflous Option wrapping in api") @@ -77,11 +102,15 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> println!(); } + if !product.associated_bar_codes.is_empty() { + println!(" Barcodes:"); + } + for barcode in product.associated_bar_codes { let unit = unit_by_id(config, barcode.amount.unit.to_string().as_str()).await?; println!( - " - {}: {} {}", + " - {}: {} {}", barcode.id, barcode.amount.value, if barcode.amount.value == 1 { @@ -97,6 +126,31 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> Ok(()) } +pub(crate) async fn barcode(config: &Configuration, command: BarcodeCommand) -> Result<()> { + match command { + BarcodeCommand::Buy { id } => { + buy_barcode(config, i32::try_from(id).unwrap()).await?; + } + BarcodeCommand::Consume { + id, + amount, + unit_id, + } => { + consume_barcode( + config, + i32::try_from(id).unwrap(), + UnitAmount { + unit: unit_id, + value: i64::from(amount), + }, + ) + .await?; + } + } + + Ok(()) +} + pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result<()> { match command { UnitCommand::Register { diff --git a/crates/rocie-cli/src/main.rs b/crates/rocie-cli/src/main.rs index ef81ad9..e3188c8 100644 --- a/crates/rocie-cli/src/main.rs +++ b/crates/rocie-cli/src/main.rs @@ -17,6 +17,7 @@ async fn main() -> Result<()> { match args.command { Command::Product { command } => handle::product(&config, command).await?, Command::Unit { command } => handle::unit(&config, command).await?, + Command::Barcode { command } => handle::barcode(&config, command).await?, } Ok(()) |
