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.rs1
-rw-r--r--crates/rocie-server/src/storage/sql/get/product/mod.rs81
2 files changed, 82 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/mod.rs b/crates/rocie-server/src/storage/sql/get/mod.rs
new file mode 100644
index 0000000..2268e85
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/mod.rs
@@ -0,0 +1 @@
+pub(crate) mod product;
diff --git a/crates/rocie-server/src/storage/sql/get/product/mod.rs b/crates/rocie-server/src/storage/sql/get/product/mod.rs
new file mode 100644
index 0000000..bcc3e32
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/product/mod.rs
@@ -0,0 +1,81 @@
+use crate::{
+    app::App,
+    storage::sql::product::{Product, ProductId},
+};
+
+use sqlx::query;
+
+impl Product {
+    pub(crate) async fn from_id(app: &App, id: ProductId) -> Result<Option<Self>, from_id::Error> {
+        let record = query!(
+            "
+        SELECT name, description, parent
+        FROM products
+        WHERE id = ?
+",
+            id
+        )
+        .fetch_optional(&app.db)
+        .await?;
+
+        if let Some(record) = record {
+            Ok(Some(Self {
+                id,
+                name: record.name,
+                description: record.description,
+                associated_bar_codes: vec![], // todo
+            }))
+        } else {
+            Ok(None)
+        }
+    }
+
+    pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
+        let records = query!(
+            "
+        SELECT id, name, description, parent
+        FROM products
+"
+        )
+        .fetch_all(&app.db)
+        .await?;
+
+        Ok(records
+            .into_iter()
+            .map(|record| {
+                Self {
+                    id: ProductId::from_db(&record.id),
+                    name: record.name,
+                    description: record.description,
+                    associated_bar_codes: vec![], // todo
+                }
+            })
+            .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 {
+    }
+}