Add single cake endpoint handler

This commit is contained in:
bronku 2025-03-26 14:33:07 +01:00
parent f552e87a9d
commit 01732b97fc
4 changed files with 56 additions and 0 deletions

View file

@ -18,6 +18,25 @@ func (h *Server) cakes(r *http.Request) (any, int, error) {
return data, http.StatusOK, err return data, http.StatusOK, err
} }
func (h *Server) cake(r *http.Request) (any, int, error) {
url := strings.Split(r.URL.String(), "/")
if len(url) < 3 || url[2] == "" {
return store.Cake{}, http.StatusOK, nil
}
id, err := strconv.Atoi(url[2])
if err != nil {
return nil, http.StatusBadRequest, err
}
data, err := h.s.GetCake(id)
if err != nil {
return nil, http.StatusNotFound, err
}
return data, http.StatusOK, nil
}
func (h *Server) order(r *http.Request) (any, int, error) { func (h *Server) order(r *http.Request) (any, int, error) {
type formData struct { type formData struct {
Order store.Order Order store.Order

View file

@ -26,6 +26,7 @@ func (h *Server) loadHandler() {
mux.HandleFunc("GET /", h.render(h.index, "index.html")) mux.HandleFunc("GET /", h.render(h.index, "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"))
h.Handler = mux h.Handler = mux
} }

View file

@ -0,0 +1,31 @@
<header>
<h1>New Order</h1>
</header>
<nav>
<a href="/">back</a>
</nav>
<main>
<form method="post">
<div>
<h2>Info</h2>
<input hidden="true" name="id" value="{{.ID}}">
<label>name</label>
<input type="text" name="name" value="{{.Name}}">
<label>price</label>
<input type="number" name="price" min="0" value="{{.Price}}">
<label>category</label>
<select name="category" id="category" >
<option {{ if eq .Category "common" }}selected{{ end }} value="common">common</option>
<option {{ if eq .Category "christmas" }}selected{{ end }} value="christmas">christmas</option>
<option {{ if eq .Category "easter" }}selected{{ end }} value="easter">easter</option>
<option {{ if eq .Category "donuts" }}selected{{ end }} value="donuts">donuts</option>
</select>
<label>availability</label>
<select name="availibility" id="availibility" >
<option {{ if eq .Availability "available" }}selected{{ end }} value="available">available</option>
<option {{ if eq .Availability "unavailable" }}selected{{ end }} value="unavailable">unavailable</option>
</select>
</div>
<button type="submit">submit</button>
</form>
</main>

View file

@ -0,0 +1,5 @@
update cake
set
availability = "available";
pragma user_version = 5;