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] option to add additional cakes
- [x] saving orders on server - [x] saving orders on server
- [ ] displaying orders - [ ] displaying orders
- [ ] move id to cake struct - [x] move id to cake struct
- [ ] refresh the list on submission - [ ] refresh the list on submission
- [ ] peristent storage - [ ] peristent storage
- [ ] rudimentary css
## ux design ## ux design
### adding an order: ### adding an order:
1. user selects the _add order_ button 1. user selects the _add order_ button

View file

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

View file

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

View file

@ -3,7 +3,7 @@
<button hx-get="edit_cakes" hx-swap="outerHTML" hx-target="closest div">edit</button> <button hx-get="edit_cakes" hx-swap="outerHTML" hx-target="closest div">edit</button>
<div id="cakes"> <div id="cakes">
{{range $key, $value := .}} {{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> {{$value.Price}}</button>
{{end}} {{end}}
</div> </div>

View file

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

View file

@ -1,11 +1,11 @@
<form hx-post="new_cake" hx-swap="outerHTML"> <form hx-post="new_cake" hx-swap="outerHTML">
<div> <div>
<label for="name">Name</label> <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>
<div> <div>
<label for="price">Price</label> <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> </div>
<input value="{{.ID}}" name="id" hidden></input> <input value="{{.ID}}" name="id" hidden></input>
<button type="submit">Submit</button> <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> <button hx-get="cake_editor" hx-swap="innerHTML" hx-target="#cake_editor">New</button>
<div id="cakes"> <div id="cakes">
{{range $key, $value := .}} {{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> {{$value.Price}}</button>
{{end}} {{end}}
</div> </div>