1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
pub mod models; pub mod schema; use super::errors::*; use diesel; use diesel::prelude::*; use diesel::sqlite::SqliteConnection; use dotenv::dotenv; use std::env; use self::models::*; pub fn establish_connection() -> SqliteConnection { dotenv().ok(); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); SqliteConnection::establish(&database_url) .expect(&format!("Error connecting to {}", database_url)) } pub fn create_recipe<'a>(conn: &SqliteConnection, name: &'a str, description: &'a str) -> Result<usize> { use self::schema::recipes; let new_recipe = NewRecipe { name: name, description: description, }; let size = try!(diesel::insert(&new_recipe) .into(recipes::table) .execute(conn)); Ok(size) } pub fn create_recipe_ingredients<'a>(conn: &SqliteConnection, recipe_id: i32, ingredient_id: i32, amount: f32) -> Result<usize> { use self::schema::recipe_ingredients; let new_ingrdient = NewRecipeIngredient { recipe_id: recipe_id, ingredient_id: ingredient_id, amount: amount, }; let size = try!(diesel::insert(&new_ingrdient) .into(recipe_ingredients::table) .execute(conn)); Ok(size) } pub fn create_ingredient<'a>(conn: &SqliteConnection, name: &'a str, description: &'a str, available: bool) -> Result<usize> { use self::schema::ingredients; let new_ingrdient = NewIngredient { name: name, description: description, available: available, }; let size = try!(diesel::insert(&new_ingrdient) .into(ingredients::table) .execute(conn)); Ok(size) }