diff options
Diffstat (limited to 'crates/rocie-cli/src/handle')
| -rw-r--r-- | crates/rocie-cli/src/handle/mod.rs | 84 |
1 files changed, 69 insertions, 15 deletions
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 { |
