SQLite user recipe and category fetching

This commit is contained in:
Jeremy Wall 2022-09-01 19:45:37 -04:00
parent ba68738dfa
commit 62ea422d79
6 changed files with 103 additions and 5 deletions

View File

@ -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

View File

@ -1,2 +1,3 @@
-- Add down migration script here
drop table recipes;
drop table categories;

View File

@ -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));

View File

@ -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": [],

View File

@ -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<Option<String>, 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<Option<Vec<RecipeEntry>>, 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<String>,
};
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))
}
}

View File

@ -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 {