diff options
Diffstat (limited to 'crates/rocie-cli/src')
| -rw-r--r-- | crates/rocie-cli/src/cli.rs | 49 | ||||
| -rw-r--r-- | crates/rocie-cli/src/handle/mod.rs | 148 | ||||
| -rw-r--r-- | crates/rocie-cli/src/main.rs | 72 |
3 files changed, 199 insertions, 70 deletions
diff --git a/crates/rocie-cli/src/cli.rs b/crates/rocie-cli/src/cli.rs index ac220d5..3f2662b 100644 --- a/crates/rocie-cli/src/cli.rs +++ b/crates/rocie-cli/src/cli.rs @@ -14,21 +14,62 @@ pub(crate) enum Command { #[command(subcommand)] command: ProductCommand, }, + + /// Deal with products + Unit { + #[command(subcommand)] + command: UnitCommand, + }, +} + +#[derive(Subcommand)] +pub(crate) enum UnitCommand { + /// Register a new unit + Register { + /// The full singular name of the new unit + /// E.g.: 1 kilogram + #[arg(short = 's', long)] + full_name_singular: String, + + /// The full plural name of the new unit + /// E.g.: 5 kilograms + #[arg(short = 'p', long)] + full_name_plural: String, + + /// The short name of the new unit (short names have no plural) + /// E.g.: 1 kg or 5 kg + #[arg(short = 'n', long)] + short_name: String, + + /// Optional description of the new unit + #[arg(short, long)] + description: Option<String>, + }, + + /// Fetch an unit based on id + GetById { + /// The id of the unit + #[arg(short, long)] + id: Uuid, + }, + + /// List all available units + List {}, } #[derive(Subcommand)] pub(crate) enum ProductCommand { /// Register a new product Register { - /// The name of new the product + /// The name of the new product #[arg(short, long)] name: String, - /// Optional description of the new the product + /// Optional description of the new product #[arg(short, long)] description: Option<String>, - /// Optional parent of the new the product + /// Optional parent of the new product #[arg(short, long)] parent: Option<Uuid>, }, @@ -48,7 +89,7 @@ pub(crate) enum ProductCommand { /// The unit the amount value is in #[arg(short = 'u', long)] - amount_unit: String, + amount_unit_id: Uuid, }, /// Get a already registered product by id diff --git a/crates/rocie-cli/src/handle/mod.rs b/crates/rocie-cli/src/handle/mod.rs new file mode 100644 index 0000000..1d322f8 --- /dev/null +++ b/crates/rocie-cli/src/handle/mod.rs @@ -0,0 +1,148 @@ +use crate::cli::{ProductCommand, UnitCommand}; + +use anyhow::{Context, Result}; +use rocie_client::{ + apis::{ + api_get_product_api::{product_by_id, products}, + api_get_unit_api::{unit_by_id, units}, + api_set_product_api::{associate_barcode, register_product}, + api_set_unit_api::register_unit, + configuration::Configuration, + }, + models::{Barcode, UnitAmount, UnitStub}, +}; + +pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> Result<()> { + match command { + ProductCommand::Register { + name, + description, + parent, + } => { + let new_id = register_product( + config, + rocie_client::models::ProductStub { + description: Some(description), // TODO: Fix the duplicate option + name, + parent, + }, + ) + .await + .context("Failed to register new product")?; + + println!("Registered new product with id: {new_id}"); + } + + ProductCommand::Get { id } => { + let product = product_by_id(config, id.to_string().as_str()) + .await + .with_context(|| format!("Failed to get product with id: {id}"))?; + + println!("{product:#?}"); + } + + ProductCommand::AssociateBarcode { + product_id, + 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")?, + + ProductCommand::List {} => { + let all = products(config) + .await + .context("Failed to get all products")?; + + for product in all { + print!("{}: {}", product.name, product.id); + + if let Some(description) = product + .description + .expect("Superflous Option wrapping in api") + { + println!(" ({description})"); + } else { + println!(); + } + + 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 { + unit.full_name_singular + } else { + unit.full_name_plural + } + ); + } + } + } + } + Ok(()) +} + +pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result<()> { + match command { + UnitCommand::Register { + full_name_singular, + full_name_plural, + short_name, + description, + } => { + let new_id = register_unit( + config, + UnitStub { + description: Some(description), + full_name_plural, + full_name_singular, + short_name, + }, + ) + .await + .context("Failed to register unit")?; + println!("Registered new unit with id: {new_id}"); + } + UnitCommand::List {} => { + let all = units(config).await.context("Failed to get all products")?; + + for unit in all { + print!("{}: {}", unit.full_name_singular, unit.id); + + if let Some(description) = + unit.description.expect("Superflous Option wrapping in api") + { + println!(" ({description})"); + } else { + println!(); + } + } + } + UnitCommand::GetById { id } => { + let unit = unit_by_id(config, id.to_string().as_str()) + .await + .context("Failed to find unit")?; + println!( + "Unit: {} ({},{},{})", + unit.id, unit.full_name_singular, unit.full_name_plural, unit.short_name + ); + } + } + + Ok(()) +} diff --git a/crates/rocie-cli/src/main.rs b/crates/rocie-cli/src/main.rs index f0192d4..ef81ad9 100644 --- a/crates/rocie-cli/src/main.rs +++ b/crates/rocie-cli/src/main.rs @@ -1,17 +1,11 @@ -use anyhow::{Context, Result}; +use anyhow::Result; use clap::Parser; -use rocie_client::{ - apis::{ - api_get_api::{product_by_id, products}, - api_set_api::{associate_barcode, register_product}, - configuration::Configuration, - }, - models::{Barcode, UnitAmount}, -}; +use rocie_client::apis::configuration::Configuration; -use crate::cli::{CliArgs, Command, ProductCommand}; +use crate::cli::{CliArgs, Command}; mod cli; +mod handle; #[tokio::main] async fn main() -> Result<()> { @@ -21,62 +15,8 @@ async fn main() -> Result<()> { "http://127.0.0.1:8080".clone_into(&mut config.base_path); match args.command { - Command::Product { command } => { - match command { - ProductCommand::Register { - name, - description, - parent, - } => { - let new_id = register_product( - &config, - rocie_client::models::ProductStub { - description: Some(description), // TODO: Fix - name, - parent, - }, - ) - .await - .context("Failed to register new product")?; - - println!("Registered new product with id: {new_id}"); - } - ProductCommand::Get { id } => { - let product = product_by_id(&config, id.to_string().as_str()) - .await - .with_context(|| format!("Failed to get product with id: {id}"))?; - - println!("{product:#?}"); - } - ProductCommand::AssociateBarcode { - product_id, - barcode_number, - amount_value, - amount_unit, - } => associate_barcode( - &config, - product_id.to_string().as_str(), - Barcode { - amount: Box::new(UnitAmount { - unit: amount_unit, - value: amount_value as i32, - }), - id: barcode_number as i32, - }, - ) - .await - .context("Failed to associated barcode")?, - ProductCommand::List {} => { - let all = products(&config) - .await - .context("Failed to get all products")?; - - for product in all { - println!("{}: {}", product.name, product.id); - } - } - } - } + Command::Product { command } => handle::product(&config, command).await?, + Command::Unit { command } => handle::unit(&config, command).await?, } Ok(()) |
