From bcc0564f2a719e9683ff827ce2439550e92aca71 Mon Sep 17 00:00:00 2001 From: artus40 Date: Sun, 3 Feb 2019 21:42:45 +0100 Subject: [PATCH] Uses new IngredientListManager :) --- cookbook/db.sqlite3 | Bin 20480 -> 20480 bytes cookbook/src/bin/show_recipes.rs | 23 +++------------------- cookbook/src/lib.rs | 20 +++++++++++++++++++ web/html/index.html | 5 ++++- web/src/main.rs | 32 ++++++++++++++++--------------- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index 26e15ee1f6795614dfd26ecd74e9228733e8055b..acc34e4ab2439f396fec1d1cfedd4c3d7cd61b38 100644 GIT binary patch delta 137 zcmV;40CxX?paFoO0gxL3M3Edr0YtH2q%RBu55oWt?hn}y!wn&kw{8un(gTmk){$c@Ju{5fDrdlWI>z6$%ap0{|mZWnye^V`y(~4GIed J0|1l6Pdt3e9-aUI diff --git a/cookbook/src/bin/show_recipes.rs b/cookbook/src/bin/show_recipes.rs index 9926e4e..d02e0e9 100644 --- a/cookbook/src/bin/show_recipes.rs +++ b/cookbook/src/bin/show_recipes.rs @@ -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::()); 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()); } } diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index 60c84f7..da23aa0 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -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 { diff --git a/web/html/index.html b/web/html/index.html index ee2d32e..f84e719 100644 --- a/web/html/index.html +++ b/web/html/index.html @@ -19,7 +19,10 @@

{{ items[active_view].title }}

{{ categories[items[active_view].category].name }}
-

{{ items[active_view].ingredients }}

+

Ingredients :

+
    +
  • {{ ing }}
  • +
diff --git a/web/src/main.rs b/web/src/main.rs index 3bf8178..87b9972 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -25,8 +25,9 @@ mod api { #[database("cookbook_db")] pub struct CookbookDbConn(diesel::SqliteConnection); + /// A serializable wrapper for [`cookbook::recipes::Recipe`] #[derive(Serialize)] - pub struct Recipe { + pub struct RecipeObject { id: i32, title: String, category: i16, @@ -34,33 +35,34 @@ mod api { preparation: String, } - impl Recipe { - fn from(rec: recipes::Recipe) -> Recipe { - Recipe { + impl RecipeObject { + fn from(conn: &diesel::SqliteConnection, rec: recipes::Recipe) -> Self { + Self { id: rec.id, title: rec.title, category: rec.category as i16, - ingredients: rec.ingredients.as_string(), + ingredients: rec.ingredients + .into_manager(conn) + .display_lines(), preparation: rec.preparation, } } } + /// Retrieves data from database and returns all recipes, + /// transformed into a js friendly [`RecipeObject`]. #[get("/list")] - pub fn recipes_list(conn: CookbookDbConn) -> Json> { - Json( - recipes::load_all(&conn) - .into_iter() - .map(|r| Recipe::from(r)) - .collect() - ) + pub fn recipes_list(conn: CookbookDbConn) -> Json> { + Json( recipes::load_all(&conn) + .into_iter() + .map(|r| RecipeObject::from(&conn, r)) + .collect() ) } + /// Delete a recipe given it's `id` #[get("/delete/")] pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json { - Json( - recipes::delete(&conn, id) - ) + Json( recipes::delete(&conn, id) ) } }