adding cakes 50% working
This commit is contained in:
parent
a09cee09c8
commit
cd7c9a9d0c
8 changed files with 57 additions and 38 deletions
11
README.md
11
README.md
|
|
@ -1,9 +1,12 @@
|
||||||
# iroon
|
# iroon
|
||||||
## todo:
|
## todo:
|
||||||
- extract order creation form into it's own element
|
- [ ] extract order creation form into it's own element
|
||||||
- option to add additional cakes
|
- [x] option to add additional cakes
|
||||||
- saving orders on server
|
- [x] saving orders on server
|
||||||
- displaying orders
|
- [ ] displaying orders
|
||||||
|
- [ ] move id to cake struct
|
||||||
|
- [ ] refresh the list on submission
|
||||||
|
- [ ] peristent storage
|
||||||
## ux design
|
## ux design
|
||||||
### adding an order:
|
### adding an order:
|
||||||
1. user selects the _add order_ button
|
1. user selects the _add order_ button
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,14 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Cake struct {
|
||||||
|
Name string
|
||||||
|
Price int
|
||||||
|
}
|
||||||
|
|
||||||
type controller struct {
|
type controller struct {
|
||||||
tmpl map[string]*template.Template
|
tmpl map[string]*template.Template
|
||||||
|
cakes map[int]Cake
|
||||||
http.Handler
|
http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
54
handlers.go
54
handlers.go
|
|
@ -8,39 +8,37 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
|
func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
|
||||||
type cake struct {
|
|
||||||
Name string
|
|
||||||
ID int
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
template := strings.Split(r.URL.String(), "/")[1]
|
template := strings.Split(r.URL.String(), "/")[1]
|
||||||
c.tmpl[template].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}})
|
c.tmpl[template].Execute(w, c.cakes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
|
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("received get request at ", r.URL.String())
|
type contents struct {
|
||||||
type cake struct {
|
ID int
|
||||||
Name string
|
Cake Cake
|
||||||
ID int
|
|
||||||
Price int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
url := strings.Split(r.URL.String(), "/")
|
url := strings.Split(r.URL.String(), "/")
|
||||||
|
|
||||||
if len(url) < 3 {
|
if len(url) < 3 {
|
||||||
fmt.Println("no arg")
|
c.tmpl["cake_editor"].Execute(w, contents{-1, Cake{"", 0}})
|
||||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(url[2])
|
id, err := strconv.Atoi(url[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("can't get info")
|
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
|
||||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.tmpl["cake_editor"].Execute(w, cake{"Hello", id, 120})
|
cake, exists := c.cakes[id]
|
||||||
|
if !exists {
|
||||||
|
c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out := contents{id, 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) {
|
||||||
|
|
@ -50,24 +48,30 @@ func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type cake struct {
|
type contents struct {
|
||||||
Name string
|
ID int
|
||||||
ID int
|
Cake Cake
|
||||||
Price int
|
|
||||||
}
|
}
|
||||||
var out cake
|
|
||||||
out.Name = r.FormValue("name")
|
var in contents
|
||||||
out.Price, err = strconv.Atoi(r.FormValue("price"))
|
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
|
||||||
}
|
}
|
||||||
out.ID, err = strconv.Atoi(r.FormValue("id"))
|
in.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.tmpl["alert"].Execute(w, err)
|
c.tmpl["alert"].Execute(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = c.tmpl["cake"].Execute(w, out)
|
|
||||||
|
if in.ID == -1 {
|
||||||
|
in.ID = len(c.cakes)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.cakes[in.ID] = in.Cake
|
||||||
|
_ = c.tmpl["cake"].Execute(w, in)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) {
|
func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
4
main.go
4
main.go
|
|
@ -19,6 +19,10 @@ func main() {
|
||||||
var c controller
|
var c controller
|
||||||
c.tmpl = LoadTemplates(templates)
|
c.tmpl = LoadTemplates(templates)
|
||||||
c.LoadRouter(public)
|
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}
|
||||||
|
|
||||||
err := http.ListenAndServe(":8080", c)
|
err := http.ListenAndServe(":8080", c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@
|
||||||
<h3>Cakes</h3>
|
<h3>Cakes</h3>
|
||||||
<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 .Cakes}}
|
{{range $key, $value := .}}
|
||||||
<button onclick="addCake('{{.ID}}', '{{.Name}}')">{{.Name}}</button>
|
<button onclick="addCake('{{$key}}', '{{$value.Name}} ({{$value.Price}})')">{{$value.Name}}
|
||||||
|
{{$value.Price}}</button>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
<div id="cake_preview">
|
<div id="cake_preview">
|
||||||
<ul>
|
<ul>
|
||||||
<li>ID: {{.ID}}</li>
|
<li>ID: {{.ID}}</li>
|
||||||
<li>Name: {{.Name}}</li>
|
<li>Name: {{.Cake.Name}}</li>
|
||||||
<li>Price: {{.Price}}</li>
|
<li>Price: {{.Cake.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>
|
||||||
|
|
@ -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="{{.Name}}">
|
<input type="text" id="name" name="name" required value="{{.Cake.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="{{.Price}}">
|
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Cake.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>
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,9 @@
|
||||||
<button hx-get="available_cakes" hx-swap="outerHTML" hx-target="closest div">back</button>
|
<button hx-get="available_cakes" hx-swap="outerHTML" hx-target="closest div">back</button>
|
||||||
<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 .Cakes}}
|
{{range $key, $value := .}}
|
||||||
<button hx-get="cake_editor/{{.ID}}" hx-swap="innerHTML" hx-target="#cake_editor">{{.Name}}</button>
|
<button hx-get="cake_editor/{{$key}}" hx-swap="innerHTML" hx-target="#cake_editor">{{$value.Name}}
|
||||||
|
{{$value.Price}}</button>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<div id="cake_editor"></div>
|
<div id="cake_editor"></div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue