refactor and order template
This commit is contained in:
parent
922478bdeb
commit
2465956fc2
5 changed files with 126 additions and 99 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/config"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/server/dto"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/server/templates"
|
||||
)
|
||||
|
||||
|
|
@ -158,41 +159,53 @@ func (h *Server) blocked(r *http.Request) (templ.Component, int, error) {
|
|||
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", 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), templates.OrderForm(order, catalogue, tags, blockedDates)), http.StatusOK, nil
|
||||
}
|
||||
|
||||
return templates.OrderPage(order, catalogue, tags, blockedDates), http.StatusOK, nil
|
||||
func internalError(err error) *httpError {
|
||||
return &httpError{http.StatusInternalServerError, err}
|
||||
}
|
||||
func badRequest(err error) *httpError {
|
||||
return &httpError{http.StatusBadRequest, err}
|
||||
}
|
||||
func notFound(err error) *httpError {
|
||||
return &httpError{http.StatusNotFound, err}
|
||||
}
|
||||
|
||||
func (h *Server) newOrder(r *http.Request) (dto.OrderPage, *httpError) {
|
||||
var dto dto.OrderPage
|
||||
var err error
|
||||
|
||||
dto.Catalogue, err = h.s.GetCakes()
|
||||
if err != nil {
|
||||
return dto, internalError(err)
|
||||
}
|
||||
|
||||
dto.Tags, err = h.s.GetTags()
|
||||
if err != nil {
|
||||
return dto, internalError(err)
|
||||
}
|
||||
|
||||
dto.BlockedDates, err = h.s.GetBlockedDates()
|
||||
if err != nil {
|
||||
return dto, internalError(err)
|
||||
}
|
||||
|
||||
return dto, nil
|
||||
}
|
||||
|
||||
func (h *Server) order(r *http.Request) (dto.OrderPage, *httpError) {
|
||||
dto, httpErr := h.newOrder(r)
|
||||
if httpErr != nil {
|
||||
return dto, httpErr
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
return dto, badRequest(err)
|
||||
}
|
||||
|
||||
dto.Order, err = h.s.GetOrder(id)
|
||||
if err != nil {
|
||||
return dto, notFound(err)
|
||||
}
|
||||
|
||||
return dto, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue