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"
|
"github.com/Bronku/iroon/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Server) postOrder(w http.ResponseWriter, r *http.Request) {
|
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||||
err := r.ParseForm()
|
cakes, err := h.s.GetCakes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("can't parse the form")
|
return nil, http.StatusInternalServerError, err
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
}
|
||||||
return
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
return nil, http.StatusBadRequest, err
|
||||||
}
|
}
|
||||||
fmt.Println("received form: ", r.Form)
|
|
||||||
|
|
||||||
var n store.Order
|
var n store.Order
|
||||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
return nil, http.StatusBadRequest, err
|
||||||
fmt.Println("can't parse order id: ", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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"))
|
n.Paid, err = strconv.Atoi(r.FormValue("paid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
return nil, http.StatusBadRequest, err
|
||||||
fmt.Println("can't parse order paid: ", err)
|
}
|
||||||
return
|
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)
|
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 {
|
for _, e := range cakes {
|
||||||
value := r.FormValue(fmt.Sprintf("cake[%d]", e.ID))
|
e.Amount, err = strconv.Atoi(r.FormValue(fmt.Sprintf("cake[%d]", e.ID)))
|
||||||
if value == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
amount, err := strconv.Atoi(value)
|
|
||||||
e.Amount = amount
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
n.Cakes = append(n.Cakes, e)
|
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)
|
n.ID, err = h.s.SaveOrder(n)
|
||||||
|
return n, http.StatusAccepted, err
|
||||||
w.Header().Set("content-type", "text/html")
|
|
||||||
w.WriteHeader(http.StatusAccepted)
|
|
||||||
w.Write([]byte("accepted <a href='/'>back</a>"))
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ func (h *Server) loadHandler() {
|
||||||
|
|
||||||
mux.HandleFunc("GET /order/", h.render(h.getOrder, "order.html"))
|
mux.HandleFunc("GET /order/", h.render(h.getOrder, "order.html"))
|
||||||
mux.HandleFunc("GET /", h.render(h.index, "index.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
|
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}}">
|
<input type="tel" name="phone" value="{{.Phone}}">
|
||||||
<label>location</label>
|
<label>location</label>
|
||||||
<select name="location">
|
<select name="location">
|
||||||
<option value="Kartuzy">Kartuzy</option>
|
<option {{ if eq .Location "Kartuzy" }}selected{{ end }} value="Kartuzy">Kartuzy</option>
|
||||||
<option value="Somonino">Somonino</option>
|
<option {{ if eq .Location "Somonino" }}selected{{ end }} value="Somonino">Somonino</option>
|
||||||
</select>
|
</select>
|
||||||
<label>date</label>
|
<label>date</label>
|
||||||
<input type="date" name="date" value="{{.Date.Format "2006-01-02"}}">
|
<input type="date" name="date" value="{{.Date.Format "2006-01-02"}}">
|
||||||
<label>status</label>
|
<label>status</label>
|
||||||
<select name="status" id="status">
|
<select name="status" id="status" >
|
||||||
<option value="accepted">accepted</option>
|
<option {{ if eq .Status "accepted" }}selected{{ end }} value="accepted">accepted</option>
|
||||||
<option value="done">done</option>
|
<option {{ if eq .Status "done" }}selected{{ end }} value="done">done</option>
|
||||||
</select>
|
</select>
|
||||||
<label>paid</label>
|
<label>paid</label>
|
||||||
<input type="number" name="paid" min="0" value="{{.Paid}}">
|
<input type="number" name="paid" min="0" value="{{.Paid}}">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue