diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index e4e6f14..32548bf 100644 Binary files a/cookbook/db.sqlite3 and b/cookbook/db.sqlite3 differ diff --git a/cookbook/src/bin/write_recipe.rs b/cookbook/src/bin/write_recipe.rs index 998bd5e..b0a3585 100644 --- a/cookbook/src/bin/write_recipe.rs +++ b/cookbook/src/bin/write_recipe.rs @@ -1,10 +1,10 @@ extern crate cookbook; extern crate diesel; -use self::cookbook::*; use std::io::{Read, stdin}; -use self::models::NewRecipe; use diesel::SqliteConnection; +use self::cookbook::*; +use self::models::NewRecipe; struct CreateRecipe<'a> { connection: &'a SqliteConnection, @@ -16,7 +16,7 @@ impl<'a> CreateRecipe<'a> { fn new(conn: &'a SqliteConnection) -> Self { CreateRecipe{ connection: conn, - title: "", + title: "New recipe", ingredients: String::new(), } } @@ -29,7 +29,7 @@ impl<'a> CreateRecipe<'a> { use crate::ingredients::*; // Check it exists or create - if let Some(ingdt) = find(self.connection, &name) { + if let Some(_ingdt) = find(self.connection, &name) { println!("="); } else { create(self.connection, &name); @@ -39,9 +39,13 @@ impl<'a> CreateRecipe<'a> { self.ingredients.push_str(&name); } + /// Builds a NewRecipe instance from current data and insert it. fn insert(self) { - let _ = create_recipe(self.connection, self.title, &self.ingredients); - println!("Added {}", self.title); + let new_recipe = NewRecipe::new(self.title, 0, &self.ingredients, ""); + match new_recipe.insert(self.connection) { + Ok(new) => println!("Added {}", new.title), + Err(e) => println!("Error: {}", e), + }; } } diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index e822d74..a614405 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -21,22 +21,6 @@ pub fn establish_connection() -> SqliteConnection { .expect(&format!("Error connecting to {}", db_url)) } -pub fn create_recipe(conn: &SqliteConnection, title: &str, ingdts: &str) -> usize { - use self::schema::recipes; - - let new_recipe = NewRecipe { - title: title, - category: 0, - ingredients: ingdts.to_string(), - preparation: &String::new(), - }; - - diesel::insert_into(recipes::table) - .values(&new_recipe) - .execute(conn) - .expect("Error inserting recipe") - -} pub mod ingredients { use crate::models::{Ingredient, NewIngredient}; diff --git a/cookbook/src/models.rs b/cookbook/src/models.rs index 0e78498..f120216 100644 --- a/cookbook/src/models.rs +++ b/cookbook/src/models.rs @@ -1,5 +1,6 @@ use super::schema::recipes; use super::schema::ingredients; +use super::diesel::prelude::*; #[derive(Queryable)] pub struct Recipe { @@ -15,10 +16,29 @@ pub struct Recipe { pub struct NewRecipe<'a> { pub title: &'a str, pub category: i32, - pub ingredients: String, + pub ingredients: &'a str, pub preparation: &'a str, } +impl<'a> NewRecipe<'a> { + pub fn new(title: &'a str, category: i32, ingredients: &'a str, preparation: &'a str) -> Self { + NewRecipe{ + title, + category, + ingredients, + preparation, + } + } + + pub fn insert(self, conn: &SqliteConnection) -> Result { + diesel::insert_into(recipes::table) + .values(&self) + .execute(conn) + .expect("Error inserting recipe"); + Ok(self) + } +} + #[derive(Queryable)] pub struct Ingredient { pub id: i32,