diff --git a/controller.go b/controller.go index 10f2954..651eb0d 100644 --- a/controller.go +++ b/controller.go @@ -16,7 +16,10 @@ func (c *controller) LoadRouter(pub fs.FS) { serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) serveMux.HandleFunc("POST /order/new", c.postNewOrder) - serveMux.HandleFunc("GET /cake/options", c.cakeOptions) + serveMux.HandleFunc("GET /available_cakes", c.cakeOptions) + serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions) + serveMux.HandleFunc("GET /cake_editor", c.editCakes) + serveMux.HandleFunc("POST /new_cake", c.newCake) c.Handler = serveMux } diff --git a/handlers.go b/handlers.go index 89308d2..6040973 100644 --- a/handlers.go +++ b/handlers.go @@ -3,14 +3,55 @@ package main import ( "fmt" "net/http" + "strconv" + "strings" ) -func (c *controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { +func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) { type cake struct { Name string ID int } - c.tmpl["available_cakes"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) + 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}}}) +} + +func (c *controller) editCakes(w http.ResponseWriter, r *http.Request) { + type cake struct { + Name string + ID int + Price int + } + w.Header().Set("Content-Type", "text/html") + c.tmpl["cake_editor"].Execute(w, cake{"Hello", 1, 120}) +} + +func (c *controller) newCake(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + c.tmpl["alert"].Execute(w, err) + return + } + + type cake struct { + Name string + ID int + Price int + } + var out cake + out.Name = r.FormValue("name") + out.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")) + if err != nil { + c.tmpl["alert"].Execute(w, err) + return + } + _ = c.tmpl["cake"].Execute(w, out) } func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) { diff --git a/public/index.html b/public/index.html index 4e28837..617b667 100644 --- a/public/index.html +++ b/public/index.html @@ -46,7 +46,7 @@ -