Adds ingredients managing
This commit is contained in:
Binary file not shown.
@@ -25,10 +25,18 @@ impl<'a> CreateRecipe<'a> {
|
|||||||
self.title = title;
|
self.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_ingredient(&mut self, ingredient: String) {
|
fn add_ingredient(&mut self, name: String) {
|
||||||
// TODO: Validate ingredient
|
use crate::ingredients::*;
|
||||||
self.ingredients.push_str(&ingredient);
|
|
||||||
println!("+");
|
// Check it exists or create
|
||||||
|
if let Some(ingdt) = find(self.connection, &name) {
|
||||||
|
println!("=");
|
||||||
|
} else {
|
||||||
|
create(self.connection, &name);
|
||||||
|
println!("+{}", &name);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.ingredients.push_str(&name);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(self) {
|
fn insert(self) {
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ pub mod schema;
|
|||||||
pub mod models;
|
pub mod models;
|
||||||
|
|
||||||
mod recipe;
|
mod recipe;
|
||||||
mod storage;
|
|
||||||
mod importer;
|
mod importer;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use std::env;
|
use std::env;
|
||||||
use crate::models::{Recipe, NewRecipe, Ingredient, NewIngredient};
|
use crate::models::{Recipe, NewRecipe};
|
||||||
|
|
||||||
pub fn establish_connection() -> SqliteConnection {
|
pub fn establish_connection() -> SqliteConnection {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
@@ -39,10 +38,36 @@ pub fn create_recipe(conn: &SqliteConnection, title: &str, ingdts: &str) -> usiz
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_ingredient(conn: &SqliteConnection, alias: &str) -> Option<Ingredient> {
|
pub mod ingredients {
|
||||||
// Find ingredient by alias
|
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
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -34,23 +34,3 @@ pub struct Ingredient {
|
|||||||
/// Associated tags to constraint planning
|
/// Associated tags to constraint planning
|
||||||
tags: FoodTags,
|
tags: FoodTags,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ingredient {
|
|
||||||
/// Builds a brand new ingredient with given name as first alias
|
|
||||||
pub(super) fn new(name: String) -> Ingredient {
|
|
||||||
Ingredient {
|
|
||||||
alias: vec![name],
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Find an existing ingredient by alias
|
|
||||||
pub fn find(alias: &str) -> Option<Ingredient> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the ingredient most common name
|
|
||||||
pub fn name(&self) -> &str {
|
|
||||||
self.alias.first().expect("Ingredient without any name !")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
//! Storage backend for persistent data
|
|
||||||
//!
|
|
||||||
//! Data is stored in a sqlite file.
|
|
||||||
//! We'll try to use Diesel-rs, but not ready yet to grasp it all.
|
|
||||||
Reference in New Issue
Block a user