Minor refactoring

This commit is contained in:
2019-01-29 21:44:05 +01:00
parent 4576ffb03b
commit 874a9f86f8
4 changed files with 31 additions and 23 deletions

View File

@@ -1,10 +1,10 @@
extern crate cookbook;
extern crate diesel;
use self::cookbook::*;
use std::io::{Read, stdin};
use self::models::NewRecipe;
use diesel::SqliteConnection;
use self::cookbook::*;
use self::models::NewRecipe;
struct CreateRecipe<'a> {
connection: &'a SqliteConnection,
@@ -16,7 +16,7 @@ impl<'a> CreateRecipe<'a> {
fn new(conn: &'a SqliteConnection) -> Self {
CreateRecipe{
connection: conn,
title: "",
title: "New recipe",
ingredients: String::new(),
}
}
@@ -29,7 +29,7 @@ impl<'a> CreateRecipe<'a> {
use crate::ingredients::*;
// Check it exists or create
if let Some(ingdt) = find(self.connection, &name) {
if let Some(_ingdt) = find(self.connection, &name) {
println!("=");
} else {
create(self.connection, &name);
@@ -39,9 +39,13 @@ impl<'a> CreateRecipe<'a> {
self.ingredients.push_str(&name);
}
/// Builds a NewRecipe instance from current data and insert it.
fn insert(self) {
let _ = create_recipe(self.connection, self.title, &self.ingredients);
println!("Added {}", self.title);
let new_recipe = NewRecipe::new(self.title, 0, &self.ingredients, "");
match new_recipe.insert(self.connection) {
Ok(new) => println!("Added {}", new.title),
Err(e) => println!("Error: {}", e),
};
}
}