diff --git a/README.md b/README.md index 0a44538..4cae5db 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # iroon ## todo: -- extract order creation form into it's own element -- option to add additional cakes -- saving orders on server -- displaying orders +- [ ] extract order creation form into it's own element +- [x] option to add additional cakes +- [x] saving orders on server +- [ ] displaying orders +- [ ] move id to cake struct +- [ ] refresh the list on submission +- [ ] peristent storage ## ux design ### adding an order: 1. user selects the _add order_ button diff --git a/controller.go b/controller.go index 79155e6..e5094a8 100644 --- a/controller.go +++ b/controller.go @@ -6,8 +6,14 @@ import ( "net/http" ) +type Cake struct { + Name string + Price int +} + type controller struct { - tmpl map[string]*template.Template + tmpl map[string]*template.Template + cakes map[int]Cake http.Handler } diff --git a/handlers.go b/handlers.go index 0fdb160..3e063cd 100644 --- a/handlers.go +++ b/handlers.go @@ -8,39 +8,37 @@ import ( ) func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) { - type cake struct { - Name string - ID int - } w.Header().Set("Content-Type", "text/html") 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) { - fmt.Println("received get request at ", r.URL.String()) - type cake struct { - Name string - ID int - Price int + type contents struct { + ID int + Cake Cake } url := strings.Split(r.URL.String(), "/") - if len(url) < 3 { - fmt.Println("no arg") - c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0}) + c.tmpl["cake_editor"].Execute(w, contents{-1, Cake{"", 0}}) return } id, err := strconv.Atoi(url[2]) if err != nil { - fmt.Println("can't get info") - c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0}) + c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}}) 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) { @@ -50,24 +48,30 @@ func (c *controller) newCake(w http.ResponseWriter, r *http.Request) { return } - type cake struct { - Name string - ID int - Price int + type contents struct { + ID int + Cake Cake } - var out cake - out.Name = r.FormValue("name") - out.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 { c.tmpl["alert"].Execute(w, err) return } - out.ID, err = strconv.Atoi(r.FormValue("id")) + in.ID, err = strconv.Atoi(r.FormValue("id")) if err != nil { c.tmpl["alert"].Execute(w, err) 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) { diff --git a/main.go b/main.go index e5d122d..0205ac3 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,10 @@ func main() { var c controller 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} err := http.ListenAndServe(":8080", c) if err != nil { diff --git a/templates/available_cakes.html b/templates/available_cakes.html index e4c5c6d..f554067 100644 --- a/templates/available_cakes.html +++ b/templates/available_cakes.html @@ -2,8 +2,9 @@