about summary refs log tree commit diff stats
path: root/crates/rocie-cli/src/handle/mod.rs
blob: 1d322f879f3c26e649e7f984762bd9651936d0a4 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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(())
}