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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
use crate::cli::{BarcodeCommand, ProductCommand, UnitCommand};
use anyhow::{Context, Result};
use rocie_client::{
apis::{
api_get_inventory_api::amount_by_id,
api_get_product_api::{product_by_id, products},
api_get_unit_api::{unit_by_id, units},
api_set_barcode_api::{buy_barcode, consume_barcode},
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: i32::try_from(barcode_number).unwrap(),
amount: Box::new(UnitAmount {
unit: amount_unit_id,
value: i64::from(amount_value),
}),
},
)
.await
.context("Failed to associated barcode")?;
let unit = unit_by_id(config, amount_unit_id.to_string().as_str()).await?;
let product = product_by_id(config, product_id.to_string().as_str()).await?;
println!(
"Associated barcode ({barcode_number} - {amount_value} {}) with product: {} ",
unit.short_name, product.name
);
}
ProductCommand::List {} => {
let all = products(config)
.await
.context("Failed to get all products")?;
for product in all {
print!("{}: {}", product.name, product.id);
{
let product_amount = amount_by_id(config, product.id.to_string().as_str())
.await
.with_context(|| {
format!("Failed to get amount of product: {}", product.id)
})?;
let unit =
unit_by_id(config, product_amount.amount.unit.to_string().as_str()).await?;
print!(" available: {} {}", product_amount.amount.value, unit.short_name);
}
if let Some(description) = product
.description
.expect("Superflous Option wrapping in api")
{
println!(" ({description})");
} else {
println!();
}
if !product.associated_bar_codes.is_empty() {
println!(" Barcodes:");
}
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 barcode(config: &Configuration, command: BarcodeCommand) -> Result<()> {
match command {
BarcodeCommand::Buy { id } => {
buy_barcode(config, i32::try_from(id).unwrap()).await?;
}
BarcodeCommand::Consume {
id,
amount,
unit_id,
} => {
consume_barcode(
config,
i32::try_from(id).unwrap(),
UnitAmount {
unit: unit_id,
value: i64::from(amount),
},
)
.await?;
}
}
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(())
}
|