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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// rocie - An enterprise grocery management system
//
// Copyright (C) 2026 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>.
use crate::cli::{BarcodeCommand, ProductCommand, UnitCommand, UnitPropertyCommand, UserCommand};
use anyhow::{Context, Result};
use rocie_client::{
apis::{
api_get_auth_inventory_api::amount_by_id,
api_get_auth_product_api::{product_by_id, products_registered},
api_get_auth_unit_api::{unit_by_id, units},
api_get_auth_unit_property_api::{unit_properties, unit_property_by_id},
api_get_auth_user_api::users,
api_set_auth_barcode_api::{buy_barcode, consume_barcode},
api_set_auth_product_api::{associate_barcode, register_product},
api_set_auth_unit_api::register_unit,
api_set_auth_unit_property_api::register_unit_property,
configuration::Configuration,
},
models::{
Barcode, BarcodeId, ProductId, ProductParentId, UnitAmount, UnitId, UnitPropertyId,
UnitPropertyStub, UnitStub,
},
};
pub(crate) async fn user(config: &Configuration, command: UserCommand) -> Result<()> {
match command {
UserCommand::List {} => {
users(config).await?;
}
}
Ok(())
}
#[expect(clippy::too_many_lines)]
pub(crate) async fn product(config: &Configuration, command: ProductCommand) -> Result<()> {
match command {
ProductCommand::Register {
name,
description,
parent,
unit_property,
} => {
let new_id = register_product(
config,
rocie_client::models::ProductStub {
description,
name,
unit_property: UnitPropertyId {
value: unit_property,
},
parent: parent.map(|p| ProductParentId { value: p }),
},
)
.await
.context("Failed to register new product")?;
println!("Registered new product with id: {new_id}");
}
ProductCommand::Get { id } => {
let product = product_by_id(config, ProductId { value: id })
.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,
ProductId { value: product_id },
Barcode {
id: BarcodeId {
value: barcode_number,
},
amount: UnitAmount {
unit: UnitId {
value: amount_unit_id,
},
value: amount_value,
},
},
)
.await
.context("Failed to associated barcode")?;
let unit = unit_by_id(
config,
UnitId {
value: amount_unit_id,
},
)
.await?;
let product = product_by_id(config, ProductId { value: product_id }).await?;
println!(
"Associated barcode ({barcode_number} - {amount_value} {}) with product: {} ",
unit.short_name, product.name
);
}
ProductCommand::List {} => {
let all = products_registered(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).await.with_context(|| {
format!("Failed to get amount of product: {}", product.id)
})?;
let unit = unit_by_id(config, product_amount.amount.unit).await?;
print!(
" available: {} {}",
product_amount.amount.value, unit.short_name
);
}
if let Some(description) = product.description {
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).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, times } => {
buy_barcode(config, BarcodeId { value: id }, u32::from(times)).await?;
}
BarcodeCommand::Consume {
id,
amount,
unit_id,
} => {
consume_barcode(
config,
BarcodeId { value: id },
UnitAmount {
unit: UnitId { value: unit_id },
value: 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,
unit_property,
} => {
let new_id = register_unit(
config,
UnitStub {
description,
full_name_plural,
full_name_singular,
short_name,
unit_property: UnitPropertyId {
value: unit_property,
},
},
)
.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 units")?;
for unit in all {
print!("{}: {}", unit.full_name_singular, unit.id);
if let Some(description) = unit.description {
println!(" ({description})");
} else {
println!();
}
}
}
UnitCommand::GetById { id } => {
let unit = unit_by_id(config, UnitId { value: id })
.await
.context("Failed to find unit")?;
println!(
"Unit: {} ({},{},{})",
unit.id, unit.full_name_singular, unit.full_name_plural, unit.short_name
);
}
}
Ok(())
}
pub(crate) async fn unit_property(
config: &Configuration,
command: UnitPropertyCommand,
) -> Result<()> {
match command {
UnitPropertyCommand::Register { name, description } => {
let new_id = register_unit_property(config, UnitPropertyStub { description, name })
.await
.context("Failed to register unit property")?;
println!("Registered new unit property with id: {new_id}");
}
UnitPropertyCommand::List {} => {
let all = unit_properties(config)
.await
.context("Failed to get all unit properties")?;
for unit_prop in all {
print!("{}: {}", unit_prop.name, unit_prop.id);
if let Some(description) = unit_prop.description {
println!(" ({description})");
} else {
println!();
}
}
}
UnitPropertyCommand::GetById { id } => {
let unit = unit_property_by_id(config, UnitPropertyId { value: id })
.await
.context("Failed to find unit property")?;
println!("Unit property: {} ({})", unit.id, unit.name);
}
}
Ok(())
}
|