about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/get/unit
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get/unit')
-rw-r--r--crates/rocie-server/src/storage/sql/get/unit/mod.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/crates/rocie-server/src/storage/sql/get/unit/mod.rs b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
new file mode 100644
index 0000000..6c2bbcc
--- /dev/null
+++ b/crates/rocie-server/src/storage/sql/get/unit/mod.rs
@@ -0,0 +1,79 @@
+use crate::{
+    app::App,
+    storage::sql::unit::{Unit, UnitId},
+};
+
+use sqlx::query;
+
+impl Unit {
+    pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
+        let records = query!(
+            "
+        SELECT id, full_name_singular, full_name_plural, short_name, description
+        FROM units
+"
+        )
+        .fetch_all(&app.db)
+        .await?;
+
+        Ok(records
+            .into_iter()
+            .map(|record| Self {
+                id: UnitId::from_db(&record.id),
+                full_name_singular: record.full_name_singular,
+                full_name_plural: record.full_name_plural,
+                short_name: record.short_name,
+                description: record.description,
+            })
+            .collect())
+    }
+
+    pub(crate) async fn from_id(app: &App, id: UnitId) -> Result<Option<Self>, from_id::Error> {
+        let record = query!(
+            "
+        SELECT full_name_singular, full_name_plural, short_name, description
+        FROM units
+        WHERE id = ?
+",
+            id
+        )
+        .fetch_optional(&app.db)
+        .await?;
+
+        if let Some(record) = record {
+            Ok(Some(Self {
+                id,
+                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 {
+    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 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 {}
+}