194 lines
5.6 KiB
Go
194 lines
5.6 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 weekInterval(t time.Time) (time.Time, time.Time) {
|
|
monday := t.AddDate(0, 0, -((int(t.Weekday()) + 6) % 7))
|
|
sunday := monday.AddDate(0, 0, 6)
|
|
return monday, sunday
|
|
}
|
|
|
|
func parseDateRange(r *http.Request) (first, last time.Time) {
|
|
now := time.Now()
|
|
weekFirst, weekLast := weekInterval(now)
|
|
|
|
first, last = weekFirst, weekLast
|
|
if from := r.URL.Query().Get("from"); from != "" {
|
|
if t, err := time.Parse(config.DateFormat, from); err == nil {
|
|
first = t
|
|
}
|
|
}
|
|
if to := r.URL.Query().Get("to"); to != "" {
|
|
if t, err := time.Parse(config.DateFormat, to); err == nil {
|
|
last = t
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func filterOrdersWithSpecialCakes(orders []models.Order) []models.Order {
|
|
var out []models.Order
|
|
for _, o := range orders {
|
|
if len(o.SpecialCakes) > 0 {
|
|
out = append(out, o)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (h *Server) orders(r *http.Request) (templ.Component, int, error) {
|
|
first, last := parseDateRange(r)
|
|
now := time.Now()
|
|
today := now.Format(config.DateFormat)
|
|
tomorrow := now.AddDate(0, 0, 1).Format(config.DateFormat)
|
|
specialFilter := r.URL.Query().Get("special")
|
|
|
|
orders, err := h.s.GetOrders(first, last)
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
if specialFilter == "true" {
|
|
orders = filterOrdersWithSpecialCakes(orders)
|
|
}
|
|
cakeCounts, err := h.s.GetCakeCounts(first, last)
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
firstStr := first.Format(config.DateFormat)
|
|
lastStr := last.Format(config.DateFormat)
|
|
|
|
if r.Header.Get("HX-Target") == "main" {
|
|
counts, err := h.s.GetOrderCounts()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
return templates.MainContent("Panel główny", templates.DateForm(firstStr, lastStr, today, tomorrow, specialFilter), templates.OrdersMain(counts, cakeCounts, orders, firstStr, lastStr, specialFilter)), http.StatusOK, nil
|
|
}
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.OrdersContent(orders, cakeCounts, firstStr, lastStr, specialFilter), http.StatusOK, nil
|
|
}
|
|
|
|
counts, err := h.s.GetOrderCounts()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
return templates.OrdersPage(firstStr, lastStr, today, tomorrow, specialFilter, counts, cakeCounts, orders), http.StatusOK, nil
|
|
}
|
|
|
|
func (h *Server) cakes(r *http.Request) (templ.Component, int, error) {
|
|
cakes, err := h.s.GetCakes()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
tags, err := h.s.GetTags()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent("Katalog", templ.NopComponent, templates.CakesMain(cakes, tags)), http.StatusOK, nil
|
|
}
|
|
|
|
return templates.CakesPage(cakes, tags), http.StatusOK, nil
|
|
}
|
|
|
|
func (h *Server) cake(r *http.Request) (templ.Component, int, error) {
|
|
url := strings.Split(r.URL.Path, "/")
|
|
if len(url) < 3 || url[2] == "" {
|
|
tags, err := h.s.GetTags()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent("Ciasto nowe", templ.NopComponent, templates.CakeForm(models.Cake{}, tags)), http.StatusOK, nil
|
|
}
|
|
return templates.CakePage(models.Cake{}, tags), 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
|
|
}
|
|
|
|
tags, err := h.s.GetTags()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent(templates.CakeTitle(data), templ.NopComponent, templates.CakeForm(data, tags)), http.StatusOK, nil
|
|
}
|
|
|
|
return templates.CakePage(data, tags), http.StatusOK, nil
|
|
}
|
|
|
|
func (h *Server) blocked(r *http.Request) (templ.Component, int, error) {
|
|
dates, err := h.s.GetBlockedDates()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent("Terminy zablokowane", templ.NopComponent, templates.BlockedMain(dates)), http.StatusOK, nil
|
|
}
|
|
|
|
return templates.BlockedPage(dates), http.StatusOK, nil
|
|
}
|
|
|
|
func (h *Server) order(r *http.Request) (templ.Component, int, error) {
|
|
catalogue, err := h.s.GetCakes()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
tags, err := h.s.GetTags()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
blockedDates, err := h.s.GetBlockedDates()
|
|
if err != nil {
|
|
return nil, http.StatusInternalServerError, err
|
|
}
|
|
|
|
url := strings.Split(r.URL.Path, "/")
|
|
if len(url) < 3 || url[2] == "" {
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent("Nowe zamówienie", templ.NopComponent, templates.OrderForm(models.Order{Date: time.Now()}, catalogue, tags, blockedDates)), http.StatusOK, nil
|
|
}
|
|
return templates.OrderPage(models.Order{Date: time.Now()}, catalogue, tags, blockedDates), http.StatusOK, nil
|
|
}
|
|
|
|
id, err := strconv.Atoi(url[2])
|
|
if err != nil {
|
|
return nil, http.StatusBadRequest, err
|
|
}
|
|
|
|
order, err := h.s.GetOrder(id)
|
|
if err != nil {
|
|
return nil, http.StatusNotFound, err
|
|
}
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
return templates.MainContent(templates.OrderTitle(order), templ.NopComponent, templates.OrderForm(order, catalogue, tags, blockedDates)), http.StatusOK, nil
|
|
}
|
|
|
|
return templates.OrderPage(order, catalogue, tags, blockedDates), http.StatusOK, nil
|
|
}
|