From a7cc92f903c1fd7e19c191b5695bec2d6be75c3b Mon Sep 17 00:00:00 2001 From: artus40 Date: Sat, 2 Feb 2019 15:56:44 +0100 Subject: [PATCH] Adds simple delete api --- .env | 1 + cookbook/db.sqlite3 | Bin 20480 -> 20480 bytes cookbook/src/bin/write_recipe.rs | 2 +- cookbook/src/lib.rs | 10 ++++++++++ web/html/index.html | 13 ++++++++++++- web/src/main.rs | 9 ++++++++- 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..e726e98 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL="cookbook/db.sqlite3" diff --git a/cookbook/db.sqlite3 b/cookbook/db.sqlite3 index 6d8c13757756b5f1b730edb29c9df7ea43c5069f..dd3a8c05e721384279b5e27d5bcbee28f19b8efc 100644 GIT binary patch delta 185 zcmZozz}T>Wae_3X+C&*=Ry77aoq&xg3-m?U`Cc&a^YP!|@8J*T*X6I~d%^c&v!K8Q zJ~b5%HU>iuRbHq39EFrrh2+C4iw>_$%tdcJx>y$p8QV diff --git a/cookbook/src/bin/write_recipe.rs b/cookbook/src/bin/write_recipe.rs index d3e5900..518ecc0 100644 --- a/cookbook/src/bin/write_recipe.rs +++ b/cookbook/src/bin/write_recipe.rs @@ -76,7 +76,7 @@ fn main() { } let mut category_id = String::new(); stdin().read_line(&mut category_id).unwrap(); - let category_id = category_id.trim().parse::().expect("Could not parse id"); + let category_id = category_id.trim().parse::().unwrap_or(0); builder.set_category(category_id); println!("Ingredients (empty line to finish): "); diff --git a/cookbook/src/lib.rs b/cookbook/src/lib.rs index 8f688d0..05d51c7 100644 --- a/cookbook/src/lib.rs +++ b/cookbook/src/lib.rs @@ -30,6 +30,16 @@ pub mod recipes { recipes.load::(conn) .expect("Error loading recipe's list") } + + pub fn delete(conn: &SqliteConnection, recipe_id: i32) -> bool { + use self::schema::recipes::dsl::*; + + diesel::delete( + recipes.filter( + id.eq(recipe_id))) + .execute(conn) + .is_ok() + } } diff --git a/web/html/index.html b/web/html/index.html index fd3019c..d0de205 100644 --- a/web/html/index.html +++ b/web/html/index.html @@ -9,10 +9,11 @@

Cook Assistant

- +

{{ items[active_view].title }}

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

{{ items[active_view].ingredients }}

+
@@ -57,6 +58,16 @@ setActiveView: function(id) { this.active_view = id - 1; }, + closeActiveView: function() { + this.active_view = -1; + }, + deleteRecipe: function(id) { + fetch("/api/delete/" + id) + .then((res) => res.json()) + .then((data) => console.log("Deleted :" + data)) + .catch((err) => console.error(err)); + this.closeActiveView(); + }, fetchRecipesList: function() { fetch("/api/list") .then((res) => res.json()) diff --git a/web/src/main.rs b/web/src/main.rs index 8811870..f532e43 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -55,11 +55,18 @@ mod api { .collect() ) } + + #[get("/delete/")] + pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json { + Json( + recipes::delete(&conn, id) + ) + } } fn main() { rocket::ignite() .attach(api::CookbookDbConn::fairing()) .mount("/", routes![index]) - .mount("/api", routes![api::recipes_list]).launch(); + .mount("/api", routes![api::recipes_list, api::delete_recipe]).launch(); }