about summary refs log tree commit diff stats
path: root/crates/rocie-cli/src/main.rs
blob: f0192d45f724a7fff520ce4e3e425bbeb951d657 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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(())
}