about summary refs log tree commit diff stats
path: root/crates/rocie-cli/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-cli/src')
-rw-r--r--crates/rocie-cli/src/cli.rs63
-rw-r--r--crates/rocie-cli/src/main.rs83
2 files changed, 146 insertions, 0 deletions
diff --git a/crates/rocie-cli/src/cli.rs b/crates/rocie-cli/src/cli.rs
new file mode 100644
index 0000000..ac220d5
--- /dev/null
+++ b/crates/rocie-cli/src/cli.rs
@@ -0,0 +1,63 @@
+use clap::{Parser, Subcommand};
+use uuid::Uuid;
+
+#[derive(Parser)]
+pub(crate) struct CliArgs {
+    #[command(subcommand)]
+    pub(crate) command: Command,
+}
+
+#[derive(Subcommand)]
+pub(crate) enum Command {
+    /// Deal with products
+    Product {
+        #[command(subcommand)]
+        command: ProductCommand,
+    },
+}
+
+#[derive(Subcommand)]
+pub(crate) enum ProductCommand {
+    /// Register a new product
+    Register {
+        /// The name of new the product
+        #[arg(short, long)]
+        name: String,
+
+        /// Optional description of the new the product
+        #[arg(short, long)]
+        description: Option<String>,
+
+        /// Optional parent of the new the product
+        #[arg(short, long)]
+        parent: Option<Uuid>,
+    },
+
+    AssociateBarcode {
+        /// The id of the product to associated the barcode with
+        #[arg(short, long)]
+        product_id: Uuid,
+
+        /// The barcode number
+        #[arg(short, long)]
+        barcode_number: u32,
+
+        /// The quantity of amount, this barcode signifies
+        #[arg(short = 'v', long)]
+        amount_value: u32,
+
+        /// The unit the amount value is in
+        #[arg(short = 'u', long)]
+        amount_unit: String,
+    },
+
+    /// Get a already registered product by id
+    Get {
+        /// The id of the product
+        #[arg(short, long)]
+        id: Uuid,
+    },
+
+    /// List all available products
+    List {},
+}
diff --git a/crates/rocie-cli/src/main.rs b/crates/rocie-cli/src/main.rs
new file mode 100644
index 0000000..f0192d4
--- /dev/null
+++ b/crates/rocie-cli/src/main.rs
@@ -0,0 +1,83 @@
+use anyhow::{Context, 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 crate::cli::{CliArgs, Command, ProductCommand};
+
+mod cli;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+    let args = CliArgs::parse();
+
+    let mut config = Configuration::new();
+    "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);
+                    }
+                }
+            }
+        }
+    }
+
+    Ok(())
+}