diff options
Diffstat (limited to 'view/recipe.go')
-rw-r--r-- | view/recipe.go | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/view/recipe.go b/view/recipe.go index 889f6ec..d274f85 100644 --- a/view/recipe.go +++ b/view/recipe.go @@ -2,6 +2,7 @@ package view import ( "net/http" + "sort" "xengineering.eu/ceres/model" ) @@ -9,16 +10,35 @@ import ( func RecipeRead(db *model.DB) http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - recipe := model.Recipe{} - recipe.Id = r.PathValue("id") + var data struct { + Recipe model.Recipe + HasIngredients bool + Summary []model.Ingredient + } + + data.Recipe.Id = r.PathValue("id") - var obj model.Object = &recipe + var obj model.Object = &data.Recipe err := db.Transaction(obj.Read) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + data.HasIngredients = false + for _, step := range data.Recipe.Steps { + if len(step.Ingredients) > 0 { + data.HasIngredients = true + } + for _, ingredient := range step.Ingredients { + data.Summary = append(data.Summary, ingredient) + } + } + + sort.Slice(data.Summary, func(i, j int) bool { + return data.Summary[i].Type < data.Summary[j].Type + }) + template := "recipe" view, ok := r.URL.Query()["view"] if ok { @@ -45,7 +65,7 @@ func RecipeRead(db *model.DB) http.Handler { return } - err = html.ExecuteTemplate(w, template, recipe) + err = html.ExecuteTemplate(w, template, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -57,9 +77,11 @@ func RecipeRead(db *model.DB) http.Handler { func RecipeCreate() http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - recipe := model.Recipe{} + var data struct { + Recipe model.Recipe + } - err := html.ExecuteTemplate(w, "recipe-edit", recipe) + err := html.ExecuteTemplate(w, "recipe-edit", data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return |