cake-order-tracker/server/post.go
2026-06-16 16:54:45 +02:00

185 lines
4.8 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(n.ID), http.StatusAccepted, err
}
func (h *Server) postOrder(r *http.Request) (int, *httpError) {
cakes, err := h.s.GetCakes()
if err != nil {
return 0, internalError(err)
}
err = r.ParseForm()
if err != nil {
return 0, badRequest(err)
}
var n models.Order
n.ID, err = strconv.Atoi(r.FormValue("id"))
if err != nil {
return 0, badRequest(err)
}
n.Paid, err = models.ParsePrice(r.FormValue("paid"))
if err != nil {
return 0, badRequest(err)
}
n.Date, err = time.Parse(config.DateFormat, r.FormValue("date"))
if err != nil {
return 0, badRequest(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)
if err != nil {
return 0, internalError(err)
}
return n.ID, nil
}
func safeStr(s []string, i int) string {
if i < len(s) {
return s[i]
}
return ""
}
func (h *Server) postBlocked(r *http.Request) (templ.Component, int, error) {
if err := r.ParseForm(); err != nil {
return nil, http.StatusBadRequest, err
}
if err := h.s.AddBlockedDate(r.FormValue("date")); err != nil {
return nil, http.StatusInternalServerError, err
}
dates, _ := h.s.GetBlockedDates()
return templates.BlockedMain(dates), http.StatusOK, nil
}
func (h *Server) deleteBlocked(r *http.Request) (templ.Component, int, error) {
id, err := strconv.Atoi(r.PathValue("id"))
if err != nil {
return nil, http.StatusBadRequest, err
}
if err := h.s.RemoveBlockedDate(id); err != nil {
return nil, http.StatusInternalServerError, err
}
dates, _ := h.s.GetBlockedDates()
return templates.BlockedMain(dates), http.StatusOK, nil
}
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
}