From 473052ed8f2c83052ed5b47a7f4cec68ac2621a6 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 13 Oct 2024 19:52:28 +0200 Subject: model: Replace global db variable by custom type Reducing global variables makes it easier to understand functions independently of the rest of the code. Adding the new model.DB type as a custom variant of the sql.DB type makes it possible to write methods for the database which makes the code way more readable. --- view/recipes.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'view/recipes.go') diff --git a/view/recipes.go b/view/recipes.go index fe995b2..e7153cd 100644 --- a/view/recipes.go +++ b/view/recipes.go @@ -6,19 +6,21 @@ import ( "xengineering.eu/ceres/model" ) -func RecipesRead(w http.ResponseWriter, r *http.Request) { - recipes := make(model.Recipes, 0) +func RecipesRead(db *model.DB) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + recipes := make(model.Recipes, 0) - var obj model.Object = &recipes - err := model.Transaction(obj.Read) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + var obj model.Object = &recipes + err := db.Transaction(obj.Read) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - err = html.ExecuteTemplate(w, "recipes", recipes) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + err = html.ExecuteTemplate(w, "recipes", recipes) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } } -- cgit v1.2.3-70-g09d2