Improves fields::RecipeCategory
This commit is contained in:
@@ -6,14 +6,10 @@ use self::models::*;
|
||||
use self::diesel::prelude::*;
|
||||
|
||||
fn main() {
|
||||
use cookbook::schema::recipes::dsl::*;
|
||||
|
||||
let conn = establish_connection();
|
||||
let result = recipes
|
||||
.load::<Recipe>(&conn)
|
||||
.expect("Error loading recipes");
|
||||
|
||||
println!("Here are {} recipes :", result.len());
|
||||
let result = recipes::load_all(&conn);
|
||||
println!("Here are {} recipes [{}]:", result.len(), result.len() * std::mem::size_of::<Recipe>());
|
||||
for rec in result {
|
||||
println!("*************\n{}\n({:?})", rec.title, rec.category);
|
||||
println!("-------------\n");
|
||||
|
||||
@@ -27,6 +27,10 @@ impl<'a> CreateRecipe<'a> {
|
||||
self.title = title;
|
||||
}
|
||||
|
||||
fn set_category(&mut self, id: i16) {
|
||||
self.category = RecipeCategory::from_id(id);
|
||||
}
|
||||
|
||||
fn add_ingredient(&mut self, name: String) {
|
||||
use crate::ingredients::*;
|
||||
|
||||
@@ -43,7 +47,11 @@ impl<'a> CreateRecipe<'a> {
|
||||
|
||||
/// Builds a NewRecipe instance from current data and insert it.
|
||||
fn insert(self) {
|
||||
let new_recipe = NewRecipe::new(self.title, 0, &self.ingredients, "");
|
||||
let new_recipe = NewRecipe::new(
|
||||
self.title,
|
||||
self.category.unwrap_or(RecipeCategory::Breakfast),
|
||||
&self.ingredients,
|
||||
"");
|
||||
match new_recipe.insert(self.connection) {
|
||||
Ok(new) => println!("Added {}", new.title),
|
||||
Err(e) => println!("Error: {}", e),
|
||||
@@ -62,6 +70,15 @@ fn main() {
|
||||
let title = &title[..(title.len() - 1)];
|
||||
builder.set_title(title);
|
||||
|
||||
println!("Category : ");
|
||||
for cat in &RecipeCategory::all() {
|
||||
println!("{} - {}", cat.id(), cat.name());
|
||||
}
|
||||
let mut category_id = String::new();
|
||||
stdin().read_line(&mut category_id).unwrap();
|
||||
let category_id = category_id.trim().parse::<i16>().expect("Could not parse id");
|
||||
builder.set_category(category_id);
|
||||
|
||||
println!("Ingredients (empty line to finish): ");
|
||||
loop {
|
||||
let mut ingdts = String::new();
|
||||
@@ -69,7 +86,7 @@ fn main() {
|
||||
if &ingdts == "\r\n" {
|
||||
break;
|
||||
}
|
||||
builder.add_ingredient(ingdts.clone());
|
||||
builder.add_ingredient(ingdts);
|
||||
}
|
||||
|
||||
builder.insert();
|
||||
|
||||
Reference in New Issue
Block a user