Uses new IngredientListManager :)

This commit is contained in:
2019-02-03 21:42:45 +01:00
parent b14e566593
commit bcc0564f2a
5 changed files with 44 additions and 36 deletions

View File

@@ -1,35 +1,18 @@
extern crate cookbook;
extern crate diesel;
use self::cookbook::*;
use diesel::sqlite::SqliteConnection;
use cookbook::{
ingredients::IngredientList,
recipes::Recipe,
establish_connection,
recipes::{self, Recipe},
};
struct IngredientListManager<'a>(&'a SqliteConnection, IngredientList);
impl<'a> IngredientListManager<'a> {
fn display_lines(&self) -> String {
let mut repr = String::new();
for id in self.1.iter() {
let ingdt = ingredients::get(self.0, id).expect("Database integrity error");
repr.push_str(&format!("{}\n", ingdt.alias));
}
repr
}
}
fn main() {
let conn = establish_connection();
let result = recipes::load_all(&conn);
println!("Here are {} recipes [{}]:", result.len(), result.len() * std::mem::size_of::<Recipe>());
for rec in result {
println!("*************\n{}\n({:?})", rec.title, rec.category);
println!("-------------\n");
println!("{}", IngredientListManager(&conn, rec.ingredients).display_lines());
println!("{}", rec.ingredients.into_manager(&conn).display_lines());
}
}

View File

@@ -49,6 +49,26 @@ pub mod ingredients {
use super::{SqliteConnection, schema};
use super::diesel::prelude::*;
pub struct IngredientListManager<'a>(&'a SqliteConnection, IngredientList);
impl<'a> IngredientListManager<'a> {
pub fn display_lines(&self) -> String {
use self::get;
let mut repr = String::new();
for id in self.1.iter() {
let ingdt = get(self.0, id).expect("Database integrity error");
repr.push_str(&format!("{}\n", ingdt.alias));
}
repr
}
}
impl<'a> IngredientList {
pub fn into_manager(self, conn: &'a SqliteConnection) -> IngredientListManager<'a> {
IngredientListManager(conn, self)
}
}
/// Returns the id of the ingredient identifiable by `name: &str`
/// If the ingredient does not yet exists, it is inserted in database.
pub fn get_id_or_create(conn: &SqliteConnection, name: &str) -> Result<i32,String> {