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

@@ -12,6 +12,7 @@ mod importer;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;
use crate::models::{Recipe, NewRecipe, Ingredient, NewIngredient};
pub fn establish_connection() -> SqliteConnection {
dotenv().ok();
@@ -21,7 +22,27 @@ 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 fn find_ingredient(conn: &SqliteConnection, alias: &str) -> Option<Ingredient> {
// Find ingredient by alias
None
}
#[cfg(test)]
mod tests {