diff --git a/cookbook/.env b/cookbook/.env new file mode 100644 index 0000000..4ccaeb0 --- /dev/null +++ b/cookbook/.env @@ -0,0 +1 @@ +DATABASE_URL=db.sqlite3 diff --git a/cookbook/Cargo.toml b/cookbook/Cargo.toml index a460661..a5b60ec 100644 --- a/cookbook/Cargo.toml +++ b/cookbook/Cargo.toml @@ -5,3 +5,6 @@ authors = ["artus "] edition = "2018" [dependencies] +libsqlite3-sys = { version = "*", features = ["bundled"] } +diesel = { version = "1.4.1", features = ["sqlite"] } +dotenv = "0.9.0" diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 new file mode 100644 index 0000000..4478b71 Binary files /dev/null and b/cookbook/db.sqlite3 differ diff --git a/cookbook/diesel.toml b/cookbook/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/cookbook/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/cookbook/migrations/.gitkeep b/cookbook/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/cookbook/migrations/2019-01-27-201901_create_recipes/down.sql b/cookbook/migrations/2019-01-27-201901_create_recipes/down.sql new file mode 100644 index 0000000..cbda4b8 --- /dev/null +++ b/cookbook/migrations/2019-01-27-201901_create_recipes/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE recipes diff --git a/cookbook/migrations/2019-01-27-201901_create_recipes/up.sql b/cookbook/migrations/2019-01-27-201901_create_recipes/up.sql new file mode 100644 index 0000000..709cf18 --- /dev/null +++ b/cookbook/migrations/2019-01-27-201901_create_recipes/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE recipes ( + id INTEGER PRIMARY KEY NOT NULL, + title VARCHAR NOT NULL, + category INTEGER NOT NULL, + ingredients TEXT NOT NULL, + preparation TEXT NOT NULL +) diff --git a/cookbook/src/bin/show_recipes.rs b/cookbook/src/bin/show_recipes.rs new file mode 100644 index 0000000..2b5fb0c --- /dev/null +++ b/cookbook/src/bin/show_recipes.rs @@ -0,0 +1,23 @@ +extern crate cookbook; +extern crate diesel; + +use self::cookbook::*; +use self::models::*; +use self::diesel::prelude::*; + +fn main() { + use cookbook::schema::recipes::dsl::*; + + let conn = establish_connection(); + let result = recipes + .limit(5) + .load::(&conn) + .expect("Error loading recipes"); + + println!("Here are {} recipes :", result.len()); + for rec in result { + println!("{}", rec.title); + println!("-------------\n"); + println!("{}", rec.ingredients); + } +} diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index 30c1801..9f1d396 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -1,7 +1,28 @@ +#[macro_use] +extern crate diesel; +extern crate dotenv; + +pub mod schema; +pub mod models; + mod recipe; mod storage; mod importer; +use diesel::prelude::*; +use dotenv::dotenv; +use std::env; + +pub fn establish_connection() -> SqliteConnection { + dotenv().ok(); + + let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set !"); + SqliteConnection::establish(&db_url) + .expect(&format!("Error connecting to {}", db_url)) +} + + + #[cfg(test)] mod tests { #[test] diff --git a/cookbook/src/models.rs b/cookbook/src/models.rs new file mode 100644 index 0000000..53e04c2 --- /dev/null +++ b/cookbook/src/models.rs @@ -0,0 +1,8 @@ +#[derive(Queryable)] +pub struct Recipe { + pub id: i32, + pub title: String, + pub category: i32, + pub ingredients: String, + pub preparation: String, +} diff --git a/cookbook/src/recipe.rs b/cookbook/src/recipe.rs index c71c5e9..9076fe5 100644 --- a/cookbook/src/recipe.rs +++ b/cookbook/src/recipe.rs @@ -40,8 +40,7 @@ impl Ingredient { pub(super) fn new(name: String) -> Ingredient { Ingredient { alias: vec![name], - .. - Default::default() + ..Default::default() } } diff --git a/cookbook/src/schema.rs b/cookbook/src/schema.rs new file mode 100644 index 0000000..21e9919 --- /dev/null +++ b/cookbook/src/schema.rs @@ -0,0 +1,9 @@ +table! { + recipes (id) { + id -> Integer, + title -> Text, + category -> Integer, + ingredients -> Text, + preparation -> Text, + } +} diff --git a/planner/src/solver.rs b/planner/src/solver.rs index 52aafc2..c041869 100644 --- a/planner/src/solver.rs +++ b/planner/src/solver.rs @@ -122,8 +122,7 @@ impl<'a, V> ProblemBuilder<'a, V> { }) } - pub fn add_variable( - mut self, + pub fn add_variable(mut self, name: String, domain: &'a Domain, value: Option<&'a V>, @@ -153,8 +152,7 @@ mod tests { let mut problem: Problem<_> = Problem::build() .add_variable(String::from("Left"), &domain, None) .add_variable(String::from("Right"), &domain, None) - .add_constraint( - |assign: &Variables| { + .add_constraint(|assign: &Variables| { assign.get("Left").unwrap() == assign.get("Right").unwrap() }) .finish(); diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..cc8b02e --- /dev/null +++ b/web/index.html @@ -0,0 +1,30 @@ + + + + + + + +
+

Cook Assistant

+

Fetch value from wasm : {{ value }}

+ {{ message }} +
+ + +