camper/pkg/booking/prebooking.go

68 lines
1.9 KiB
Go

package booking
import (
"net/http"
"dev.tandem.ws/tandem/camper/pkg/auth"
"dev.tandem.ws/tandem/camper/pkg/database"
httplib "dev.tandem.ws/tandem/camper/pkg/http"
"dev.tandem.ws/tandem/camper/pkg/template"
)
type PrebookingHandler struct {
}
func NewPrebookingHandler() *PrebookingHandler {
return &PrebookingHandler{}
}
func (h *PrebookingHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var head string
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
switch head {
case "":
switch r.Method {
case http.MethodGet:
servePrebookingIndex(w, r, user, company, conn)
default:
httplib.MethodNotAllowed(w, r, http.MethodGet)
}
default:
http.NotFound(w, r)
}
})
}
func servePrebookingIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
filters := newFilterForm(r.Context(), conn, company, user.Locale)
if err := filters.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
filters.BookingStatus.Selected = []string{"created"}
bookings, err := collectBookingEntries(r.Context(), conn, user.Locale.Language, filters)
if err != nil {
panic(err)
}
page := &prebookingIndex{
Bookings: filters.buildCursor(bookings),
Filters: filters,
}
page.MustRender(w, r, user, company)
}
type prebookingIndex struct {
Bookings []*bookingEntry
Filters *filterForm
}
func (page prebookingIndex) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
if httplib.IsHTMxRequest(r) && page.Filters.Paginated() {
template.MustRenderAdminNoLayout(w, r, user, company, "prebooking/results.gohtml", page)
} else {
template.MustRenderAdminFiles(w, r, user, company, page, "prebooking/index.gohtml", "prebooking/results.gohtml")
}
}