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) fmt.Println("can't parse order id: ", err)
return return
} }
n.Name = r.FormValue("name")
n.Surname = r.FormValue("surname") n.Name = strings.TrimSpace(r.FormValue("name"))
n.Phone = r.FormValue("phone")
n.Location = r.FormValue("location") 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")) n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order date: ", err) fmt.Println("can't parse order date: ", err)
return return
} }
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) w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order paid: ", err) fmt.Println("can't parse order paid: ", err)
return 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) fmt.Println("parsed order: ", n)
h.s.saveOrder(n)
w.Header().Set("content-type", "text/html") w.Header().Set("content-type", "text/html")
w.WriteHeader(http.StatusAccepted) w.WriteHeader(http.StatusAccepted)
w.Write([]byte("accepted <a href='/'>back</a>")) w.Write([]byte("accepted <a href='/'>back</a>"))

View file

@ -5,11 +5,35 @@
<a href="order">New</a> <a href="order">New</a>
</nav> </nav>
<main> <main>
<ul> <table>
<tr>
<th></th>
<th>Name</th>
<th>Surname</th>
<th>Phone</th>
<th>Location</th>
<th>Date</th>
<th>Accepted</th>
<th>Paid</th>
<th>Cakes</th>
</tr>
{{range .}} {{range .}}
<li id="order[{{.ID}}]"> <tr>
<a href="/order/{{.ID}}">{{.Name}} {{.Surname}}</a> <th><a href="/order/{{.ID}}">edit</a></th>
</li> <th>{{.Name}}</th>
<th>{{.Surname}}</th>
<th>{{.Phone}}</th>
<th>{{.Location}}</th>
<th>{{.Date.Format "2006-01-02"}}</th>
<th>{{.Accepted.Format "2006-01-02"}}</th>
<th>{{.Paid}}</th>
<th>
{{range .Cakes}}
{{.Name}}
{{.Amount}}
{{end}}
</th>
</tr>
{{end}} {{end}}
</ul> </table>
</main> </main>

13
main.go
View file

@ -18,12 +18,15 @@ func main() {
h.tmpl = templates h.tmpl = templates
h.s = NewStore() h.s = NewStore()
h.s.saveCake(cake{"Sernik", -1, 120, 0}) h.s.saveCake(cake{"Sernik ulubiony", -1, 65, 0})
h.s.saveCake(cake{"Malinowa chmurka", -1, 120, 0}) h.s.saveCake(cake{"Malinowa chmurka", -1, 150, 0})
h.s.saveCake(cake{"Mako sernik", -1, 150, 0})
h.s.saveCake(cake{"Rolada makowa", -1, 80, 0})
h.s.saveCake(cake{"Wieniec bezowy", -1, 120, 0})
h.s.saveOrder(order{-1, "Albert", "Camus", "123456789", "Kartuzy", time.Now().AddDate(0, 0, 7), 0, nil}) h.s.saveOrder(order{-1, "Albert", "Camus", "123456789", "Kartuzy", time.Now().AddDate(0, 0, 7), time.Now(), 0, nil})
h.s.saveOrder(order{-1, "George", "Orwell", "", "Kartuzy", time.Now().AddDate(0, 1, 0), 0, nil}) h.s.saveOrder(order{-1, "George", "Orwell", "", "Kartuzy", time.Now().AddDate(0, 1, 0), time.Now(), 0, nil})
h.s.saveOrder(order{-1, "Karl", "Marx", "0700", "Somonino", time.Now(), 0, nil}) h.s.saveOrder(order{-1, "Karl", "Marx", "0700", "Somonino", time.Now(), time.Now(), 0, nil})
http.HandleFunc("GET /order/", logger(h.form)) http.HandleFunc("GET /order/", logger(h.form))
http.HandleFunc("GET /", logger(h.index)) http.HandleFunc("GET /", logger(h.index))

View file

@ -9,9 +9,9 @@ import (
func logger(in http.HandlerFunc) http.HandlerFunc { func logger(in http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
log := fmt.Sprint(r.Method, " ", r.URL.String()) //log := fmt.Sprint(r.Method, " ", r.URL.String())
fmt.Println(r.Method, r.URL.String())
in(w, r) in(w, r)
log += fmt.Sprint(" ", time.Since(start)) fmt.Println(r.Method, "finished in", time.Since(start))
fmt.Println(log)
} }
} }

View file

@ -16,6 +16,7 @@ type order struct {
Phone string Phone string
Location string Location string
Date time.Time Date time.Time
Accepted time.Time
Paid int // increments of 0.01 Paid int // increments of 0.01
Cakes []cake Cakes []cake
} }