about summary refs log tree commit diff stats
path: root/crates/rocie-cli
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-cli')
-rw-r--r--crates/rocie-cli/src/cli.rs44
-rw-r--r--crates/rocie-cli/src/handle/mod.rs65
-rw-r--r--crates/rocie-cli/src/main.rs1
3 files changed, 106 insertions, 4 deletions
diff --git a/crates/rocie-cli/src/cli.rs b/crates/rocie-cli/src/cli.rs
index 29c284f..a84ab0e 100644
--- a/crates/rocie-cli/src/cli.rs
+++ b/crates/rocie-cli/src/cli.rs
@@ -15,12 +15,18 @@ pub(crate) enum Command {
         command: ProductCommand,
     },
 
-    /// Deal with products
+    /// Deal with units
     Unit {
         #[command(subcommand)]
         command: UnitCommand,
     },
 
+    /// Deal with unit properties
+    UnitProperty {
+        #[command(subcommand)]
+        command: UnitPropertyCommand,
+    },
+
     /// Deal with Barcodes
     Barcode {
         #[command(subcommand)]
@@ -50,6 +56,10 @@ pub(crate) enum UnitCommand {
         /// Optional description of the new unit
         #[arg(short, long)]
         description: Option<String>,
+
+        /// The id of the unit property this unit measures.
+        #[arg(short, long)]
+        unit_property: Uuid,
     },
 
     /// Fetch an unit based on id
@@ -64,6 +74,34 @@ pub(crate) enum UnitCommand {
 }
 
 #[derive(Subcommand)]
+pub(crate) enum UnitPropertyCommand {
+    /// Register a new unit
+    Register {
+        /// The name of the new unit property
+        ///
+        /// E.g.:
+        ///     - Mass
+        ///     - Volume
+        #[arg(short = 'n', long)]
+        name: String,
+
+        /// Optional description of the new unit property
+        #[arg(short, long)]
+        description: Option<String>,
+    },
+
+    /// Fetch an unit property based on id
+    GetById {
+        /// The id of the unit property
+        #[arg(short, long)]
+        id: Uuid,
+    },
+
+    /// List all available unit properties
+    List {},
+}
+
+#[derive(Subcommand)]
 #[expect(variant_size_differences, reason = "It's just a testing cli")]
 pub(crate) enum BarcodeCommand {
     /// Buy an barcode
@@ -104,6 +142,10 @@ pub(crate) enum ProductCommand {
         /// Optional parent of the new product
         #[arg(short, long)]
         parent: Option<Uuid>,
+
+        /// The id of the unit property this product is measured in.
+        #[arg(short, long)]
+        unit_property: Uuid,
     },
 
     AssociateBarcode {
diff --git a/crates/rocie-cli/src/handle/mod.rs b/crates/rocie-cli/src/handle/mod.rs
index fc6f137..101f56d 100644
--- a/crates/rocie-cli/src/handle/mod.rs
+++ b/crates/rocie-cli/src/handle/mod.rs
@@ -1,4 +1,4 @@
-use crate::cli::{BarcodeCommand, ProductCommand, UnitCommand};
+use crate::cli::{BarcodeCommand, ProductCommand, UnitCommand, UnitPropertyCommand};
 
 use anyhow::{Context, Result};
 use rocie_client::{
@@ -6,12 +6,17 @@ use rocie_client::{
         api_get_inventory_api::amount_by_id,
         api_get_product_api::{product_by_id, products},
         api_get_unit_api::{unit_by_id, units},
+        api_get_unit_property_api::{unit_properties, unit_property_by_id},
         api_set_barcode_api::{buy_barcode, consume_barcode},
         api_set_product_api::{associate_barcode, register_product},
         api_set_unit_api::register_unit,
+        api_set_unit_property_api::register_unit_property,
         configuration::Configuration,
     },
-    models::{Barcode, BarcodeId, ProductId, UnitAmount, UnitId, UnitStub},
+    models::{
+        Barcode, BarcodeId, ProductId, UnitAmount, UnitId, UnitPropertyId, UnitPropertyStub,
+        UnitStub,
+    },
 };
 
 pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> Result<()> {
@@ -20,12 +25,16 @@ pub(crate) async fn product(config: &Configuration, command: ProductCommand) ->
             name,
             description,
             parent,
+            unit_property,
         } => {
             let new_id = register_product(
                 config,
                 rocie_client::models::ProductStub {
                     description: Some(description), // TODO: Fix the duplicate option
                     name,
+                    unit_property: UnitPropertyId {
+                        value: unit_property,
+                    },
                     parent: Some(parent.map(|p| ProductId { value: p })),
                 },
             )
@@ -169,6 +178,7 @@ pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result
             full_name_plural,
             short_name,
             description,
+            unit_property,
         } => {
             let new_id = register_unit(
                 config,
@@ -177,6 +187,9 @@ pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result
                     full_name_plural,
                     full_name_singular,
                     short_name,
+                    unit_property: UnitPropertyId {
+                        value: unit_property,
+                    },
                 },
             )
             .await
@@ -184,7 +197,7 @@ pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result
             println!("Registered new unit with id: {new_id}");
         }
         UnitCommand::List {} => {
-            let all = units(config).await.context("Failed to get all products")?;
+            let all = units(config).await.context("Failed to get all units")?;
 
             for unit in all {
                 print!("{}: {}", unit.full_name_singular, unit.id);
@@ -211,3 +224,49 @@ pub(crate) async fn unit(config: &Configuration, command: UnitCommand) -> Result
 
     Ok(())
 }
+
+pub(crate) async fn unit_property(
+    config: &Configuration,
+    command: UnitPropertyCommand,
+) -> Result<()> {
+    match command {
+        UnitPropertyCommand::Register { name, description } => {
+            let new_id = register_unit_property(
+                config,
+                UnitPropertyStub {
+                    description: Some(description),
+                    name,
+                },
+            )
+            .await
+            .context("Failed to register unit property")?;
+            println!("Registered new unit property with id: {new_id}");
+        }
+        UnitPropertyCommand::List {} => {
+            let all = unit_properties(config)
+                .await
+                .context("Failed to get all unit properties")?;
+
+            for unit_prop in all {
+                print!("{}: {}", unit_prop.name, unit_prop.id);
+
+                if let Some(description) = unit_prop
+                    .description
+                    .expect("Superflous Option wrapping in api")
+                {
+                    println!(" ({description})");
+                } else {
+                    println!();
+                }
+            }
+        }
+        UnitPropertyCommand::GetById { id } => {
+            let unit = unit_property_by_id(config, UnitPropertyId { value: id })
+                .await
+                .context("Failed to find unit property")?;
+            println!("Unit property: {} ({})", unit.id, unit.name);
+        }
+    }
+
+    Ok(())
+}
diff --git a/crates/rocie-cli/src/main.rs b/crates/rocie-cli/src/main.rs
index e3188c8..26e0f62 100644
--- a/crates/rocie-cli/src/main.rs
+++ b/crates/rocie-cli/src/main.rs
@@ -18,6 +18,7 @@ async fn main() -> Result<()> {
         Command::Product { command } => handle::product(&config, command).await?,
         Command::Unit { command } => handle::unit(&config, command).await?,
         Command::Barcode { command } => handle::barcode(&config, command).await?,
+        Command::UnitProperty { command } => handle::unit_property(&config, command).await?,
     }
 
     Ok(())