Special cakes UI: overlay drawer, basket integration, catalogue tag filter, form parsing, JS save/edit/remove. HTMX navigation: MainContent component, nav links with hx-get, handlers return partial on HX-Request. Dashboard: metrics grid, CakeBreakdown, OrderRow with status select, CakeCounts/OrderCounts, torty filter, tort badge, results-pane. Collapsible sidebar: toggle button, localStorage, CSS transitions. Location field: dropdown replaced with pill toggle. Inline style cleanup: moved to CSS classes (tag-grid, grow, icon.sm, header-actions, toggle-group, success-icon, etc.). Dead CSS removal: pagination, status-chip, badge-pill, login-page. Helpers: exported CakeTitle/OrderTitle, added tagNames, tagIDsComma, orderItemsDesc, specialCakeDetail, removed unused helpers.
159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/a-h/templ"
|
|
|
|
"git.bronku.xyz/bronku/cake-order-tracker/config"
|
|
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
|
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
|
)
|
|
|
|
func (h *Server) postCake(r *http.Request) (templ.Component, int, error) {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
var n models.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 = models.ParsePrice(r.FormValue("price"))
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
|
|
for _, idStr := range r.Form["tag"] {
|
|
tagID, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
n.Tags = append(n.Tags, models.Tag{ID: tagID})
|
|
}
|
|
if newTag := strings.TrimSpace(r.FormValue("new_tag")); newTag != "" {
|
|
tagID, err := h.s.CreateTag(newTag)
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
n.Tags = append(n.Tags, models.Tag{ID: tagID, Name: newTag})
|
|
}
|
|
|
|
n.ID, err = h.s.SaveCake(n)
|
|
return templates.Confirmation("Potwierdzone", n.ID), http.StatusAccepted, err
|
|
}
|
|
|
|
func (h *Server) postOrder(r *http.Request) (templ.Component, int, error) {
|
|
cakes, err := h.s.GetCakes()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
err = r.ParseForm()
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
|
|
var n models.Order
|
|
n.ID, err = strconv.Atoi(r.FormValue("id"))
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
n.Paid, err = models.ParsePrice(r.FormValue("paid"))
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
n.Date, err = time.Parse(config.DateFormat, r.FormValue("date"))
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
|
|
n.Cakes = make([]models.Cake, 0)
|
|
for _, e := range cakes {
|
|
e.Amount, err = strconv.Atoi(r.FormValue("cake[" + strconv.Itoa(e.ID) + "]"))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
n.Cakes = append(n.Cakes, e)
|
|
}
|
|
|
|
specialNames := r.Form["special_name[]"]
|
|
specialPrices := r.Form["special_price[]"]
|
|
specialSizes := r.Form["special_size[]"]
|
|
specialShapes := r.Form["special_shape[]"]
|
|
specialFlavours := r.Form["special_flavour[]"]
|
|
specialNotes := r.Form["special_notes[]"]
|
|
specialTags := r.Form["special_tags[]"]
|
|
specialNewTags := r.Form["special_new_tag[]"]
|
|
for i := range specialNames {
|
|
price, err := models.ParsePrice(specialPrices[i])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
var tags []models.Tag
|
|
if i < len(specialTags) && specialTags[i] != "" {
|
|
for _, idStr := range strings.Split(specialTags[i], ",") {
|
|
id, err := strconv.Atoi(strings.TrimSpace(idStr))
|
|
if err == nil {
|
|
tags = append(tags, models.Tag{ID: id})
|
|
}
|
|
}
|
|
}
|
|
if newTag := strings.TrimSpace(safeStr(specialNewTags, i)); newTag != "" {
|
|
tagID, err := h.s.CreateTag(newTag)
|
|
if err == nil {
|
|
tags = append(tags, models.Tag{ID: tagID, Name: newTag})
|
|
}
|
|
}
|
|
|
|
n.SpecialCakes = append(n.SpecialCakes, models.SpecialCake{
|
|
Name: specialNames[i],
|
|
Price: price,
|
|
Size: safeStr(specialSizes, i),
|
|
Shape: safeStr(specialShapes, i),
|
|
Flavour: safeStr(specialFlavours, i),
|
|
Notes: safeStr(specialNotes, i),
|
|
Tags: tags,
|
|
})
|
|
}
|
|
|
|
n.Accepted = time.Now()
|
|
n.Name = strings.TrimSpace(r.FormValue("name"))
|
|
n.Surname = strings.TrimSpace(r.FormValue("surname"))
|
|
n.Phone = strings.TrimSpace(r.FormValue("phone"))
|
|
n.Location = strings.TrimSpace(r.FormValue("location"))
|
|
n.Status = strings.TrimSpace(r.FormValue("status"))
|
|
|
|
n.ID, err = h.s.SaveOrder(n)
|
|
return templates.Confirmation("Potwierdzone", n.ID), http.StatusAccepted, err
|
|
}
|
|
|
|
func safeStr(s []string, i int) string {
|
|
if i < len(s) {
|
|
return s[i]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (h *Server) updateStatus(r *http.Request) (templ.Component, int, error) {
|
|
id, err := strconv.Atoi(r.PathValue("id"))
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
order, err := h.s.GetOrder(id)
|
|
if err != nil {
|
|
return nil, http.StatusNotFound, err
|
|
}
|
|
order.Status = r.FormValue("status")
|
|
if _, err := h.s.SaveOrder(order); err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
return templates.OrderRow(order), http.StatusOK, nil
|
|
}
|