Adds simple delete api
This commit is contained in:
Binary file not shown.
@@ -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): ");
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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())
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user