summary refs log tree commit diff stats
path: root/src/api/mod.rs
blob: 8b9e77dc66861dc0f77ff849e06661e9679795b9 (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
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, products},
        api_get_unit_api::unit_by_id,
    },
    models::{Product, ProductAmount, ProductId, Unit, UnitId},
};

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_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_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_products() -> Result<Vec<Product>, Error> {
    let config = expect_context::<Store<ConfigState>>();
    products(&config.config().read())
        .await
        .map_err(Into::<Error>::into)
}