Add POST endpoint for creating new cakes
This commit is contained in:
parent
01732b97fc
commit
2857507fae
3 changed files with 24 additions and 1 deletions
|
|
@ -10,6 +10,28 @@ import (
|
|||
"github.com/Bronku/iroon/internal/store"
|
||||
)
|
||||
|
||||
func (h *Server) postCake(r *http.Request) (any, int, error) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
var n store.Cake
|
||||
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Name = r.FormValue("name")
|
||||
n.Price, err = strconv.Atoi(r.FormValue("price"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
n.Category = r.FormValue("category")
|
||||
n.Availability = r.FormValue("availibility")
|
||||
fmt.Println(n)
|
||||
return n, http.StatusAccepted, err
|
||||
}
|
||||
|
||||
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||
cakes, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ func (h *Server) loadHandler() {
|
|||
mux.HandleFunc("POST /order/", h.render(h.postOrder, "confirmation.html"))
|
||||
mux.HandleFunc("GET /cakes", h.render(h.cakes, "cakes.html"))
|
||||
mux.HandleFunc("GET /cake/", h.render(h.cake, "cake.html"))
|
||||
mux.HandleFunc("POST /cake/", h.render(h.postCake, "confirmation.html"))
|
||||
|
||||
h.Handler = mux
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,5 +5,5 @@
|
|||
<a href="/">index</a>
|
||||
</nav>
|
||||
<main>
|
||||
<h2>Order {{.ID}} for {{.Name}} {{.Surname}} confirmed</h2>
|
||||
<h2>{{.ID}} confirmed</h2>
|
||||
</main>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue