move to templ
This commit is contained in:
parent
31a33cc220
commit
bb32802324
34 changed files with 1011 additions and 528 deletions
|
|
@ -1,13 +1,15 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
||||
)
|
||||
|
||||
func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
|
||||
|
|
@ -16,24 +18,18 @@ func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
|
|||
return firstDay, lastDay
|
||||
}
|
||||
|
||||
func (h *Server) orders(_ *http.Request) (any, int, error) {
|
||||
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)
|
||||
fmt.Println(first, last)
|
||||
orders, err := h.s.GetOrders(first, last)
|
||||
data := struct {
|
||||
First string
|
||||
Last string
|
||||
Orders []models.Order
|
||||
}{first.Format("2006-01-02"), last.Format("2006-01-02"), orders}
|
||||
return data, http.StatusOK, err
|
||||
return templates.OrdersPage(first.Format("2006-01-02"), last.Format("2006-01-02"), orders), http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) ordersSearch(r *http.Request) (any, int, error) {
|
||||
func (h *Server) ordersSearch(r *http.Request) (templ.Component, int, error) {
|
||||
from, err := time.Parse("2006-01-02", r.URL.Query().Get("from"))
|
||||
if err != nil {
|
||||
from = time.Time{}
|
||||
|
|
@ -42,20 +38,19 @@ func (h *Server) ordersSearch(r *http.Request) (any, int, error) {
|
|||
if err != nil {
|
||||
to = time.Time{}
|
||||
}
|
||||
fmt.Println(from, to)
|
||||
data, err := h.s.GetOrders(from, to)
|
||||
return data, http.StatusOK, err
|
||||
return templates.OrdersTable(data), http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) cakes(_ *http.Request) (any, int, error) {
|
||||
func (h *Server) cakes(_ *http.Request) (templ.Component, int, error) {
|
||||
data, err := h.s.GetCakes()
|
||||
return data, http.StatusOK, err
|
||||
return templates.CakesPage(data), http.StatusOK, err
|
||||
}
|
||||
|
||||
func (h *Server) cake(r *http.Request) (any, int, error) {
|
||||
func (h *Server) cake(r *http.Request) (templ.Component, int, error) {
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
if len(url) < 3 || url[2] == "" {
|
||||
return models.Cake{}, http.StatusOK, nil
|
||||
return templates.CakePage(models.Cake{}), http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
|
|
@ -68,26 +63,19 @@ func (h *Server) cake(r *http.Request) (any, int, error) {
|
|||
return nil, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
return data, http.StatusOK, nil
|
||||
return templates.CakePage(data), http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (h *Server) order(r *http.Request) (any, int, error) {
|
||||
type formData struct {
|
||||
Order models.Order
|
||||
Catalogue []models.Cake
|
||||
}
|
||||
func (h *Server) order(r *http.Request) (templ.Component, int, error) {
|
||||
var err error
|
||||
var data formData
|
||||
|
||||
data.Catalogue, err = h.s.GetCakes()
|
||||
catalogue, err := h.s.GetCakes()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
if len(url) < 3 || url[2] == "" {
|
||||
data.Order.Date = time.Now()
|
||||
return data, http.StatusOK, nil
|
||||
return templates.OrderPage(models.Order{Date: time.Now()}, catalogue), http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
|
|
@ -95,10 +83,10 @@ func (h *Server) order(r *http.Request) (any, int, error) {
|
|||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
data.Order, err = h.s.GetOrder(id)
|
||||
order, err := h.s.GetOrder(id)
|
||||
if err != nil {
|
||||
return nil, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
return data, http.StatusOK, nil
|
||||
return templates.OrderPage(order, catalogue), http.StatusOK, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue