diff --git a/Makefile b/Makefile index e434099..36fda75 100644 --- a/Makefile +++ b/Makefile @@ -37,5 +37,5 @@ clean: rm -rf web/dist/* cargo clean -sqlx-prepare: wasm kitches/src/*.rs - cd kitchen; cargo sqlx-prepare +sqlx-prepare: + cd kitchen; cargo sqlx prepare diff --git a/kitchen/migrations/20220901233514_recipes_v1.down.sql b/kitchen/migrations/20220901233514_recipes_v1.down.sql index 089b9a1..e4b95f6 100644 --- a/kitchen/migrations/20220901233514_recipes_v1.down.sql +++ b/kitchen/migrations/20220901233514_recipes_v1.down.sql @@ -1,2 +1,3 @@ -- Add down migration script here -drop table recipes; \ No newline at end of file +drop table recipes; +drop table categories; \ No newline at end of file diff --git a/kitchen/migrations/20220901233514_recipes_v1.up.sql b/kitchen/migrations/20220901233514_recipes_v1.up.sql index 82c2eb3..d9c300f 100644 --- a/kitchen/migrations/20220901233514_recipes_v1.up.sql +++ b/kitchen/migrations/20220901233514_recipes_v1.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here create table recipes(user_id TEXT NOT NULL, recipe_id TEXT NOT NULL, recipe_text TEXT, constraint recipe_primary_key primary key (user_id, recipe_id)); -create table categories(user_id TEXT NOT NULL, category_text, +create table categories(user_id TEXT NOT NULL, category_text TEXT, constraint category_primary_key primary key (user_id)); \ No newline at end of file diff --git a/kitchen/sqlx-data.json b/kitchen/sqlx-data.json index 1f46458..62df1b6 100644 --- a/kitchen/sqlx-data.json +++ b/kitchen/sqlx-data.json @@ -56,6 +56,30 @@ }, "query": "select session_value from sessions where id = ?" }, + "95fbc362a2e17add05218a2dac431275b5cc55bd7ac8f4173ee10afefceafa3b": { + "describe": { + "columns": [ + { + "name": "recipe_id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "recipe_text", + "ordinal": 1, + "type_info": "Text" + } + ], + "nullable": [ + false, + true + ], + "parameters": { + "Right": 1 + } + }, + "query": "select recipe_id, recipe_text from recipes where user_id = ?" + }, "9ad4acd9b9d32c9f9f441276aa71a17674fe4d65698848044778bd4aef77d42d": { "describe": { "columns": [], @@ -66,6 +90,24 @@ }, "query": "insert into sessions (id, session_value) values (?, ?)" }, + "c988364f9f83f4fa8bd0e594bab432ee7c9ec47ca40f4d16e5e2a8763653f377": { + "describe": { + "columns": [ + { + "name": "category_text", + "ordinal": 0, + "type_info": "Text" + } + ], + "nullable": [ + true + ], + "parameters": { + "Right": 1 + } + }, + "query": "select category_text from categories where user_id = ?" + }, "d84685a82585c5e4ae72c86ba1fe6e4a7241c4c3c9e948213e5849d956132bad": { "describe": { "columns": [], diff --git a/kitchen/src/web/session.rs b/kitchen/src/web/session.rs index 9bce4fc..ac5bab7 100644 --- a/kitchen/src/web/session.rs +++ b/kitchen/src/web/session.rs @@ -36,6 +36,8 @@ use sqlx::{ }; use tracing::{debug, error, info, instrument}; +use recipe_store::RecipeEntry; + pub const AXUM_SESSION_COOKIE_NAME: &'static str = "kitchen-session-cookie"; #[derive(Debug, Serialize, Deserialize)] @@ -233,3 +235,56 @@ impl AuthStore for SqliteStore { Ok(()) } } + +impl SqliteStore { + pub async fn get_categories_for_user( + &self, + user_id: &str, + ) -> Result, recipe_store::Error> { + match sqlx::query_scalar!( + "select category_text from categories where user_id = ?", + user_id, + ) + .fetch_optional(self.pool.as_ref()) + .await + { + Ok(Some(result)) => return Ok(result), + Ok(None) => return Ok(None), + Err(err) => { + error!(?err, "Error getting categories from sqlite db"); + return Err(recipe_store::Error::from(format!("{:?}", err))); + } + } + } + + pub async fn get_recipes_for_user( + &self, + user_id: &str, + ) -> Result>, recipe_store::Error> { + // NOTE(jwall): We allow dead code becaue Rust can't figure out that + // this code is actually constructed but it's done via the query_as + // macro. + #[allow(dead_code)] + struct RecipeRow { + pub recipe_id: String, + pub recipe_text: Option, + }; + let rows = sqlx::query_as!( + RecipeRow, + "select recipe_id, recipe_text from recipes where user_id = ?", + user_id, + ) + .fetch_all(self.pool.as_ref()) + .await + .map_err(|e| format!("{:?}", e))? + .iter() + .map(|row| { + RecipeEntry( + row.recipe_id.clone(), + row.recipe_text.clone().unwrap_or_else(|| String::new()), + ) + }) + .collect(); + Ok(Some(rows)) + } +} diff --git a/recipe-store/src/lib.rs b/recipe-store/src/lib.rs index b87873f..a0ca4da 100644 --- a/recipe-store/src/lib.rs +++ b/recipe-store/src/lib.rs @@ -62,7 +62,7 @@ where } #[derive(Serialize, Deserialize)] -pub struct RecipeEntry(String, String); +pub struct RecipeEntry(pub String, pub String); impl RecipeEntry { pub fn recipe_id(&self) -> &str {