From 7bb54a56e993723b2867efbafddd1b9abfebcd9e Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 22 Apr 2023 19:28:54 +0200 Subject: Remove legacy Recipe struct --- handler.go | 30 ++++++++++-------------------- recipe.go | 13 +++++++++++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/handler.go b/handler.go index 8a514e7..c234d3d 100644 --- a/handler.go +++ b/handler.go @@ -15,18 +15,9 @@ const ( VALID_ID_REGEX = `^[0-9]+$` ) -type Recipe struct { - Id string - Title string - Text string - Html string -} - func indexGet(w http.ResponseWriter, r *http.Request) { list := getRecipeList() - sort.Sort(list) - ServeTemplate(w, "index.html", list) } @@ -64,16 +55,17 @@ func recipeEditGet(w http.ResponseWriter, r *http.Request) { } idStr := ids[0] - textpath := filepath.Join(config.Data, "recipes", idStr, "text") - data, _ := ioutil.ReadFile(textpath) - - recipe := Recipe{ - idStr, - "", - string(data), - "", + text, err := getRecipeText(idStr) + if err != nil { + http.Error(w, "Could not get recipe.", 400) + return } + recipe := struct{ + Id string + Text string + }{idStr, string(text)} + ServeTemplate(w, "recipe_edit.html", recipe) } @@ -113,9 +105,7 @@ func recipeConfirmDeletionGet(w http.ResponseWriter, r *http.Request) { return } - recipe := Recipe{ids[0], "", "", ""} - - ServeTemplate(w, "recipe_confirm_deletion.html", recipe) + ServeTemplate(w, "recipe_confirm_deletion.html", struct{Id string}{ids[0]}) } func recipeConfirmDeletionPost(w http.ResponseWriter, r *http.Request) { diff --git a/recipe.go b/recipe.go index 76c5204..a1a27e9 100644 --- a/recipe.go +++ b/recipe.go @@ -22,11 +22,20 @@ type recipe struct { } } +func getRecipeText(id string) ([]byte, error) { + var b []byte + textpath := filepath.Join(config.Data, "recipes", id, "text") + b, err := ioutil.ReadFile(textpath) + if err != nil { + return b, err + } + return b, nil +} + func getRecipe(id string) (recipe, error) { r := recipe{} - textpath := filepath.Join(config.Data, "recipes", id, "text") - data, err := ioutil.ReadFile(textpath) + data, err := getRecipeText(id) if err != nil { return r, err } -- cgit v1.2.3-70-g09d2