post order
This commit is contained in:
parent
4666618151
commit
c540ccc28e
5 changed files with 38 additions and 37 deletions
18
server/errors.go
Normal file
18
server/errors.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
type httpError struct {
|
||||
code int
|
||||
error
|
||||
}
|
||||
|
||||
func internalError(err error) *httpError {
|
||||
return &httpError{http.StatusInternalServerError, err}
|
||||
}
|
||||
func badRequest(err error) *httpError {
|
||||
return &httpError{http.StatusBadRequest, err}
|
||||
}
|
||||
func notFound(err error) *httpError {
|
||||
return &httpError{http.StatusNotFound, err}
|
||||
}
|
||||
|
|
@ -159,16 +159,6 @@ func (h *Server) blocked(r *http.Request) (templ.Component, int, error) {
|
|||
return templates.BlockedPage(dates), http.StatusOK, nil
|
||||
}
|
||||
|
||||
func internalError(err error) *httpError {
|
||||
return &httpError{http.StatusInternalServerError, err}
|
||||
}
|
||||
func badRequest(err error) *httpError {
|
||||
return &httpError{http.StatusBadRequest, err}
|
||||
}
|
||||
func notFound(err error) *httpError {
|
||||
return &httpError{http.StatusNotFound, err}
|
||||
}
|
||||
|
||||
func (h *Server) newOrder(r *http.Request) (dto.OrderPage, *httpError) {
|
||||
var dto dto.OrderPage
|
||||
var err error
|
||||
|
|
|
|||
|
|
@ -46,32 +46,32 @@ func (h *Server) postCake(r *http.Request) (templ.Component, int, error) {
|
|||
}
|
||||
|
||||
n.ID, err = h.s.SaveCake(n)
|
||||
return templates.Confirmation("Potwierdzone", n.ID), http.StatusAccepted, err
|
||||
return templates.Confirmation(n.ID), http.StatusAccepted, err
|
||||
}
|
||||
|
||||
func (h *Server) postOrder(r *http.Request) (templ.Component, int, error) {
|
||||
func (h *Server) postOrder(r *http.Request) (int, *httpError) {
|
||||
cakes, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
return 0, internalError(err)
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return 0, badRequest(err)
|
||||
}
|
||||
|
||||
var n models.Order
|
||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return 0, badRequest(err)
|
||||
}
|
||||
n.Paid, err = models.ParsePrice(r.FormValue("paid"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return 0, badRequest(err)
|
||||
}
|
||||
n.Date, err = time.Parse(config.DateFormat, r.FormValue("date"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return 0, badRequest(err)
|
||||
}
|
||||
|
||||
n.Cakes = make([]models.Cake, 0)
|
||||
|
|
@ -132,7 +132,10 @@ func (h *Server) postOrder(r *http.Request) (templ.Component, int, error) {
|
|||
n.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
|
||||
n.ID, err = h.s.SaveOrder(n)
|
||||
return templates.Confirmation("Potwierdzone", n.ID), http.StatusAccepted, err
|
||||
if err != nil {
|
||||
return 0, internalError(err)
|
||||
}
|
||||
return n.ID, nil
|
||||
}
|
||||
|
||||
func safeStr(s []string, i int) string {
|
||||
|
|
|
|||
|
|
@ -10,11 +10,6 @@ import (
|
|||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
type httpError struct {
|
||||
code int
|
||||
error
|
||||
}
|
||||
|
||||
type fetcher[T any] func(r *http.Request) (T, *httpError)
|
||||
type templateBuilder[T any] func(data T) templ.Component
|
||||
|
||||
|
|
@ -64,10 +59,11 @@ func New(store *store.Store) *Server {
|
|||
mux.HandleFunc("GET /", redirect("/orders", http.StatusSeeOther))
|
||||
mux.HandleFunc("GET /order", render("Nowe zamówienie", server.newOrder, templates.OrderForm))
|
||||
mux.HandleFunc("GET /order/{id}", render("Zamówienie", server.order, templates.OrderForm))
|
||||
mux.HandleFunc("POST /order", render("Potwierdzenie", server.postOrder, templates.Confirmation))
|
||||
mux.HandleFunc("POST /order/{id}", render("Potwierdzenie", server.postOrder, templates.Confirmation))
|
||||
// mux.HandleFunc("GET /orders", render(server.orders))
|
||||
// mux.HandleFunc("GET /cake", render(server.cake))
|
||||
// mux.HandleFunc("GET /cakes", render(server.cakes))
|
||||
// mux.HandleFunc("POST /order", render(server.postOrder))
|
||||
// mux.HandleFunc("POST /order/{id}/status", render(server.updateStatus))
|
||||
// mux.HandleFunc("POST /cake", render(server.postCake))
|
||||
// mux.HandleFunc("GET /blocked", render(server.blocked))
|
||||
|
|
|
|||
|
|
@ -2,17 +2,11 @@ package templates
|
|||
|
||||
import "strconv"
|
||||
|
||||
templ Confirmation(title string, id int) {
|
||||
@Layout(title, ConfirmationBody(id))
|
||||
}
|
||||
|
||||
templ ConfirmationBody(id int) {
|
||||
<section class="confirmation">
|
||||
<span class="material-symbols-outlined success-icon">check_circle</span>
|
||||
<h2>{ strconv.Itoa(id) } — zapisano</h2>
|
||||
<a href="/orders" class="btn btn-primary">
|
||||
<span class="material-symbols-outlined">arrow_back</span>
|
||||
Powrót do zamówień
|
||||
</a>
|
||||
</section>
|
||||
templ Confirmation(id int) {
|
||||
<span class="material-symbols-outlined success-icon">check_circle</span>
|
||||
<h2>{ strconv.Itoa(id) } — zapisano</h2>
|
||||
<a href="/orders" class="btn btn-primary">
|
||||
<span class="material-symbols-outlined">arrow_back</span>
|
||||
Powrót do zamówień
|
||||
</a>
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue