Refactor postOrder to render confirmation page
This commit is contained in:
parent
47323b32f8
commit
0e762841a3
4 changed files with 38 additions and 55 deletions
|
|
@ -10,73 +10,47 @@ import (
|
|||
"github.com/Bronku/iroon/store"
|
||||
)
|
||||
|
||||
func (h *Server) postOrder(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||
cakes, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
fmt.Println("can't parse the form")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
fmt.Println("received form: ", r.Form)
|
||||
|
||||
var n store.Order
|
||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Println("can't parse order id: ", err)
|
||||
return
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
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.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
|
||||
n.Paid, err = strconv.Atoi(r.FormValue("paid"))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Println("can't parse order paid: ", err)
|
||||
return
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
n.Accepted = time.Now()
|
||||
|
||||
n.Cakes = make([]store.Cake, 0)
|
||||
cakes, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "server side error getting available cakes: ", err)
|
||||
return
|
||||
}
|
||||
for _, e := range cakes {
|
||||
value := r.FormValue(fmt.Sprintf("cake[%d]", e.ID))
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
amount, err := strconv.Atoi(value)
|
||||
e.Amount = amount
|
||||
e.Amount, err = strconv.Atoi(r.FormValue(fmt.Sprintf("cake[%d]", e.ID)))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
n.Cakes = append(n.Cakes, e)
|
||||
}
|
||||
|
||||
fmt.Println("parsed order: ", n)
|
||||
n.Accepted = time.Now()
|
||||
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.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
|
||||
h.s.SaveOrder(n)
|
||||
|
||||
w.Header().Set("content-type", "text/html")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
w.Write([]byte("accepted <a href='/'>back</a>"))
|
||||
n.ID, err = h.s.SaveOrder(n)
|
||||
return n, http.StatusAccepted, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue