From 202d7c5976e72616e23fab031a0edf814b96300a Mon Sep 17 00:00:00 2001 From: artus40 Date: Mon, 28 Jan 2019 15:23:39 +0100 Subject: [PATCH] Adds write script for recipes, adds ingredients migrations --- cookbook/db.sqlite3 | Bin 20480 -> 20480 bytes .../down.sql | 2 + .../up.sql | 5 ++ cookbook/src/bin/show_recipes.rs | 2 +- cookbook/src/bin/write_recipe.rs | 69 ++++++++++++++++++ cookbook/src/lib.rs | 21 ++++++ cookbook/src/models.rs | 24 ++++++ cookbook/src/schema.rs | 12 +++ 8 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 cookbook/migrations/2019-01-28-141711_create_ingredients/down.sql create mode 100644 cookbook/migrations/2019-01-28-141711_create_ingredients/up.sql create mode 100644 cookbook/src/bin/write_recipe.rs diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index 4478b719c5546d290015a3cb6cc74d9a8b5c77ab..c67a2ba1437d16eb8b2f7c92c893c7810d378d89 100644 GIT binary patch delta 545 zcmY*XyHDFd7(eG>tgAj>(h34X(34UXrQzYTBQ=CBsTGwW0u<723>R{^mVzDmu{N+W zwaUbX#L^Xo|AVn*ym*`^zxn@zx_5mx_6N{XB#a!Jld`P@(P_Dw2XGIWit zYD}-P3VY93ye(;LNv~2?sj+I!Fou_}-$lYJM6SuzC{HiQtC#7L^jw!3Uaw5$Wc3!2 z-{hF=5R=TSw^7U=bucH(Xih1^C+>AZPk_lC?sWlgyJ24dE@ovkqpU$7{6Ggo(=6H* z)Qf5ZHmEDYJs!9-RjL7y+!`JC9ND zdP+u2*@sRX6Ny953T?}Aokw+oJc)O?=LU(LL3h9M{eHrZ+-Ie@MH%kG9$Lg3iO~2A OP%b%w%qXw@CI10eCXQAB delta 152 zcmZozz}T>Wae}lUD+2=q3^M}R920enMOhj2bOLz!e=sofaWU}S { + 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, +);