diff --git a/server/fetcher.go b/server/fetcher.go index 5d3e1dc..e30bd1a 100644 --- a/server/fetcher.go +++ b/server/fetcher.go @@ -2,4 +2,4 @@ package server import "net/http" -type fetcher func(r *http.Request) (any, error) +type fetcher func(r *http.Request) (any, int, error) diff --git a/server/get.go b/server/get.go index 347e039..65a5e09 100644 --- a/server/get.go +++ b/server/get.go @@ -8,11 +8,12 @@ import ( "github.com/Bronku/iroon/store" ) -func (h *Server) index(r *http.Request) (any, error) { - return h.s.GetOrders() +func (h *Server) index(r *http.Request) (any, int, error) { + data, err := h.s.GetOrders() + return data, http.StatusOK, err } -func (h *Server) order(r *http.Request) (any, error) { +func (h *Server) getOrder(r *http.Request) (any, int, error) { type formData struct { Order store.Order Catalogue []store.Cake @@ -22,23 +23,23 @@ func (h *Server) order(r *http.Request) (any, error) { data.Catalogue, err = h.s.GetCakes() if err != nil { - return nil, err + return nil, http.StatusInternalServerError, err } url := strings.Split(r.URL.String(), "/") if len(url) < 3 || url[2] == "" { - return data, nil + return data, http.StatusOK, nil } id, err := strconv.Atoi(url[2]) if err != nil { - return nil, err + return nil, http.StatusBadRequest, err } data.Order, err = h.s.GetOrder(id) if err != nil { - return nil, err + return nil, http.StatusNotFound, err } - return data, nil + return data, http.StatusOK, nil } diff --git a/server/server.go b/server/server.go index b0cd271..dc1a5ed 100644 --- a/server/server.go +++ b/server/server.go @@ -31,7 +31,7 @@ func (h *Server) openStore() error { func (h *Server) loadHandler() { mux := http.NewServeMux() - mux.HandleFunc("GET /order/", h.render(h.order, "order.html")) + 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) diff --git a/server/template.go b/server/template.go index df7b692..b483c08 100644 --- a/server/template.go +++ b/server/template.go @@ -18,9 +18,9 @@ func (h *Server) loadTemplates() error { func (s *Server) render(fetch fetcher, templateName string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/html") - data, err := fetch(r) + data, code, err := fetch(r) if err != nil { - errorPage(err, http.StatusInternalServerError).ServeHTTP(w, r) + errorPage(err, code).ServeHTTP(w, r) return } err = s.tmpl.ExecuteTemplate(w, templateName, data)