package main import ( "fmt" "net/http" "strconv" "strings" ) func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") template := strings.Split(r.URL.String(), "/")[1] c.tmpl[template].Execute(w, c.cakes) } 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}}) return } id, err := strconv.Atoi(url[2]) if err != nil { c.tmpl["cake_editor"].Execute(w, contents{0, Cake{"", 0}}) return } 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) { err := r.ParseForm() if err != nil { c.tmpl["alert"].Execute(w, err) 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")) if err != nil { c.tmpl["alert"].Execute(w, err) return } in.ID, err = strconv.Atoi(r.FormValue("id")) if err != nil { c.tmpl["alert"].Execute(w, err) return } 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) { err := r.ParseForm() if err != nil { _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: err}) } w.WriteHeader(http.StatusAccepted) fmt.Println(r.Form) _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: r.Form}) }