Uses new IngredientListManager :)
This commit is contained in:
Binary file not shown.
@@ -1,35 +1,18 @@
|
|||||||
extern crate cookbook;
|
extern crate cookbook;
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
|
||||||
use self::cookbook::*;
|
|
||||||
|
|
||||||
use diesel::sqlite::SqliteConnection;
|
|
||||||
use cookbook::{
|
use cookbook::{
|
||||||
ingredients::IngredientList,
|
establish_connection,
|
||||||
recipes::Recipe,
|
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() {
|
fn main() {
|
||||||
|
|
||||||
let conn = establish_connection();
|
let conn = establish_connection();
|
||||||
let result = recipes::load_all(&conn);
|
let result = recipes::load_all(&conn);
|
||||||
println!("Here are {} recipes [{}]:", result.len(), result.len() * std::mem::size_of::<Recipe>());
|
println!("Here are {} recipes [{}]:", result.len(), result.len() * std::mem::size_of::<Recipe>());
|
||||||
for rec in result {
|
for rec in result {
|
||||||
println!("*************\n{}\n({:?})", rec.title, rec.category);
|
println!("*************\n{}\n({:?})", rec.title, rec.category);
|
||||||
println!("-------------\n");
|
println!("-------------\n");
|
||||||
println!("{}", IngredientListManager(&conn, rec.ingredients).display_lines());
|
println!("{}", rec.ingredients.into_manager(&conn).display_lines());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,26 @@ pub mod ingredients {
|
|||||||
use super::{SqliteConnection, schema};
|
use super::{SqliteConnection, schema};
|
||||||
use super::diesel::prelude::*;
|
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`
|
/// Returns the id of the ingredient identifiable by `name: &str`
|
||||||
/// If the ingredient does not yet exists, it is inserted in database.
|
/// 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> {
|
pub fn get_id_or_create(conn: &SqliteConnection, name: &str) -> Result<i32,String> {
|
||||||
|
|||||||
@@ -19,7 +19,10 @@
|
|||||||
<button @click="closeActiveView" class="button is-pulled-right">X close</button>
|
<button @click="closeActiveView" class="button is-pulled-right">X close</button>
|
||||||
<h4 class="title">{{ items[active_view].title }}</h4>
|
<h4 class="title">{{ items[active_view].title }}</h4>
|
||||||
<h6 class="subtitle">{{ categories[items[active_view].category].name }}</h6>
|
<h6 class="subtitle">{{ categories[items[active_view].category].name }}</h6>
|
||||||
<p><strong>{{ items[active_view].ingredients }}</strong></p>
|
<p><strong>Ingredients : </strong></p>
|
||||||
|
<ul>
|
||||||
|
<li v-for="ing in items[active_view].ingredients.split('\n')">{{ ing }}</li>
|
||||||
|
</ul>
|
||||||
<button @click="deleteRecipe(items[active_view].id)" class="button is-danger is-pulled-right">DELETE !</button>
|
<button @click="deleteRecipe(items[active_view].id)" class="button is-danger is-pulled-right">DELETE !</button>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -25,8 +25,9 @@ mod api {
|
|||||||
#[database("cookbook_db")]
|
#[database("cookbook_db")]
|
||||||
pub struct CookbookDbConn(diesel::SqliteConnection);
|
pub struct CookbookDbConn(diesel::SqliteConnection);
|
||||||
|
|
||||||
|
/// A serializable wrapper for [`cookbook::recipes::Recipe`]
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Recipe {
|
pub struct RecipeObject {
|
||||||
id: i32,
|
id: i32,
|
||||||
title: String,
|
title: String,
|
||||||
category: i16,
|
category: i16,
|
||||||
@@ -34,33 +35,34 @@ mod api {
|
|||||||
preparation: String,
|
preparation: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Recipe {
|
impl RecipeObject {
|
||||||
fn from(rec: recipes::Recipe) -> Recipe {
|
fn from(conn: &diesel::SqliteConnection, rec: recipes::Recipe) -> Self {
|
||||||
Recipe {
|
Self {
|
||||||
id: rec.id,
|
id: rec.id,
|
||||||
title: rec.title,
|
title: rec.title,
|
||||||
category: rec.category as i16,
|
category: rec.category as i16,
|
||||||
ingredients: rec.ingredients.as_string(),
|
ingredients: rec.ingredients
|
||||||
|
.into_manager(conn)
|
||||||
|
.display_lines(),
|
||||||
preparation: rec.preparation,
|
preparation: rec.preparation,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieves data from database and returns all recipes,
|
||||||
|
/// transformed into a js friendly [`RecipeObject`].
|
||||||
#[get("/list")]
|
#[get("/list")]
|
||||||
pub fn recipes_list(conn: CookbookDbConn) -> Json<Vec<Recipe>> {
|
pub fn recipes_list(conn: CookbookDbConn) -> Json<Vec<RecipeObject>> {
|
||||||
Json(
|
Json( recipes::load_all(&conn)
|
||||||
recipes::load_all(&conn)
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|r| Recipe::from(r))
|
.map(|r| RecipeObject::from(&conn, r))
|
||||||
.collect()
|
.collect() )
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete a recipe given it's `id`
|
||||||
#[get("/delete/<id>")]
|
#[get("/delete/<id>")]
|
||||||
pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json<bool> {
|
pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json<bool> {
|
||||||
Json(
|
Json( recipes::delete(&conn, id) )
|
||||||
recipes::delete(&conn, id)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user