Add order total calculation and update template display
This commit is contained in:
parent
5af6e871c2
commit
fe703a9b1c
5 changed files with 22 additions and 4 deletions
|
|
@ -23,3 +23,11 @@ type Order struct {
|
||||||
Paid int // increments of 0.01
|
Paid int // increments of 0.01
|
||||||
Cakes []Cake
|
Cakes []Cake
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *Order) Total() int {
|
||||||
|
out := o.Paid * (-1)
|
||||||
|
for _, e := range o.Cakes {
|
||||||
|
out += e.Price * e.Amount
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/Bronku/iroon/internal/models"
|
"github.com/Bronku/iroon/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Server) index(r *http.Request) (any, int, error) {
|
func (h *Server) orders(r *http.Request) (any, int, error) {
|
||||||
data, err := h.s.GetOrders()
|
data, err := h.s.GetOrders()
|
||||||
return data, http.StatusOK, err
|
return data, http.StatusOK, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
internal/server/http.go
Normal file
9
internal/server/http.go
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func (s *Server) redirect(path string, code int) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
http.Redirect(w, r, path, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,7 +20,8 @@ func (h *Server) loadHandler() {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.HandleFunc("GET /order/", h.render(h.order, "order.html"))
|
mux.HandleFunc("GET /order/", h.render(h.order, "order.html"))
|
||||||
mux.HandleFunc("GET /", h.render(h.index, "index.html"))
|
mux.HandleFunc("GET /", h.redirect("/orders", http.StatusSeeOther))
|
||||||
|
mux.HandleFunc("GET /orders", h.render(h.orders, "index.html"))
|
||||||
mux.HandleFunc("POST /order/", h.render(h.postOrder, "confirmation.html"))
|
mux.HandleFunc("POST /order/", h.render(h.postOrder, "confirmation.html"))
|
||||||
mux.HandleFunc("GET /cakes", h.render(h.cakes, "cakes.html"))
|
mux.HandleFunc("GET /cakes", h.render(h.cakes, "cakes.html"))
|
||||||
mux.HandleFunc("GET /cake/", h.render(h.cake, "cake.html"))
|
mux.HandleFunc("GET /cake/", h.render(h.cake, "cake.html"))
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
<th>Accepted</th>
|
<th>Accepted</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
<th>Paid</th>
|
<th>Total</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
<th>{{.Date.Format "2006-01-02"}}</th>
|
<th>{{.Date.Format "2006-01-02"}}</th>
|
||||||
<th>{{.Accepted.Format "2006-01-02"}}</th>
|
<th>{{.Accepted.Format "2006-01-02"}}</th>
|
||||||
<th>{{.Status}}</th>
|
<th>{{.Status}}</th>
|
||||||
<th>{{.Paid}}</th>
|
<th>{{.Total}}</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue