summary refs log tree commit diff stats
path: root/src/api
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-30 09:15:56 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-09-30 09:15:56 +0200
commitd0263ce46160cd4152c67381fab2ee557f3aa483 (patch)
treeb24a725d71b984e466ff478fbb4449111beeacac /src/api
parentchore: Second version (diff)
downloadweb-client-d0263ce46160cd4152c67381fab2ee557f3aa483.zip
feat(treewide): Switch to tailwindcss and add more components
Diffstat (limited to 'src/api')
-rw-r--r--src/api/mod.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs
new file mode 100644
index 0000000..8b9e77d
--- /dev/null
+++ b/src/api/mod.rs
@@ -0,0 +1,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)
+}