summary refs log tree commit diff stats
path: root/src/api/mod.rs
blob: 3879223043847e07f64a234ad9c2b981d1d19472 (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
use leptos::{
    error::Error,
    prelude::{Read, expect_context},
};
use reactive_stores::Store;
use rocie_client::{
    apis::{
        api_get_inventory_api::amount_by_id,
        api_get_product_api::{
            product_by_id, product_by_name, product_suggestion_by_name, products,
        },
        api_get_unit_api::unit_by_id,
        api_get_unit_property_api::unit_property_by_id,
        api_set_barcode_api::buy_barcode, configuration::Configuration,
    },
    models::{
        BarcodeId, Product, ProductAmount, ProductId, Unit, UnitId, UnitProperty, UnitPropertyId,
    },
};

use crate::{ConfigState, ConfigStateStoreFields};

pub(crate) async fn get_amount_by_id(product_id: ProductId) -> Result<ProductAmount, Error> {
    let config = expect_context::<Store<ConfigState>>();
    amount_by_id(&config.config().read(), product_id)
        .await
        .map_err(Into::<Error>::into)
}
pub(crate) async fn get_product_by_id(product_id: ProductId) -> Result<Product, Error> {
    let config = expect_context::<Store<ConfigState>>();
    product_by_id(&config.config().read(), product_id)
        .await
        .map_err(Into::<Error>::into)
}
pub(crate) async fn get_product_by_name(
    name: String,
) -> Result<
    Product,
    rocie_client::apis::Error<rocie_client::apis::api_get_product_api::ProductByNameError>,
> {
    let config = expect_context::<Store<ConfigState>>();
    product_by_name(&config.config().read(), &name).await
}
pub(crate) async fn get_products_by_part_name(part_name: String) -> Result<Vec<Product>, Error> {
    let config = expect_context::<Store<ConfigState>>();
    product_suggestion_by_name(&config.config().read(), &part_name)
        .await
        .map_err(Into::<Error>::into)
}
pub(crate) async fn get_unit_by_id(unit_id: UnitId) -> Result<Unit, Error> {
    let config = expect_context::<Store<ConfigState>>();
    unit_by_id(&config.config().read(), unit_id)
        .await
        .map_err(Into::<Error>::into)
}
pub(crate) async fn get_unit_property_by_id(
    unit_id: UnitPropertyId,
) -> Result<UnitProperty, Error> {
    let config = expect_context::<Store<ConfigState>>();
    unit_property_by_id(&config.config().read(), unit_id)
        .await
        .map_err(Into::<Error>::into)
}

pub(crate) async fn get_full_product_by_id(
    id: ProductId,
) -> Result<(Product, ProductAmount, Unit), Error> {
    let amount = get_amount_by_id(id).await?;
    let product = get_product_by_id(id).await?;
    let unit = get_unit_by_id(amount.amount.unit).await?;

    Ok::<_, Error>((product, amount, unit))
}
pub(crate) async fn get_product_unit_by_id(
    id: ProductId,
) -> Result<(Product, UnitProperty), Error> {
    let product = get_product_by_id(id).await?;
    let unit = get_unit_property_by_id(product.unit_property).await?;

    Ok::<_, Error>((product, unit))
}

pub(crate) async fn get_products() -> Result<Vec<Product>, Error> {
    let config = expect_context::<Store<ConfigState>>();
    products(&config.config().read())
        .await
        .map_err(Into::<Error>::into)
}

pub(crate) async fn buy_barcode_wrapper(
    config: &Configuration,
    barcode_number: u32,
) -> Result<(), Error> {
    buy_barcode(
        config,
        BarcodeId {
            value: barcode_number,
        },
    )
    .await
    .map_err(Into::<Error>::into)
}