Minor refactoring
This commit is contained in:
Binary file not shown.
@@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,22 +21,6 @@ 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 mod ingredients {
|
||||
use crate::models::{Ingredient, NewIngredient};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::schema::recipes;
|
||||
use super::schema::ingredients;
|
||||
use super::diesel::prelude::*;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Recipe {
|
||||
@@ -15,10 +16,29 @@ pub struct Recipe {
|
||||
pub struct NewRecipe<'a> {
|
||||
pub title: &'a str,
|
||||
pub category: i32,
|
||||
pub ingredients: String,
|
||||
pub ingredients: &'a str,
|
||||
pub preparation: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> NewRecipe<'a> {
|
||||
pub fn new(title: &'a str, category: i32, ingredients: &'a str, preparation: &'a str) -> Self {
|
||||
NewRecipe{
|
||||
title,
|
||||
category,
|
||||
ingredients,
|
||||
preparation,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(self, conn: &SqliteConnection) -> Result<Self, String> {
|
||||
diesel::insert_into(recipes::table)
|
||||
.values(&self)
|
||||
.execute(conn)
|
||||
.expect("Error inserting recipe");
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct Ingredient {
|
||||
pub id: i32,
|
||||
|
||||
Reference in New Issue
Block a user