diff options
Diffstat (limited to 'crates/rocie-cli')
| -rw-r--r-- | crates/rocie-cli/src/handle/mod.rs | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/crates/rocie-cli/src/handle/mod.rs b/crates/rocie-cli/src/handle/mod.rs index c39f7b1..fc6f137 100644 --- a/crates/rocie-cli/src/handle/mod.rs +++ b/crates/rocie-cli/src/handle/mod.rs @@ -11,7 +11,7 @@ use rocie_client::{ api_set_unit_api::register_unit, configuration::Configuration, }, - models::{Barcode, UnitAmount, UnitStub}, + models::{Barcode, BarcodeId, ProductId, UnitAmount, UnitId, UnitStub}, }; pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> Result<()> { @@ -26,7 +26,7 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> rocie_client::models::ProductStub { description: Some(description), // TODO: Fix the duplicate option name, - parent, + parent: Some(parent.map(|p| ProductId { value: p })), }, ) .await @@ -36,7 +36,7 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> } ProductCommand::Get { id } => { - let product = product_by_id(config, id.to_string().as_str()) + let product = product_by_id(config, ProductId { value: id }) .await .with_context(|| format!("Failed to get product with id: {id}"))?; @@ -51,20 +51,30 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> } => { associate_barcode( config, - product_id.to_string().as_str(), + ProductId { value: product_id }, Barcode { - id: i32::try_from(barcode_number).unwrap(), - amount: Box::new(UnitAmount { - unit: amount_unit_id, - value: i64::from(amount_value), - }), + id: BarcodeId { + value: barcode_number, + }, + amount: UnitAmount { + unit: UnitId { + value: amount_unit_id, + }, + value: 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?; + let unit = unit_by_id( + config, + UnitId { + value: amount_unit_id, + }, + ) + .await?; + let product = product_by_id(config, ProductId { value: product_id }).await?; println!( "Associated barcode ({barcode_number} - {amount_value} {}) with product: {} ", @@ -81,16 +91,17 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> print!("{}: {}", product.name, product.id); { - let product_amount = amount_by_id(config, product.id.to_string().as_str()) - .await - .with_context(|| { + let product_amount = + amount_by_id(config, product.id).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?; + let unit = unit_by_id(config, product_amount.amount.unit).await?; - print!(" available: {} {}", product_amount.amount.value, unit.short_name); + print!( + " available: {} {}", + product_amount.amount.value, unit.short_name + ); } if let Some(description) = product @@ -107,7 +118,7 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> } for barcode in product.associated_bar_codes { - let unit = unit_by_id(config, barcode.amount.unit.to_string().as_str()).await?; + let unit = unit_by_id(config, barcode.amount.unit).await?; println!( " - {}: {} {}", @@ -129,7 +140,7 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> pub(crate) async fn barcode(config: &Configuration, command: BarcodeCommand) -> Result<()> { match command { BarcodeCommand::Buy { id } => { - buy_barcode(config, i32::try_from(id).unwrap()).await?; + buy_barcode(config, BarcodeId { value: id }).await?; } BarcodeCommand::Consume { id, @@ -138,10 +149,10 @@ pub(crate) async fn barcode(config: &Configuration, command: BarcodeCommand) -> } => { consume_barcode( config, - i32::try_from(id).unwrap(), + BarcodeId { value: id }, UnitAmount { - unit: unit_id, - value: i64::from(amount), + unit: UnitId { value: unit_id }, + value: amount, }, ) .await?; @@ -188,7 +199,7 @@ pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result } } UnitCommand::GetById { id } => { - let unit = unit_by_id(config, id.to_string().as_str()) + let unit = unit_by_id(config, UnitId { value: id }) .await .context("Failed to find unit")?; println!( |
