Works on ingredients
This commit is contained in:
@@ -3,7 +3,22 @@ extern crate diesel;
|
||||
|
||||
use self::cookbook::*;
|
||||
use self::models::*;
|
||||
use self::diesel::prelude::*;
|
||||
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use cookbook::models::fields::IngredientList;
|
||||
|
||||
struct IngredientListManager<'a>(&'a SqliteConnection, IngredientList);
|
||||
|
||||
impl<'a> IngredientListManager<'a> {
|
||||
fn display_lines(&self) -> String {
|
||||
let mut repr = String::new();
|
||||
for id in self.1.iter() {
|
||||
let ingdt = ingredients::get(self.0, id).expect("Database integrity error");
|
||||
repr.push_str(&format!("{}\n", ingdt.alias));
|
||||
}
|
||||
repr
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -13,6 +28,6 @@ fn main() {
|
||||
for rec in result {
|
||||
println!("*************\n{}\n({:?})", rec.title, rec.category);
|
||||
println!("-------------\n");
|
||||
println!("{}", rec.ingredients);
|
||||
println!("{}", rec.ingredients.as_string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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