From 25fe18973f11d3db61a1ae9ca05f2bd63d3edea9 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 3 Mar 2024 15:05:15 +0100 Subject: 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. --- view/recipe.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'view/recipe.go') 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 -- cgit v1.2.3-70-g09d2