diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index 4478b71..c67a2ba 100644 Binary files a/cookbook/db.sqlite3 and b/cookbook/db.sqlite3 differ diff --git a/cookbook/migrations/2019-01-28-141711_create_ingredients/down.sql b/cookbook/migrations/2019-01-28-141711_create_ingredients/down.sql new file mode 100644 index 0000000..57f6dc2 --- /dev/null +++ b/cookbook/migrations/2019-01-28-141711_create_ingredients/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE ingredients diff --git a/cookbook/migrations/2019-01-28-141711_create_ingredients/up.sql b/cookbook/migrations/2019-01-28-141711_create_ingredients/up.sql new file mode 100644 index 0000000..a70c33a --- /dev/null +++ b/cookbook/migrations/2019-01-28-141711_create_ingredients/up.sql @@ -0,0 +1,5 @@ +-- Your SQL goes here +CREATE TABLE ingredients ( + id INTEGER PRIMARY KEY NOT NULL, + alias VARCHAR NOT NULL +) diff --git a/cookbook/src/bin/show_recipes.rs b/cookbook/src/bin/show_recipes.rs index 2b5fb0c..2d86407 100644 --- a/cookbook/src/bin/show_recipes.rs +++ b/cookbook/src/bin/show_recipes.rs @@ -16,7 +16,7 @@ fn main() { println!("Here are {} recipes :", result.len()); for rec in result { - println!("{}", rec.title); + println!("*************\n{}", rec.title); println!("-------------\n"); println!("{}", rec.ingredients); } diff --git a/cookbook/src/bin/write_recipe.rs b/cookbook/src/bin/write_recipe.rs new file mode 100644 index 0000000..49788ae --- /dev/null +++ b/cookbook/src/bin/write_recipe.rs @@ -0,0 +1,69 @@ +extern crate cookbook; +extern crate diesel; + +use self::cookbook::*; +use std::io::{Read, stdin}; +use self::models::NewRecipe; +use diesel::SqliteConnection; + +struct CreateRecipe<'a> { + connection: &'a SqliteConnection, + title: &'a str, + ingredients: String, +} + +impl<'a> CreateRecipe<'a> { + fn new(conn: &'a SqliteConnection) -> Self { + CreateRecipe{ + connection: conn, + title: "", + ingredients: String::new(), + } + } + + fn set_title(&mut self, title: &'a str) { + self.title = title; + } + + fn add_ingredient(&mut self, ingredient: String) { + // TODO: Validate ingredient + self.ingredients.push_str(&ingredient); + println!("+"); + } + + fn insert(self) { + let _ = create_recipe(self.connection, self.title, &self.ingredients); + println!("Added {}", self.title); + } +} + + +fn main() { + let conn = establish_connection(); + let mut builder = CreateRecipe::new(&conn); + + println!("Title : "); + let mut title = String::new(); + stdin().read_line(&mut title).unwrap(); + let title = &title[..(title.len() - 1)]; + builder.set_title(title); + + println!("Ingredients (empty line to finish): "); + loop { + let mut ingdts = String::new(); + stdin().read_line(&mut ingdts).unwrap(); + if &ingdts == "\r\n" { + break; + } + builder.add_ingredient(ingdts.clone()); + } + + builder.insert(); +} + + +#[cfg(not(windows))] +const EOF: &'static str = "CTRL+D"; + +#[cfg(windows)] +const EOF: &'static str = "CTRL+Z"; diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index 9f1d396..60380c0 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -12,6 +12,7 @@ mod importer; use diesel::prelude::*; use dotenv::dotenv; use std::env; +use crate::models::{Recipe, NewRecipe, Ingredient, NewIngredient}; pub fn establish_connection() -> SqliteConnection { dotenv().ok(); @@ -21,7 +22,27 @@ 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 fn find_ingredient(conn: &SqliteConnection, alias: &str) -> Option { + // Find ingredient by alias + None +} #[cfg(test)] mod tests { diff --git a/cookbook/src/models.rs b/cookbook/src/models.rs index 53e04c2..0e78498 100644 --- a/cookbook/src/models.rs +++ b/cookbook/src/models.rs @@ -1,3 +1,6 @@ +use super::schema::recipes; +use super::schema::ingredients; + #[derive(Queryable)] pub struct Recipe { pub id: i32, @@ -6,3 +9,24 @@ pub struct Recipe { pub ingredients: String, pub preparation: String, } + +#[derive(Insertable)] +#[table_name="recipes"] +pub struct NewRecipe<'a> { + pub title: &'a str, + pub category: i32, + pub ingredients: String, + pub preparation: &'a str, +} + +#[derive(Queryable)] +pub struct Ingredient { + pub id: i32, + pub alias: String, +} + +#[derive(Insertable)] +#[table_name="ingredients"] +pub struct NewIngredient<'a> { + pub alias: &'a str, +} diff --git a/cookbook/src/schema.rs b/cookbook/src/schema.rs index 21e9919..bfe1621 100644 --- a/cookbook/src/schema.rs +++ b/cookbook/src/schema.rs @@ -1,3 +1,10 @@ +table! { + ingredients (id) { + id -> Integer, + alias -> Text, + } +} + table! { recipes (id) { id -> Integer, @@ -7,3 +14,8 @@ table! { preparation -> Text, } } + +allow_tables_to_appear_in_same_query!( + ingredients, + recipes, +);