summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-03-03 14:09:56 +0100
committerxengineering <me@xengineering.eu>2024-03-03 14:09:56 +0100
commit7cecddd5ecd95c8d559208f9292a3590f8c4cb37 (patch)
treed8e3340025425f489c9eac5a51d2c5b3694fbfee
parent78116f571ad4801bd10c51077b50ec87f1f68be7 (diff)
downloadceres-7cecddd5ecd95c8d559208f9292a3590f8c4cb37.tar
ceres-7cecddd5ecd95c8d559208f9292a3590f8c4cb37.tar.zst
ceres-7cecddd5ecd95c8d559208f9292a3590f8c4cb37.zip
controller: Provide recipe deletion via HTTP
-rw-r--r--controller/recipe.go17
-rw-r--r--main.go7
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()