move id to cake struct

This commit is contained in:
Bronku 2025-02-01 07:29:24 +01:00
parent cd7c9a9d0c
commit 7515010afd
8 changed files with 20 additions and 29 deletions

View file

@ -4,9 +4,10 @@
- [x] option to add additional cakes
- [x] saving orders on server
- [ ] displaying orders
- [ ] move id to cake struct
- [x] move id to cake struct
- [ ] refresh the list on submission
- [ ] peristent storage
- [ ] rudimentary css
## ux design
### adding an order:
1. user selects the _add order_ button

View file

@ -9,6 +9,7 @@ import (
type Cake struct {
Name string
Price int
ID int
}
type controller struct {

View file

@ -14,31 +14,25 @@ func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
}
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
type contents struct {
ID int
Cake Cake
}
url := strings.Split(r.URL.String(), "/")
if len(url) < 3 {
c.tmpl["cake_editor"].Execute(w, contents{-1, Cake{"", 0}})
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
return
}
id, err := strconv.Atoi(url[2])
if err != nil {
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
return
}
cake, exists := c.cakes[id]
if !exists {
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
return
}
out := contents{id, cake}
c.tmpl["cake_editor"].Execute(w, out)
c.tmpl["cake_editor"].Execute(w, cake)
}
func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
@ -48,14 +42,9 @@ func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
return
}
type contents struct {
ID int
Cake Cake
}
var in contents
in.Cake.Name = r.FormValue("name")
in.Cake.Price, err = strconv.Atoi(r.FormValue("price"))
var in Cake
in.Name = r.FormValue("name")
in.Price, err = strconv.Atoi(r.FormValue("price"))
if err != nil {
c.tmpl["alert"].Execute(w, err)
return
@ -70,7 +59,7 @@ func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
in.ID = len(c.cakes)
}
c.cakes[in.ID] = in.Cake
c.cakes[in.ID] = in
_ = c.tmpl["cake"].Execute(w, in)
}

View file

@ -20,9 +20,9 @@ func main() {
c.tmpl = LoadTemplates(templates)
c.LoadRouter(public)
c.cakes = make(map[int]Cake)
c.cakes[0] = Cake{"Sernik", 120}
c.cakes[1] = Cake{"Malinowa chmórka", 120}
c.cakes[2] = Cake{"Beza Pavlova", 8}
c.cakes[0] = Cake{"Sernik", 120, 0}
c.cakes[1] = Cake{"Malinowa chmórka", 120, 1}
c.cakes[2] = Cake{"Beza Pavlova", 8, 2}
err := http.ListenAndServe(":8080", c)
if err != nil {

View file

@ -3,7 +3,7 @@
<button hx-get="edit_cakes" hx-swap="outerHTML" hx-target="closest div">edit</button>
<div id="cakes">
{{range $key, $value := .}}
<button onclick="addCake('{{$key}}', '{{$value.Name}} ({{$value.Price}})')">{{$value.Name}}
<button onclick="addCake('{{$value.ID}}', '{{$value.Name}} ({{$value.Price}})')">{{$value.Name}}
{{$value.Price}}</button>
{{end}}
</div>

View file

@ -1,8 +1,8 @@
<div id="cake_preview">
<ul>
<li>ID: {{.ID}}</li>
<li>Name: {{.Cake.Name}}</li>
<li>Price: {{.Cake.Price}}</li>
<li>Name: {{.Name}}</li>
<li>Price: {{.Price}}</li>
</ul>
<button type="button" onClick="document.getElementById('cake_preview').remove()">close</button>
</div>

View file

@ -1,11 +1,11 @@
<form hx-post="new_cake" hx-swap="outerHTML">
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required value="{{.Cake.Name}}">
<input type="text" id="name" name="name" required value="{{.Name}}">
</div>
<div>
<label for="price">Price</label>
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Cake.Price}}">
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Price}}">
</div>
<input value="{{.ID}}" name="id" hidden></input>
<button type="submit">Submit</button>

View file

@ -4,7 +4,7 @@
<button hx-get="cake_editor" hx-swap="innerHTML" hx-target="#cake_editor">New</button>
<div id="cakes">
{{range $key, $value := .}}
<button hx-get="cake_editor/{{$key}}" hx-swap="innerHTML" hx-target="#cake_editor">{{$value.Name}}
<button hx-get="cake_editor/{{$value.ID}}" hx-swap="innerHTML" hx-target="#cake_editor">{{$value.Name}}
{{$value.Price}}</button>
{{end}}
</div>