diff options
author | xengineering <me@xengineering.eu> | 2024-10-24 17:24:00 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-10-24 20:14:14 +0200 |
commit | 9422e8194245c9cdbfa09b38c20173964349e2a2 (patch) | |
tree | b65f6b8682a97f81309022934c0a230d8481c4d3 | |
parent | dc37aaceea903fcc84144c7566a14b3c5378dcaf (diff) | |
download | ceres-9422e8194245c9cdbfa09b38c20173964349e2a2.tar ceres-9422e8194245c9cdbfa09b38c20173964349e2a2.tar.zst ceres-9422e8194245c9cdbfa09b38c20173964349e2a2.zip |
view: Order ingredient summary by ingredient name
This makes it easier to write a shopping list for the required
ingredients.
-rw-r--r-- | model/ingredient.go | 16 | ||||
-rw-r--r-- | view/html/recipe-confirm-deletion.html | 4 | ||||
-rw-r--r-- | view/html/recipe-edit.html | 22 | ||||
-rw-r--r-- | view/html/recipe.html | 28 | ||||
-rw-r--r-- | view/recipe.go | 34 |
5 files changed, 68 insertions, 36 deletions
diff --git a/model/ingredient.go b/model/ingredient.go index 9922060..e4570ff 100644 --- a/model/ingredient.go +++ b/model/ingredient.go @@ -45,6 +45,22 @@ func (i *Ingredient) Validate() error { return nil } +func (i Ingredient) String() string { + str := "" + + if i.Amount != "" { + str += i.Amount + " " + } + + if i.Unit != "" { + str += i.Unit + " " + } + + str += i.Type + + return str +} + func (i *Ingredient) Create(tx *sql.Tx) error { if i.Id != "" { return fmt.Errorf("Cannot create ingredient if ID is given") diff --git a/view/html/recipe-confirm-deletion.html b/view/html/recipe-confirm-deletion.html index 2bc9429..0e51681 100644 --- a/view/html/recipe-confirm-deletion.html +++ b/view/html/recipe-confirm-deletion.html @@ -9,8 +9,8 @@ </header> <main> <p>Do you really want to delete this recipe?</p> - <button onclick="window.location.href='/recipe/{{.Id}}';">cancel</button> - <button onclick="del('/recipe/{{.Id}}')">delete</button> + <button onclick="window.location.href='/recipe/{{.Recipe.Id}}';">cancel</button> + <button onclick="del('/recipe/{{.Recipe.Id}}')">delete</button> </main> {{ template "footer" }} <script src="/static/view/static/ceres.js"></script> diff --git a/view/html/recipe-edit.html b/view/html/recipe-edit.html index cd3ba86..9a18e75 100644 --- a/view/html/recipe-edit.html +++ b/view/html/recipe-edit.html @@ -8,24 +8,24 @@ <h1>Recipe editor</h1> </header> <main> - <form action="/recipe{{if ne .Id ""}}/{{.Id}}{{end}}" onsubmit="updateRecipe(event)"> - <input type="hidden" name="id" value="{{.Id}}"> - <input type="hidden" name="created" value="{{.Created}}"> - <input type="hidden" name="last_changed" value="{{.LastChanged}}"> + <form action="/recipe{{if ne .Recipe.Id ""}}/{{.Recipe.Id}}{{end}}" onsubmit="updateRecipe(event)"> + <input type="hidden" name="id" value="{{.Recipe.Id}}"> + <input type="hidden" name="created" value="{{.Recipe.Created}}"> + <input type="hidden" name="last_changed" value="{{.Recipe.LastChanged}}"> - <p><input type="text" name="title" value="{{.Title}}" placeholder="Title" required></p> - <p><input type="number" name="portions" value="{{.Portions}}" placeholder="Portions"></p> - <p><input type="text" name="url" value="{{.Url}}" placeholder="URL"></p> - <p><textarea name="notes" rows="4" cols="50" placeholder="Notes">{{.Notes}}</textarea></p> + <p><input type="text" name="title" value="{{.Recipe.Title}}" placeholder="Title" required></p> + <p><input type="number" name="portions" value="{{.Recipe.Portions}}" placeholder="Portions"></p> + <p><input type="text" name="url" value="{{.Recipe.Url}}" placeholder="URL"></p> + <p><textarea name="notes" rows="4" cols="50" placeholder="Notes">{{.Recipe.Notes}}</textarea></p> - <div id="steps">{{range .Steps}} + <div id="steps">{{range .Recipe.Steps}} {{template "recipe-step" .}}{{end}} </div> <button type="button" onclick="addNewStep();">add step</button> - <button type="submit">save</button>{{if eq .Id ""}} + <button type="submit">save</button>{{if eq .Recipe.Id ""}} <button onclick="window.location.href='/recipes';">cancel</button>{{else}} - <button onclick="window.location.href='/recipe/{{.Id}}';">cancel</button>{{end}} + <button onclick="window.location.href='/recipe/{{.Recipe.Id}}';">cancel</button>{{end}} </form> </main> {{ template "footer" }} diff --git a/view/html/recipe.html b/view/html/recipe.html index 7abcad4..cf30d06 100644 --- a/view/html/recipe.html +++ b/view/html/recipe.html @@ -1,30 +1,24 @@ {{define "recipe"}} -{{- $hasIngredients := false -}} -{{- range .Steps -}} - {{- if gt (len .Ingredients) 0 -}} - {{- $hasIngredients = true -}} - {{- end -}} -{{- end -}} <!DOCTYPE html> <html> {{ template "head" }} <body> <header> {{ template "nav" }} - <h1>{{.Title}}</h1> + <h1>{{.Recipe.Title}}</h1> </header> <main> <p> - <button onclick="window.location.href='/recipe/{{.Id}}?view=recipe-edit';">edit</button> - <button onclick="window.location.href='/recipe/{{.Id}}?view=recipe-confirm-deletion';">delete</button> - </p>{{ if ne .Notes "" }} - <p class="notice" style="white-space: pre-line;">{{.Notes}}</p>{{end}}{{ if ne .Portions "" }} - <p><i>Portions:</i> {{.Portions}}</p>{{end}}{{ if ne .Url "" }} - <p><i>Original recipe:</i> <a href="{{.Url}}">link</a></p>{{end}} - {{if $hasIngredients}}<p><i>Ingredient summary:</i></p> - <ul>{{range .Steps}}{{range $i, $el := .Ingredients}} - <li>{{if ne $el.Amount ""}}{{$el.Amount}} {{end}}{{if ne $el.Unit ""}}{{$el.Unit}} {{end}}{{$el.Type}}</li>{{end}}{{end}} - </ul>{{end}}{{range .Steps}} + <button onclick="window.location.href='/recipe/{{.Recipe.Id}}?view=recipe-edit';">edit</button> + <button onclick="window.location.href='/recipe/{{.Recipe.Id}}?view=recipe-confirm-deletion';">delete</button> + </p>{{ if ne .Recipe.Notes "" }} + <p class="notice" style="white-space: pre-line;">{{.Recipe.Notes}}</p>{{end}}{{ if ne .Recipe.Portions "" }} + <p><i>Portions:</i> {{.Recipe.Portions}}</p>{{end}}{{ if ne .Recipe.Url "" }} + <p><i>Original recipe:</i> <a href="{{.Recipe.Url}}">link</a></p>{{end}}{{if .HasIngredients}} + <p><i>Ingredient summary:</i></p> + <ul>{{range .Summary}} + <li>{{.}}</li>{{end}} + </ul>{{end}}{{range .Recipe.Steps}} <section> <p>{{.Text}}</p> <p><i>{{range $i, $el := .Ingredients}}{{if $i}}, {{end}}{{if ne $el.Amount ""}}{{$el.Amount}} {{end}}{{if ne $el.Unit ""}}{{$el.Unit}} {{end}}{{$el.Type}}{{end}}</i></p> 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 |