summaryrefslogtreecommitdiff
path: root/view/recipe.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-03-03 15:05:15 +0100
committerxengineering <me@xengineering.eu>2024-03-03 15:05:15 +0100
commit25fe18973f11d3db61a1ae9ca05f2bd63d3edea9 (patch)
treee641a20fe27f3ef869478a1dc01c322d1082ce33 /view/recipe.go
parent6b0db50680a52e4ca7e6d0ee8ed3dae6a1093d16 (diff)
downloadceres-25fe18973f11d3db61a1ae9ca05f2bd63d3edea9.tar
ceres-25fe18973f11d3db61a1ae9ca05f2bd63d3edea9.tar.zst
ceres-25fe18973f11d3db61a1ae9ca05f2bd63d3edea9.zip
view: Replace edit by view parameter
The old edit URL parameter allowed to select one different HTML template. A more generic approach is to provide a view parameter which allows to use multiple alternative HTML templates for the same data defined by the Go struct. This makes implementing additional pages like a confirm page for recipe deletion easier.
Diffstat (limited to 'view/recipe.go')
-rw-r--r--view/recipe.go29
1 files changed, 24 insertions, 5 deletions
diff --git a/view/recipe.go b/view/recipe.go
index 1dd6045..25abc6b 100644
--- a/view/recipe.go
+++ b/view/recipe.go
@@ -18,13 +18,32 @@ func Recipe(w http.ResponseWriter, r *http.Request) {
return
}
- tmpl := "recipe"
- edit, ok := r.URL.Query()["edit"]
- if ok && len(edit) == 1 && edit[0] == "true" {
- tmpl = "recipe-edit"
+ template := "recipe"
+ view, ok := r.URL.Query()["view"]
+ if ok {
+ if len(view) > 1 {
+ http.Error(w, "More than one 'view' parameter given in URL", http.StatusBadRequest)
+ return
+ }
+ template = view[0]
}
- err = html.ExecuteTemplate(w, tmpl, recipe)
+ is_valid := false
+ valid_templates := []string{
+ "recipe",
+ "recipe-edit",
+ }
+ for _, v := range valid_templates {
+ if template == v {
+ is_valid = true
+ }
+ }
+ if !is_valid {
+ http.Error(w, "Unsupported view: " + template, http.StatusBadRequest)
+ return
+ }
+
+ err = html.ExecuteTemplate(w, template, recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return