From fb4d24ef4489d0200a34d777b29cabf91103307c Mon Sep 17 00:00:00 2001 From: artus40 Date: Sun, 27 Jan 2019 21:52:46 +0100 Subject: [PATCH] building db support with diesel.rs --- cookbook/.env | 1 + cookbook/Cargo.toml | 3 ++ cookbook/db.sqlite3 | Bin 0 -> 20480 bytes cookbook/diesel.toml | 5 +++ cookbook/migrations/.gitkeep | 0 .../2019-01-27-201901_create_recipes/down.sql | 2 ++ .../2019-01-27-201901_create_recipes/up.sql | 8 +++++ cookbook/src/bin/show_recipes.rs | 23 ++++++++++++++ cookbook/src/lib.rs | 21 ++++++++++++ cookbook/src/models.rs | 8 +++++ cookbook/src/recipe.rs | 3 +- cookbook/src/schema.rs | 9 ++++++ planner/src/solver.rs | 6 ++-- web/index.html | 30 ++++++++++++++++++ 14 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 cookbook/.env create mode 100644 cookbook/db.sqlite3 create mode 100644 cookbook/diesel.toml create mode 100644 cookbook/migrations/.gitkeep create mode 100644 cookbook/migrations/2019-01-27-201901_create_recipes/down.sql create mode 100644 cookbook/migrations/2019-01-27-201901_create_recipes/up.sql create mode 100644 cookbook/src/bin/show_recipes.rs create mode 100644 cookbook/src/models.rs create mode 100644 cookbook/src/schema.rs create mode 100644 web/index.html 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 0000000000000000000000000000000000000000..4478b719c5546d290015a3cb6cc74d9a8b5c77ab GIT binary patch literal 20480 zcmeI(L66cv6bJB`?XIg$06h`2mv`_$lU-9lv$`io99LpNEXBC-&{P<)3AEUDjUIQu ziZ{Q9A3{HbXOB+VN+1z8@uvJ=I%&(inaulz%M{vw?u415H*qozGHSBxjC1yYh%vUa z)VRLBs@t}1zaszave;p__j{$v4`!52R{mP~R5r_>FB^n@2tWV=5P$##AOHafKmY;| z_)7u3|N2th+vDf2vf%YZC2APXR9c+2TdowIq%+6ZohQYANb98tUL z$z$nK&uw>vdqPj;33Z=&)Ezj^ex8$s*+kI`;kKR#ceQI6WNI8I9~YCX3=E@jqDG;L zvXngeQct-&?JQBVAPKTCj#hh1rge7AcdJ71^PBT>d|%I!s)?Tt->PZgPs8!Wz;u1j zRv~jy^!hxiy;n(^FQTZhcGtFkkCB{4e!kdtNA^9@=`HG^NAj^4I3BeIt}DBqzeutA zTUV-M!C4rM)Q9xlMDN%A;53Wp_x}22`StaEhW;ky`*~K3>p%S?AOHafKmY;|fB*y_009U<00I!$ i41wi$`!}OvR0#nHKmY;|fB*y_009U<00I!$5P_dAf5S}x literal 0 HcmV?d00001 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 }} +
+ + +