about summary refs log tree commit diff stats
path: root/crates/rocie-cli
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-06 10:31:40 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-06 10:31:40 +0200
commit9a9d5c5880095adeb43a045dca638243c8f946e4 (patch)
tree86e0d23af339b3139efab15749aaf5b59aa0965b /crates/rocie-cli
parentchore: Initial commit (diff)
downloadserver-9a9d5c5880095adeb43a045dca638243c8f946e4.zip
feat: Provide basic API frame
Diffstat (limited to 'crates/rocie-cli')
-rw-r--r--crates/rocie-cli/Cargo.toml38
-rw-r--r--crates/rocie-cli/src/cli.rs63
-rw-r--r--crates/rocie-cli/src/main.rs83
3 files changed, 184 insertions, 0 deletions
diff --git a/crates/rocie-cli/Cargo.toml b/crates/rocie-cli/Cargo.toml
new file mode 100644
index 0000000..e68a7b4
--- /dev/null
+++ b/crates/rocie-cli/Cargo.toml
@@ -0,0 +1,38 @@
+# rocie - An enterprise grocery management system
+#
+# Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This file is part of Rocie.
+#
+# You should have received a copy of the License along with this program.
+# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+[package]
+name = "rocie-cli"
+keywords = []
+categories = []
+default-run = "rocie-cli"
+version.workspace = true
+edition.workspace = true
+authors.workspace = true
+license.workspace = true
+repository.workspace = true
+rust-version.workspace = true
+description.workspace = true
+publish = false
+
+[lints]
+workspace = true
+
+[dependencies]
+anyhow = "1.0.99"
+clap = { version = "4.5.47", features = ["derive"] }
+rocie-client.workspace = true
+tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] }
+uuid = "1.18.1"
+
+
+[package.metadata.docs.rs]
+all-features = true
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(())
+}