full form parsing

This commit is contained in:
bronku 2025-02-07 09:27:20 +01:00
parent 0570e47be9
commit 475d79f16e
5 changed files with 68 additions and 19 deletions

View file

@ -57,28 +57,49 @@ func (h *handler) addOrder(w http.ResponseWriter, r *http.Request) {
fmt.Println("can't parse order id: ", err)
return
}
n.Name = r.FormValue("name")
n.Surname = r.FormValue("surname")
n.Phone = r.FormValue("phone")
n.Location = r.FormValue("location")
n.Name = strings.TrimSpace(r.FormValue("name"))
n.Surname = strings.TrimSpace(r.FormValue("surname"))
n.Phone = strings.TrimSpace(r.FormValue("phone"))
n.Location = strings.TrimSpace(r.FormValue("location"))
n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order date: ", err)
return
}
n.Paid, err = strconv.Atoi(r.FormValue("paid"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order paid: ", err)
return
}
n.Cakes = make([]cake, 0)
h.s.saveOrder(n)
n.Accepted = time.Now()
n.Cakes = make([]cake, 0)
for _, e := range h.s.getCakes() {
value := r.FormValue(fmt.Sprintf("cake[%d]", e.ID))
if value == "" {
continue
}
amount, err := strconv.Atoi(value)
e.Amount = amount
if err != nil {
continue
}
n.Cakes = append(n.Cakes, e)
}
fmt.Println("parsed order: ", n)
h.s.saveOrder(n)
w.Header().Set("content-type", "text/html")
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("accepted <a href='/'>back</a>"))