diff options
author | xengineering <me@xengineering.eu> | 2023-04-22 17:40:02 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-04-22 17:54:57 +0200 |
commit | 17f2abc3961fb854fb127f6b99c30ebb494b8e3d (patch) | |
tree | 5659716de60641fba08742e386e386e1a31a9a8f | |
parent | cff4718b1acd5a34e8dceaf4d21d4bc11c0de8d6 (diff) | |
download | ceres-17f2abc3961fb854fb127f6b99c30ebb494b8e3d.tar ceres-17f2abc3961fb854fb127f6b99c30ebb494b8e3d.tar.zst ceres-17f2abc3961fb854fb127f6b99c30ebb494b8e3d.zip |
Implement basic recipe target with JSON
-rw-r--r-- | data/templates/recipe.html | 19 | ||||
-rw-r--r-- | handler.go | 20 | ||||
-rw-r--r-- | recipe.go | 37 |
3 files changed, 64 insertions, 12 deletions
diff --git a/data/templates/recipe.html b/data/templates/recipe.html index 348a44d..7bbb660 100644 --- a/data/templates/recipe.html +++ b/data/templates/recipe.html @@ -11,11 +11,26 @@ <a href="/index.html">HOME</a> <a href="/add_recipes">add recipe</a> </nav> - <h1>{{.Title}}</h1> + <h1>{{.Recipe.Title}}</h1> </header> <main> - {{.Html}} + <p>This recipe is written for {{.Recipe.Portions}} portions.</p> + + <p><a href="{{.Recipe.Url}}">Link</a></p> + + <hr> + + {{range .Recipe.Steps}} + <p>{{.Text}}</p> + <ul> + {{range .Ingredients}} + <li>{{.Amount}} {{.Type}}</li> + {{end}} + </ul> + <hr> + {{end}} + <a href="/recipe/edit?id={{.Id}}"><button>edit</button></a> {{ template "footer.html" }} @@ -63,18 +63,18 @@ func recipeGet(w http.ResponseWriter, r *http.Request) { } idStr := ids[0] - textpath := filepath.Join(config.Data, "recipes", idStr, "text") - data, _ := ioutil.ReadFile(textpath) - markup := Markup(data) - - recipe := Recipe{ - idStr, - markup.title(), - string(data), - markup.html(), + rec, err := getRecipe(idStr) + if err != nil { + http.Error(w, "Could not get recipe.", 400) + return } - ServeTemplate(w, "recipe.html", recipe) + data := struct{ + Id string + Recipe recipe + }{idStr, rec} + + ServeTemplate(w, "recipe.html", data) } func recipeEditGet(w http.ResponseWriter, r *http.Request) { diff --git a/recipe.go b/recipe.go new file mode 100644 index 0000000..167f690 --- /dev/null +++ b/recipe.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "path/filepath" + "io/ioutil" +) + +type recipe struct { + Title string + Portions int + Url string + Steps []struct { + Text string + Ingredients []struct { + Type string + Amount any + } + } +} + +func getRecipe(id string) (recipe, error) { + r := recipe{} + + textpath := filepath.Join(config.Data, "recipes", id, "text") + data, err := ioutil.ReadFile(textpath) + if err != nil { + return r, err + } + + err = json.Unmarshal(data, &r) + if err != nil { + return r, err + } + + return r, nil +} |