summary refs log tree commit diff stats
path: root/src/api/mod.rs
blob: 3bc870c10138b085ade809b83236f8d8f93472e2 (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
use leptos::error::Error;
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_in_storage,
            products_registered,
        },
        api_get_unit_api::unit_by_id,
        api_get_unit_property_api::{unit_properties, unit_property_by_id},
        api_set_barcode_api::buy_barcode,
        api_set_product_api::{associate_barcode, register_product},
        configuration::Configuration,
    },
    models::{
        Barcode, BarcodeId, Product, ProductAmount, ProductId, ProductStub, Unit, UnitId,
        UnitProperty, UnitPropertyId,
    },
};

macro_rules! get_config {
    () => {{
        use crate::ConfigStateStoreFields;
        use leptos::prelude::Read;

        let config =
            leptos::prelude::expect_context::<reactive_stores::Store<crate::ConfigState>>();
        config.config().read()
    }};
}

pub(crate) use get_config;

macro_rules! mk_wrapper {
    (
        $orig:ident($($arg_name:ident : $arg_type:ty),*) -> $output_type:ty
        as $new_name:ident
    ) => {
        pub(crate) async fn $new_name(
            $($arg_name : $arg_type),*
        ) -> Result<$output_type, Error> {
            let config = get_config!();

            $orig(&config, $($arg_name),*)
                .await
                .map_err(Into::<Error>::into)
        }
    };

    (
        @treat_404_as_None
        $orig:ident($($arg_name:ident : $arg_type:ty),*) -> Option<$output_type:ty>
        as $new_name:ident
    ) => {
        pub(crate) async fn $new_name(
            $($arg_name : $arg_type),*
        ) -> Result<Option<$output_type>, Error> {
            let config = get_config!();

            match $orig(&config, $($arg_name),*).await
            {
                Ok(ok) => Ok::<_, leptos::error::Error>(Some(ok)),
                Err(err) => match err {
                    rocie_client::apis::Error::ResponseError(ref response_content) => {
                        match response_content.status.as_u16() {
                            404 => Ok(None),
                            _ => Err(err.into()),
                        }
                    }
                    err => Err(err.into()),
                },
            }
        }
    };

    (
        @external_config
        $orig:ident(&config, $($arg_name:ident : $arg_type:ty),*) -> $output_type:ty
        as $new_name:ident
    ) => {
        pub(crate) async fn $new_name(
            config: &Configuration,
            $($arg_name : $arg_type),*
        ) -> Result<$output_type, Error> {
            $orig(config, $($arg_name),*)
                .await
                .map_err(Into::<Error>::into)
        }
    }
}

mk_wrapper!(product_by_id(product_id: ProductId) -> Product as product_by_id_wrapped);

mk_wrapper!(@treat_404_as_None product_by_name(name: &str) -> Option<Product> as product_by_name_404_wrapped);
mk_wrapper!(@external_config product_by_name(&config, name: &str) -> Product as product_by_name_external_wrapped);

mk_wrapper!(product_suggestion_by_name(part_name: &str) -> Vec<Product> as product_suggestion_by_name_wrapped);

mk_wrapper!(unit_by_id(unit_id: UnitId) -> Unit as unit_by_id_wrapped);
mk_wrapper!(unit_property_by_id(unit_id: UnitPropertyId) -> UnitProperty as unit_property_by_id_wrapped);
mk_wrapper!(unit_properties() -> Vec<UnitProperty> as unit_properties_wrapped);

mk_wrapper!(amount_by_id(product_id: ProductId) -> ProductAmount as amount_by_id_wrapped);

mk_wrapper!(products_registered() -> Vec<Product> as products_registered_wrapped);
mk_wrapper!(products_in_storage() -> Vec<Product> as products_in_storage_wrapped);

mk_wrapper!(@external_config buy_barcode(&config, barcode_number: BarcodeId, times: u32) -> () as buy_barcode_external_wrapped);
mk_wrapper!(@external_config register_product(&config, product_stub: ProductStub) -> ProductId as register_product_external_wrapped);
mk_wrapper!(@external_config associate_barcode(&config, id: ProductId, barcode: Barcode) -> () as associate_barcode_external_wrapped);