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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ func (h *Server) loadHandler() {
|
|||
|
||||
mux.HandleFunc("GET /order/", h.render(h.getOrder, "order.html"))
|
||||
mux.HandleFunc("GET /", h.render(h.index, "index.html"))
|
||||
mux.HandleFunc("POST /order/", h.postOrder)
|
||||
mux.HandleFunc("POST /order/", h.render(h.postOrder, "confirmation.html"))
|
||||
|
||||
h.Handler = mux
|
||||
}
|
||||
|
|
|
|||
9
server/templates/confirmation.html
Normal file
9
server/templates/confirmation.html
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<header>
|
||||
<h1>Confirmation</h1>
|
||||
</header>
|
||||
<nav>
|
||||
<a href="/">index</a>
|
||||
</nav>
|
||||
<main>
|
||||
<h2>Order {{.ID}} for {{.Name}} {{.Surname}} confirmed</h2>
|
||||
</main>
|
||||
|
|
@ -18,15 +18,15 @@
|
|||
<input type="tel" name="phone" value="{{.Phone}}">
|
||||
<label>location</label>
|
||||
<select name="location">
|
||||
<option value="Kartuzy">Kartuzy</option>
|
||||
<option value="Somonino">Somonino</option>
|
||||
<option {{ if eq .Location "Kartuzy" }}selected{{ end }} value="Kartuzy">Kartuzy</option>
|
||||
<option {{ if eq .Location "Somonino" }}selected{{ end }} value="Somonino">Somonino</option>
|
||||
</select>
|
||||
<label>date</label>
|
||||
<input type="date" name="date" value="{{.Date.Format "2006-01-02"}}">
|
||||
<label>status</label>
|
||||
<select name="status" id="status" >
|
||||
<option value="accepted">accepted</option>
|
||||
<option value="done">done</option>
|
||||
<option {{ if eq .Status "accepted" }}selected{{ end }} value="accepted">accepted</option>
|
||||
<option {{ if eq .Status "done" }}selected{{ end }} value="done">done</option>
|
||||
</select>
|
||||
<label>paid</label>
|
||||
<input type="number" name="paid" min="0" value="{{.Paid}}">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue