From 7cecddd5ecd95c8d559208f9292a3590f8c4cb37 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 3 Mar 2024 14:09:56 +0100 Subject: controller: Provide recipe deletion via HTTP --- controller/recipe.go | 17 ++++++++++++++++- main.go | 7 ++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/controller/recipe.go b/controller/recipe.go index 4d5de53..259e3bc 100644 --- a/controller/recipe.go +++ b/controller/recipe.go @@ -6,9 +6,11 @@ import ( "net/http" "xengineering.eu/ceres/model" + + "github.com/gorilla/mux" ) -func Recipe(w http.ResponseWriter, r *http.Request) { +func RecipeUpdate(w http.ResponseWriter, r *http.Request) { buf, err := io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -30,3 +32,16 @@ func Recipe(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/recipe/" + recipe.Id, http.StatusSeeOther) } + +func RecipeDelete(w http.ResponseWriter, r *http.Request) { + recipe := model.Recipe{} + recipe.Id = mux.Vars(r)[`id`] + + err := recipe.Delete() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + http.Redirect(w, r, "/recipes", http.StatusSeeOther) +} diff --git a/main.go b/main.go index bcc5aa7..aa04065 100644 --- a/main.go +++ b/main.go @@ -44,10 +44,15 @@ func startServer(addr string) *http.Server { r.PathPrefix("/static/"). Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static)))) + r.HandleFunc("/recipes", view.Recipes).Methods(`GET`) + r.HandleFunc("/recipe/{id:[0-9]+}", view.Recipe).Methods(`GET`) - r.HandleFunc("/recipe", controller.Recipe).Methods(`POST`) + r.HandleFunc("/recipe", controller.RecipeUpdate).Methods(`POST`) + r.HandleFunc("/recipe/{id:[0-9]+}", controller.RecipeDelete).Methods(`DELETE`) + r.HandleFunc("/favicon.ico", view.Favicon).Methods(`GET`) + r.HandleFunc("/", view.Index).Methods(`GET`) muxer := http.NewServeMux() -- cgit v1.2.3-70-g09d2