From 88107366bce6b7f7c788171a18c40ad102904ff4 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 1 Apr 2023 16:03:49 +0200 Subject: Implement recipe deletion --- data/templates/recipe_confirm_deletion.html | 26 ++++++++++++++++++++ data/templates/recipe_edit.html | 1 + handler.go | 38 +++++++++++++++++++++++++++++ mux.go | 11 +++++++++ server.go | 1 + 5 files changed, 77 insertions(+) create mode 100644 data/templates/recipe_confirm_deletion.html diff --git a/data/templates/recipe_confirm_deletion.html b/data/templates/recipe_confirm_deletion.html new file mode 100644 index 0000000..08c981d --- /dev/null +++ b/data/templates/recipe_confirm_deletion.html @@ -0,0 +1,26 @@ + + + + + {{ template "head.html" }} + + +
+ +

Delete a recipe

+
+ +
+

Recipe ID: {{.Id}}

+

Do you really want to delete this recipe?

+
+ +
+ + {{ template "footer.html" }} +
+ + diff --git a/data/templates/recipe_edit.html b/data/templates/recipe_edit.html index d622ce1..b35587e 100644 --- a/data/templates/recipe_edit.html +++ b/data/templates/recipe_edit.html @@ -18,6 +18,7 @@
{{.DescriptionMarkdown}}
+ {{ template "footer.html" }} diff --git a/handler.go b/handler.go index 096122e..aa231e5 100644 --- a/handler.go +++ b/handler.go @@ -188,6 +188,44 @@ func recipeEditPost(w http.ResponseWriter, r *http.Request) { updateRecipe(body, idStr) } +func recipeConfirmDeletionGet(w http.ResponseWriter, r *http.Request) { + + ids := r.URL.Query()["id"] + if len(ids) != 1 { + http.Error(w, "Exactly 1 'id' URL parameter expected.", 400) + return + } + idStr := ids[0] + + type Element struct { + Id string + } + var element Element + element.Id = idStr + + ServeTemplate(w, "recipe_confirm_deletion.html", element) +} + +func recipeConfirmDeletionPost(w http.ResponseWriter, r *http.Request) { + + ids := r.URL.Query()["id"] + if len(ids) != 1 { + http.Error(w, "Exactly 1 'id' URL parameter expected.", 400) + return + } + idStr := ids[0] + + cmd := fmt.Sprintf("DELETE FROM recipes where (id='%s');", idStr) + _, err := db.Query(cmd) + if err != nil { + fmt.Print(err) + http.Error(w, "Could not delete recipe.", 500) + return; + } + + http.Redirect(w, r, "/index.html", 303) +} + func updateRecipe(body string, idStr string) { _, _ = db.Exec(` diff --git a/mux.go b/mux.go index 9d9d09a..9fb0dad 100644 --- a/mux.go +++ b/mux.go @@ -33,6 +33,17 @@ func recipeEditMux(w http.ResponseWriter, r *http.Request) { } } +func recipeConfirmDeletionMux(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + recipeConfirmDeletionGet(w, r) + case "POST": + recipeConfirmDeletionPost(w, r) + default: + http.Error(w, "Bad Request", 400) + } +} + func addRecipesMux(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": diff --git a/server.go b/server.go index e91859c..4a685f2 100644 --- a/server.go +++ b/server.go @@ -10,6 +10,7 @@ func setupRoutes() { http.HandleFunc("/", indexMux) http.HandleFunc("/recipe", recipeMux) http.HandleFunc("/recipe/edit", recipeEditMux) + http.HandleFunc("/recipe/confirm-deletion", recipeConfirmDeletionMux) http.HandleFunc("/add_recipes", addRecipesMux) http.HandleFunc("/static/style.css", staticStyleMux) http.HandleFunc("/favicon.ico", faviconMux) -- cgit v1.2.3-70-g09d2