date presets
This commit is contained in:
parent
8d81940faf
commit
e8ea70d61e
5 changed files with 101 additions and 71 deletions
|
|
@ -13,34 +13,33 @@ import (
|
|||
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
||||
)
|
||||
|
||||
func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
|
||||
firstDay = time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
|
||||
lastDay = time.Date(y, m+1, 1, 0, 0, 0, -1, time.UTC)
|
||||
return firstDay, lastDay
|
||||
func weekInterval(t time.Time) (firstDay, lastDay time.Time) {
|
||||
monday := t.AddDate(0, 0, -((int(t.Weekday()) + 6) % 7))
|
||||
sunday := monday.AddDate(0, 0, 6)
|
||||
return monday, sunday
|
||||
}
|
||||
|
||||
func (h *Server) orders(_ *http.Request) (templ.Component, int, error) {
|
||||
var (
|
||||
y int
|
||||
m time.Month
|
||||
)
|
||||
y, m, _ = time.Now().Date()
|
||||
first, last := monthInterval(y, m)
|
||||
func (h *Server) orders(r *http.Request) (templ.Component, int, error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
today := now.Format(config.DateFormat)
|
||||
tomorrow := now.AddDate(0, 0, 1).Format(config.DateFormat)
|
||||
|
||||
orders, err := h.s.GetOrders(first, last)
|
||||
return templates.OrdersPage(first.Format(config.DateFormat), last.Format(config.DateFormat), orders), http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) ordersSearch(r *http.Request) (templ.Component, int, error) {
|
||||
from, err := time.Parse(config.DateFormat, r.URL.Query().Get("from"))
|
||||
if err != nil {
|
||||
from = time.Time{}
|
||||
}
|
||||
to, err := time.Parse(config.DateFormat, r.URL.Query().Get("to"))
|
||||
if err != nil {
|
||||
to = time.Time{}
|
||||
}
|
||||
data, err := h.s.GetOrders(from, to)
|
||||
return templates.OrdersTable(data), http.StatusOK, err
|
||||
return templates.OrdersPage(first.Format(config.DateFormat), last.Format(config.DateFormat), today, tomorrow, orders), http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) cakes(_ *http.Request) (templ.Component, int, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue