From 4576ffb03b6cf3e3dd81f54586142531fd9482b0 Mon Sep 17 00:00:00 2001 From: artus40 Date: Mon, 28 Jan 2019 21:25:25 +0100 Subject: [PATCH] Adds ingredients managing --- cookbook/db.sqlite3 | Bin 20480 -> 20480 bytes cookbook/src/bin/write_recipe.rs | 16 ++++++++++---- cookbook/src/lib.rs | 35 ++++++++++++++++++++++++++----- cookbook/src/recipe.rs | 20 ------------------ cookbook/src/storage.rs | 4 ---- 5 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 cookbook/src/storage.rs diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index c67a2ba1437d16eb8b2f7c92c893c7810d378d89..e4e6f149bcd12ec3a9acba4990eba49243b631b7 100644 GIT binary patch delta 201 zcmZozz}T>Wae}mTPyn-o|C zIGFhNGw?s+-@jQWae}lUF9QPuD-go~_e33IM&6AH#q#VXR)!W<#>SIx$;)t=D;ODAnHXCc zTWnHb5fEVG`_I7tn13dJEWb0q9N+)Vf&#Dk>Sfv37-TuLdEFC>l1htGd7TsU5{puJ zxq|ac3sQNxlv&vrBsr{kOHzwVjChR=4AK=+QWc6aQ}cMaj1}@zOVf&Zxg=QF7{oc$ z!HRf|O^}pWF|#q~a|H7SBmxakNXpO2PtQxtEKY@+R9^s+;pH-dnvA4`%Y%uH!H^@D zHwk17P;+TfdTC~QUVa{2H&_OtIXST?zXWWYu|jf2Vr~IgHPCcvMvyPGc_Eg;d;#L| X17$(M$shm@OqR`p3a9udPS64XC0|@X diff --git a/cookbook/src/bin/write_recipe.rs b/cookbook/src/bin/write_recipe.rs index 49788ae..998bd5e 100644 --- a/cookbook/src/bin/write_recipe.rs +++ b/cookbook/src/bin/write_recipe.rs @@ -25,10 +25,18 @@ impl<'a> CreateRecipe<'a> { self.title = title; } - fn add_ingredient(&mut self, ingredient: String) { - // TODO: Validate ingredient - self.ingredients.push_str(&ingredient); - println!("+"); + fn add_ingredient(&mut self, name: String) { + use crate::ingredients::*; + + // Check it exists or create + if let Some(ingdt) = find(self.connection, &name) { + println!("="); + } else { + create(self.connection, &name); + println!("+{}", &name); + } + + self.ingredients.push_str(&name); } fn insert(self) { diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index 60380c0..e822d74 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -6,13 +6,12 @@ pub mod schema; pub mod models; mod recipe; -mod storage; mod importer; use diesel::prelude::*; use dotenv::dotenv; use std::env; -use crate::models::{Recipe, NewRecipe, Ingredient, NewIngredient}; +use crate::models::{Recipe, NewRecipe}; pub fn establish_connection() -> SqliteConnection { dotenv().ok(); @@ -39,11 +38,37 @@ pub fn create_recipe(conn: &SqliteConnection, title: &str, ingdts: &str) -> usiz } -pub fn find_ingredient(conn: &SqliteConnection, alias: &str) -> Option { - // Find ingredient by alias - None +pub mod ingredients { + use crate::models::{Ingredient, NewIngredient}; + use super::{SqliteConnection, schema}; + use super::diesel::prelude::*; + + pub fn find(conn: &SqliteConnection, name: &str) -> Option { + use self::schema::ingredients::dsl::*; + + let results = ingredients.filter(alias.like(name)) + .limit(1) + .load::(conn) + .expect("Error finding ingredient"); + + if !results.is_empty() { + Some(results.into_iter().nth(0).unwrap()) + } else { + None + } + } + + pub fn create(conn: &SqliteConnection, name: &str) -> usize { + use self::schema::ingredients; + + diesel::insert_into(ingredients::table) + .values(&NewIngredient { alias: name }) + .execute(conn) + .expect("Error inserting ingredient") + } } + #[cfg(test)] mod tests { #[test] diff --git a/cookbook/src/recipe.rs b/cookbook/src/recipe.rs index 9076fe5..1ecc7c5 100644 --- a/cookbook/src/recipe.rs +++ b/cookbook/src/recipe.rs @@ -34,23 +34,3 @@ pub struct Ingredient { /// Associated tags to constraint planning tags: FoodTags, } - -impl Ingredient { - /// Builds a brand new ingredient with given name as first alias - pub(super) fn new(name: String) -> Ingredient { - Ingredient { - alias: vec![name], - ..Default::default() - } - } - - /// Find an existing ingredient by alias - pub fn find(alias: &str) -> Option { - None - } - - /// Get the ingredient most common name - pub fn name(&self) -> &str { - self.alias.first().expect("Ingredient without any name !") - } -} diff --git a/cookbook/src/storage.rs b/cookbook/src/storage.rs deleted file mode 100644 index 2b6bbb0..0000000 --- a/cookbook/src/storage.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Storage backend for persistent data -//! -//! Data is stored in a sqlite file. -//! We'll try to use Diesel-rs, but not ready yet to grasp it all.