about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/get/recipe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/storage/sql/get/recipe')
-rw-r--r--crates/rocie-server/src/storage/sql/get/recipe/mod.rs78
1 files changed, 78 insertions, 0 deletions
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 {}
+}