Adds ingredients managing

This commit is contained in:
2019-01-28 21:25:25 +01:00
parent 202d7c5976
commit 4576ffb03b
5 changed files with 42 additions and 33 deletions

View File

@@ -6,13 +6,12 @@ pub mod schema;
pub mod models;
mod recipe;
mod storage;
mod importer;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;
use crate::models::{Recipe, NewRecipe, Ingredient, NewIngredient};
use crate::models::{Recipe, NewRecipe};
pub fn establish_connection() -> SqliteConnection {
dotenv().ok();
@@ -39,11 +38,37 @@ pub fn create_recipe(conn: &SqliteConnection, title: &str, ingdts: &str) -> usiz
}
pub fn find_ingredient(conn: &SqliteConnection, alias: &str) -> Option<Ingredient> {
// Find ingredient by alias
None
pub mod ingredients {
use crate::models::{Ingredient, NewIngredient};
use super::{SqliteConnection, schema};
use super::diesel::prelude::*;
pub fn find(conn: &SqliteConnection, name: &str) -> Option<Ingredient> {
use self::schema::ingredients::dsl::*;
let results = ingredients.filter(alias.like(name))
.limit(1)
.load::<Ingredient>(conn)
.expect("Error finding ingredient");
if !results.is_empty() {
Some(results.into_iter().nth(0).unwrap())
} else {
None
}
}
pub fn create(conn: &SqliteConnection, name: &str) -> usize {
use self::schema::ingredients;
diesel::insert_into(ingredients::table)
.values(&NewIngredient { alias: name })
.execute(conn)
.expect("Error inserting ingredient")
}
}
#[cfg(test)]
mod tests {
#[test]