From 616f8095e274977f876eaccf8f19370385cfaffb Mon Sep 17 00:00:00 2001 From: artus40 Date: Fri, 1 Feb 2019 15:58:58 +0100 Subject: [PATCH] Adds basic vuejs functionnality, implements RecipeCategory custom field --- cookbook/src/bin/show_recipes.rs | 2 +- cookbook/src/bin/write_recipe.rs | 4 ++- cookbook/src/models.rs | 61 ++++++++++++++++++++++++++++++-- cookbook/src/schema.rs | 2 +- web/index.html | 57 ++++++++++++++++++++++++----- 5 files changed, 112 insertions(+), 14 deletions(-) diff --git a/cookbook/src/bin/show_recipes.rs b/cookbook/src/bin/show_recipes.rs index 2d25df1..704f200 100644 --- a/cookbook/src/bin/show_recipes.rs +++ b/cookbook/src/bin/show_recipes.rs @@ -15,7 +15,7 @@ fn main() { println!("Here are {} recipes :", result.len()); for rec in result { - println!("*************\n{}", rec.title); + println!("*************\n{}\n({:?})", rec.title, rec.category); println!("-------------\n"); println!("{}", rec.ingredients); } diff --git a/cookbook/src/bin/write_recipe.rs b/cookbook/src/bin/write_recipe.rs index b0a3585..31a18d5 100644 --- a/cookbook/src/bin/write_recipe.rs +++ b/cookbook/src/bin/write_recipe.rs @@ -4,11 +4,12 @@ extern crate diesel; use std::io::{Read, stdin}; use diesel::SqliteConnection; use self::cookbook::*; -use self::models::NewRecipe; +use self::models::{NewRecipe, fields::RecipeCategory}; struct CreateRecipe<'a> { connection: &'a SqliteConnection, title: &'a str, + category: Option, ingredients: String, } @@ -17,6 +18,7 @@ impl<'a> CreateRecipe<'a> { CreateRecipe{ connection: conn, title: "New recipe", + category: None, ingredients: String::new(), } } diff --git a/cookbook/src/models.rs b/cookbook/src/models.rs index dcfa9e9..6066b8d 100644 --- a/cookbook/src/models.rs +++ b/cookbook/src/models.rs @@ -2,11 +2,66 @@ use super::schema::recipes; use super::schema::ingredients; use super::diesel::prelude::*; +pub mod fields { + use diesel::{ + backend::Backend, + sqlite::Sqlite, + sql_types::SmallInt, + deserialize::{self, FromSql}, + serialize::{self, Output, ToSql}, + }; + use std::io::Write; + + /// All recipes have a single associated category + /// representing the main use of the resulting preparation. + /// + /// It is stored as Integer + #[derive(Debug, Clone, FromSqlRow, AsExpression)] + #[repr(i16)] + pub enum RecipeCategory { + Breakfast = 0, + Starter = 1, + MainCourse = 2, + Dessert = 3 + } + + impl RecipeCategory { + fn from_id(id: i16) -> Option { + match id { + 0 => Some(RecipeCategory::Breakfast), + 1 => Some(RecipeCategory::Starter), + 2 => Some(RecipeCategory::MainCourse), + 3 => Some(RecipeCategory::Dessert), + _ => None, + } + } + } + + impl FromSql for RecipeCategory { + fn from_sql(bytes: Option<&::RawValue>) -> deserialize::Result { + if let Some(result) = RecipeCategory::from_id( + >::from_sql(bytes)?) + { Ok(result) } + else { Err("Invalid RecipeCategory id".into()) } + } + } + + impl ToSql for RecipeCategory { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + let as_int = self.clone() as i16; + >::to_sql(&as_int, out) + } + } + +} + + +/// Data for a recipe stored in DB #[derive(Debug, Clone, Queryable)] pub struct Recipe { pub id: i32, pub title: String, - pub category: i32, + pub category: fields::RecipeCategory, pub ingredients: String, pub preparation: String, } @@ -15,13 +70,13 @@ pub struct Recipe { #[table_name="recipes"] pub struct NewRecipe<'a> { pub title: &'a str, - pub category: i32, + pub category: i16, pub ingredients: &'a str, pub preparation: &'a str, } impl<'a> NewRecipe<'a> { - pub fn new(title: &'a str, category: i32, ingredients: &'a str, preparation: &'a str) -> Self { + pub fn new(title: &'a str, category: i16, ingredients: &'a str, preparation: &'a str) -> Self { NewRecipe{ title, category, diff --git a/cookbook/src/schema.rs b/cookbook/src/schema.rs index bfe1621..3c4974d 100644 --- a/cookbook/src/schema.rs +++ b/cookbook/src/schema.rs @@ -9,7 +9,7 @@ table! { recipes (id) { id -> Integer, title -> Text, - category -> Integer, + category -> SmallInt, ingredients -> Text, preparation -> Text, } diff --git a/web/index.html b/web/index.html index cc8b02e..38c9350 100644 --- a/web/index.html +++ b/web/index.html @@ -7,23 +7,64 @@

Cook Assistant

-

Fetch value from wasm : {{ value }}

- {{ message }} + +
+ +

{{ items[active_view].title }}

+

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

+
+ +
+
+
+ +
+
+
+ +

{{ categories[active_category].name }}

+ +
+