Adds write script for recipes, adds ingredients migrations

This commit is contained in:
2019-01-28 15:23:39 +01:00
parent fb4d24ef44
commit 202d7c5976
8 changed files with 134 additions and 1 deletions

View File

@@ -0,0 +1,69 @@
extern crate cookbook;
extern crate diesel;
use self::cookbook::*;
use std::io::{Read, stdin};
use self::models::NewRecipe;
use diesel::SqliteConnection;
struct CreateRecipe<'a> {
connection: &'a SqliteConnection,
title: &'a str,
ingredients: String,
}
impl<'a> CreateRecipe<'a> {
fn new(conn: &'a SqliteConnection) -> Self {
CreateRecipe{
connection: conn,
title: "",
ingredients: String::new(),
}
}
fn set_title(&mut self, title: &'a str) {
self.title = title;
}
fn add_ingredient(&mut self, ingredient: String) {
// TODO: Validate ingredient
self.ingredients.push_str(&ingredient);
println!("+");
}
fn insert(self) {
let _ = create_recipe(self.connection, self.title, &self.ingredients);
println!("Added {}", self.title);
}
}
fn main() {
let conn = establish_connection();
let mut builder = CreateRecipe::new(&conn);
println!("Title : ");
let mut title = String::new();
stdin().read_line(&mut title).unwrap();
let title = &title[..(title.len() - 1)];
builder.set_title(title);
println!("Ingredients (empty line to finish): ");
loop {
let mut ingdts = String::new();
stdin().read_line(&mut ingdts).unwrap();
if &ingdts == "\r\n" {
break;
}
builder.add_ingredient(ingdts.clone());
}
builder.insert();
}
#[cfg(not(windows))]
const EOF: &'static str = "CTRL+D";
#[cfg(windows)]
const EOF: &'static str = "CTRL+Z";