Works on ingredients
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
extern crate cookbook;
|
||||
extern crate diesel;
|
||||
|
||||
use std::io::{Read, stdin};
|
||||
use std::io::{stdin};
|
||||
use diesel::SqliteConnection;
|
||||
use self::cookbook::*;
|
||||
use self::models::{NewRecipe, fields::RecipeCategory};
|
||||
use self::models::{NewRecipe, fields::RecipeCategory, fields::IngredientList};
|
||||
|
||||
struct CreateRecipe<'a> {
|
||||
connection: &'a SqliteConnection,
|
||||
title: &'a str,
|
||||
category: Option<RecipeCategory>,
|
||||
ingredients: String,
|
||||
ingredients: IngredientList,
|
||||
}
|
||||
|
||||
impl<'a> CreateRecipe<'a> {
|
||||
@@ -19,7 +19,7 @@ impl<'a> CreateRecipe<'a> {
|
||||
connection: conn,
|
||||
title: "New recipe",
|
||||
category: None,
|
||||
ingredients: String::new(),
|
||||
ingredients: IngredientList::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,23 +35,23 @@ impl<'a> CreateRecipe<'a> {
|
||||
use crate::ingredients::*;
|
||||
|
||||
// Check it exists or create
|
||||
if let Some(_ingdt) = find(self.connection, &name) {
|
||||
println!("=");
|
||||
} else {
|
||||
create(self.connection, &name);
|
||||
println!("+{}", &name);
|
||||
match get_id_or_create(self.connection, &name) {
|
||||
Ok(id) => {
|
||||
println!("Got id {}", id);
|
||||
self.ingredients.push(id);
|
||||
},
|
||||
Err(_) => println!("Error adding ingredient")
|
||||
}
|
||||
|
||||
self.ingredients.push_str(&name);
|
||||
}
|
||||
|
||||
/// Builds a NewRecipe instance from current data and insert it.
|
||||
fn insert(self) {
|
||||
let new_recipe = NewRecipe::new(
|
||||
self.title,
|
||||
self.category.unwrap_or(RecipeCategory::Breakfast),
|
||||
&self.ingredients,
|
||||
"");
|
||||
let new_recipe = NewRecipe {
|
||||
title: self.title,
|
||||
category: self.category.unwrap_or(RecipeCategory::Breakfast),
|
||||
ingredients: self.ingredients,
|
||||
preparation: ""
|
||||
};
|
||||
match new_recipe.insert(self.connection) {
|
||||
Ok(new) => println!("Added {}", new.title),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
|
||||
Reference in New Issue
Block a user