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/config/mod.rs41
-rw-r--r--crates/rocie-server/src/storage/sql/get/mod.rs3
-rw-r--r--crates/rocie-server/src/storage/sql/get/unit/mod.rs40
3 files changed, 83 insertions, 1 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/config/mod.rs b/crates/rocie-server/src/storage/sql/get/config/mod.rs
new file mode 100644
index 0000000..eb8be86
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/config/mod.rs
@@ -0,0 +1,41 @@
+use crate::{app::App, storage::sql::config::Config};
+
+use sqlx::query;
+
+impl Config {
+    pub(crate) async fn get(app: &App) -> Result<Self, get::Error> {
+        let record = query!(
+            "
+        SELECT use_defaults
+        FROM config
+        WHERE id = 0
+"
+        )
+        .fetch_one(&app.db)
+        .await?;
+
+        let should_use_defaults = if record.use_defaults == 1 {
+            true
+        } else if record.use_defaults == 0 {
+            false
+        } else {
+            unreachable!("Should not be possible, sqlite's CHECK prevents it")
+        };
+
+        Ok(Self {
+            should_use_defaults,
+        })
+    }
+}
+
+pub(crate) mod get {
+    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/mod.rs b/crates/rocie-server/src/storage/sql/get/mod.rs
index a6ee0e1..e3520da 100644
--- a/crates/rocie-server/src/storage/sql/get/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/mod.rs
@@ -1,9 +1,10 @@
 pub(crate) mod barcode;
+pub(crate) mod config;
 pub(crate) mod product;
 pub(crate) mod product_amount;
 pub(crate) mod product_parent;
-pub(crate) mod recipe_parent;
 pub(crate) mod recipe;
+pub(crate) mod recipe_parent;
 pub(crate) mod unit;
 pub(crate) mod unit_property;
 pub(crate) mod user;
diff --git a/crates/rocie-server/src/storage/sql/get/unit/mod.rs b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
index 6f5d297..2c85970 100644
--- a/crates/rocie-server/src/storage/sql/get/unit/mod.rs
+++ b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
@@ -57,6 +57,34 @@ impl Unit {
             Ok(None)
         }
     }
+
+    pub(crate) async fn from_name(app: &App, name: &str) -> Result<Option<Self>, from_name::Error> {
+        let record = query!(
+            "
+        SELECT id, full_name_singular, unit_property, full_name_plural, short_name, description
+        FROM units
+        WHERE full_name_singular = ? OR full_name_plural = ? OR short_name = ?
+",
+            name,
+            name,
+            name
+        )
+        .fetch_optional(&app.db)
+        .await?;
+
+        if let Some(record) = record {
+            Ok(Some(Self {
+                id: UnitId::from_db(&record.id),
+                unit_property: UnitPropertyId::from_db(&record.unit_property),
+                full_name_singular: record.full_name_singular,
+                full_name_plural: record.full_name_plural,
+                short_name: record.short_name,
+                description: record.description,
+            }))
+        } else {
+            Ok(None)
+        }
+    }
 }
 
 pub(crate) mod get_all {
@@ -82,3 +110,15 @@ pub(crate) mod from_id {
 
     impl ResponseError for Error {}
 }
+
+pub(crate) mod from_name {
+    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 {}
+}