about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/get
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get')
-rw-r--r--crates/rocie-server/src/storage/sql/get/mod.rs2
-rw-r--r--crates/rocie-server/src/storage/sql/get/product/mod.rs85
-rw-r--r--crates/rocie-server/src/storage/sql/get/product_parent/mod.rs87
-rw-r--r--crates/rocie-server/src/storage/sql/get/recipe/mod.rs78
4 files changed, 209 insertions, 43 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/mod.rs b/crates/rocie-server/src/storage/sql/get/mod.rs
index 62047b8..1fb54b0 100644
--- a/crates/rocie-server/src/storage/sql/get/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/mod.rs
@@ -1,5 +1,7 @@
 pub(crate) mod product;
+pub(crate) mod product_parent;
 pub(crate) mod product_amount;
 pub(crate) mod unit;
 pub(crate) mod unit_property;
 pub(crate) mod barcode;
+pub(crate) mod recipe;
diff --git a/crates/rocie-server/src/storage/sql/get/product/mod.rs b/crates/rocie-server/src/storage/sql/get/product/mod.rs
index 0df51d8..915da81 100644
--- a/crates/rocie-server/src/storage/sql/get/product/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/product/mod.rs
@@ -3,6 +3,7 @@ use crate::{
     storage::sql::{
         barcode::{Barcode, BarcodeId},
         product::{Product, ProductId},
+        product_parent::ProductParentId,
         unit::{UnitAmount, UnitId},
         unit_property::UnitPropertyId,
     },
@@ -10,11 +11,46 @@ use crate::{
 
 use sqlx::query;
 
+macro_rules! product_from_record {
+    ($app:ident, $record:ident) => {{
+        let barcodes = query!(
+            "
+            SELECT id, amount, unit
+            FROM barcodes
+            WHERE product_id = ?
+                    ",
+            $record.id,
+        )
+        .fetch_all(&$app.db)
+        .await?;
+
+        Self {
+            id: ProductId::from_db(&$record.id),
+            unit_property: UnitPropertyId::from_db(&$record.unit_property),
+            name: $record.name,
+            description: $record.description,
+            parent: $record
+                .parent
+                .map(|parent| ProductParentId::from_db(&parent)),
+            associated_bar_codes: barcodes
+                .into_iter()
+                .map(|record| Barcode {
+                    id: BarcodeId::from_db(record.id),
+                    amount: UnitAmount {
+                        value: u32::try_from(record.amount).expect("Should be strictly positve"),
+                        unit: UnitId::from_db(&record.unit),
+                    },
+                })
+                .collect(),
+        }
+    }};
+}
+
 impl Product {
     pub(crate) async fn from_id(app: &App, id: ProductId) -> Result<Option<Self>, from_id::Error> {
         let record = query!(
             "
-        SELECT name, unit_property, description, parent
+        SELECT name, id, unit_property, description, parent
         FROM products
         WHERE id = ?
 ",
@@ -24,13 +60,7 @@ impl Product {
         .await?;
 
         if let Some(record) = record {
-            Ok(Some(Self {
-                id,
-                unit_property: UnitPropertyId::from_db(&record.unit_property),
-                name: record.name,
-                description: record.description,
-                associated_bar_codes: vec![], // todo
-            }))
+            Ok(Some(product_from_record!(app, record)))
         } else {
             Ok(None)
         }
@@ -49,13 +79,7 @@ impl Product {
         .await?;
 
         if let Some(record) = record {
-            Ok(Some(Self {
-                id: ProductId::from_db(&record.id),
-                unit_property: UnitPropertyId::from_db(&record.unit_property),
-                name,
-                description: record.description,
-                associated_bar_codes: vec![], // todo
-            }))
+            Ok(Some(product_from_record!(app, record)))
         } else {
             Ok(None)
         }
@@ -73,34 +97,9 @@ impl Product {
 
         let mut all = Vec::with_capacity(records.len());
         for record in records {
-            let barcodes = query!(
-                "
-                SELECT id, amount, unit
-                FROM barcodes
-                WHERE product_id = ?
-",
-                record.id,
-            )
-            .fetch_all(&app.db)
-            .await?;
-
-            all.push(Self {
-                id: ProductId::from_db(&record.id),
-                unit_property: UnitPropertyId::from_db(&record.unit_property),
-                name: record.name,
-                description: record.description,
-                associated_bar_codes: barcodes
-                    .into_iter()
-                    .map(|record| Barcode {
-                        id: BarcodeId::from_db(record.id),
-                        amount: UnitAmount {
-                            value: u32::try_from(record.amount)
-                                .expect("Should be strictly positve"),
-                            unit: UnitId::from_db(&record.unit),
-                        },
-                    })
-                    .collect(),
-            });
+            let product = product_from_record!(app, record);
+
+            all.push(product);
         }
 
         Ok(all)
diff --git a/crates/rocie-server/src/storage/sql/get/product_parent/mod.rs b/crates/rocie-server/src/storage/sql/get/product_parent/mod.rs
new file mode 100644
index 0000000..5b85b62
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/product_parent/mod.rs
@@ -0,0 +1,87 @@
+use crate::{
+    app::App,
+    storage::sql::product_parent::{ProductParent, ProductParentId},
+};
+
+use sqlx::query;
+
+impl ProductParent {
+    pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
+        let records = query!(
+            "
+        SELECT id, parent, name, description
+        FROM parents
+"
+        )
+        .fetch_all(&app.db)
+        .await?;
+
+        let mut all = Vec::with_capacity(records.len());
+        for record in records {
+            let parent = ProductParent {
+                id: ProductParentId::from_db(&record.id),
+                parent: record
+                    .parent
+                    .map(|parent| ProductParentId::from_db(&parent)),
+                name: record.name,
+                description: record.description,
+            };
+
+            all.push(parent);
+        }
+
+        Ok(all)
+    }
+
+    pub(crate) async fn from_id(
+        app: &App,
+        id: ProductParentId,
+    ) -> Result<Option<Self>, from_id::Error> {
+        let record = query!(
+            "
+        SELECT parent, name, description
+        FROM parents
+        WHERE id = ?
+",
+            id
+        )
+        .fetch_optional(&app.db)
+        .await?;
+
+        match record {
+            Some(record) => Ok(Some(ProductParent {
+                id,
+                parent: record
+                    .parent
+                    .map(|parent| ProductParentId::from_db(&parent)),
+                name: record.name,
+                description: record.description,
+            })),
+            None => Ok(None),
+        }
+    }
+}
+
+pub(crate) mod from_id {
+    use actix_web::ResponseError;
+
+    #[derive(thiserror::Error, Debug)]
+    pub(crate) enum Error {
+        #[error("Failed to execute the sql query")]
+        SqlError(#[from] sqlx::Error),
+    }
+
+    impl ResponseError for Error {}
+}
+
+pub(crate) mod get_all {
+    use actix_web::ResponseError;
+
+    #[derive(thiserror::Error, Debug)]
+    pub(crate) enum Error {
+        #[error("Failed to execute the sql query")]
+        SqlError(#[from] sqlx::Error),
+    }
+
+    impl ResponseError for Error {}
+}
diff --git a/crates/rocie-server/src/storage/sql/get/recipe/mod.rs b/crates/rocie-server/src/storage/sql/get/recipe/mod.rs
new file mode 100644
index 0000000..9d6dc79
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/recipe/mod.rs
@@ -0,0 +1,78 @@
+use crate::{
+    app::App,
+    storage::sql::recipe::{Recipe, RecipeId},
+};
+
+use sqlx::query;
+
+impl Recipe {
+    pub(crate) async fn from_id(app: &App, id: RecipeId) -> Result<Option<Self>, from_id::Error> {
+        let record = query!(
+            "
+        SELECT content, path
+        FROM recipies
+        WHERE id = ?
+",
+            id
+        )
+        .fetch_optional(&app.db)
+        .await?;
+
+        if let Some(record) = record {
+            Ok(Some(Self {
+                id,
+                path: record
+                    .path
+                    .parse()
+                    .expect("Was a path before, should still be one"),
+                content: record.content,
+            }))
+        } else {
+            Ok(None)
+        }
+    }
+
+    pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
+        let records = query!(
+            "
+        SELECT id, content, path
+        FROM recipies
+",
+        )
+        .fetch_all(&app.db)
+        .await?;
+
+        Ok(records
+            .into_iter()
+            .map(|record| Self {
+                id: RecipeId::from_db(&record.id),
+                path: record.path.parse().expect("Is still valid"),
+                content: record.content,
+            })
+            .collect())
+    }
+}
+
+pub(crate) mod from_id {
+    use actix_web::ResponseError;
+
+    #[derive(thiserror::Error, Debug)]
+    pub(crate) enum Error {
+        #[error("Failed to execute the sql query")]
+        SqlError(#[from] sqlx::Error),
+    }
+
+    impl ResponseError for Error {}
+}
+
+pub(crate) mod get_all {
+    use actix_web::ResponseError;
+
+    #[derive(thiserror::Error, Debug)]
+    pub(crate) enum Error {
+        #[error("Failed to execute the sql query")]
+        SqlError(#[from] sqlx::Error),
+    }
+
+    impl ResponseError for Error {}
+}