Adds simple delete api

This commit is contained in:
2019-02-02 15:56:44 +01:00
parent 46532eee9e
commit a7cc92f903
6 changed files with 32 additions and 3 deletions

1
.env Normal file
View File

@@ -0,0 +1 @@
DATABASE_URL="cookbook/db.sqlite3"

Binary file not shown.

View File

@@ -76,7 +76,7 @@ fn main() {
} }
let mut category_id = String::new(); let mut category_id = String::new();
stdin().read_line(&mut category_id).unwrap(); stdin().read_line(&mut category_id).unwrap();
let category_id = category_id.trim().parse::<i16>().expect("Could not parse id"); let category_id = category_id.trim().parse::<i16>().unwrap_or(0);
builder.set_category(category_id); builder.set_category(category_id);
println!("Ingredients (empty line to finish): "); println!("Ingredients (empty line to finish): ");

View File

@@ -30,6 +30,16 @@ pub mod recipes {
recipes.load::<Recipe>(conn) recipes.load::<Recipe>(conn)
.expect("Error loading recipe's list") .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()
}
} }

View File

@@ -9,10 +9,11 @@
<h1>Cook Assistant</h1> <h1>Cook Assistant</h1>
<!-- Details View --> <!-- Details View -->
<section v-if="active_view > -1"> <section v-if="active_view > -1">
<button @click="setActiveView(-1)">X close</button> <button @click="closeActiveView">X close</button>
<h4>{{ items[active_view].title }}</h4> <h4>{{ items[active_view].title }}</h4>
<h6>{{ categories[items[active_view].category].name }}</h6> <h6>{{ categories[items[active_view].category].name }}</h6>
<p><strong>{{ items[active_view].ingredients }}</strong></p> <p><strong>{{ items[active_view].ingredients }}</strong></p>
<button @click="deleteRecipe(active_view + 1)">DELETE !</button>
</section> </section>
<!-- Category List View --> <!-- Category List View -->
<section v-else> <section v-else>
@@ -57,6 +58,16 @@
setActiveView: function(id) { setActiveView: function(id) {
this.active_view = id - 1; 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() { fetchRecipesList: function() {
fetch("/api/list") fetch("/api/list")
.then((res) => res.json()) .then((res) => res.json())

View File

@@ -55,11 +55,18 @@ mod api {
.collect() .collect()
) )
} }
#[get("/delete/<id>")]
pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json<bool> {
Json(
recipes::delete(&conn, id)
)
}
} }
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.attach(api::CookbookDbConn::fairing()) .attach(api::CookbookDbConn::fairing())
.mount("/", routes![index]) .mount("/", routes![index])
.mount("/api", routes![api::recipes_list]).launch(); .mount("/api", routes![api::recipes_list, api::delete_recipe]).launch();
} }