/* * SPDX-FileCopyrightText: 2023 jordi fita mas * SPDX-License-Identifier: AGPL-3.0-only */ package company import ( "dev.tandem.ws/tandem/camper/pkg/auth" "dev.tandem.ws/tandem/camper/pkg/campsite" "dev.tandem.ws/tandem/camper/pkg/database" httplib "dev.tandem.ws/tandem/camper/pkg/http" "dev.tandem.ws/tandem/camper/pkg/template" "dev.tandem.ws/tandem/camper/pkg/uuid" "net/http" ) type Handler struct { campsite *campsite.Handler } func NewHandler() *Handler { return &Handler{ campsite: campsite.NewHandler(), } } func (h *Handler) Handler(user *auth.User, conn *database.Conn) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var slug string slug, r.URL.Path = httplib.ShiftPath(r.URL.Path) if !uuid.Valid(slug) { http.NotFound(w, r) return } company, err := auth.QueryBySlug(r.Context(), conn, slug) if database.ErrorIsNotFound(err) { http.NotFound(w, r) return } else if err != nil { panic(err) } var head string head, r.URL.Path = httplib.ShiftPath(r.URL.Path) switch head { case "campsites": h.campsite.Handler(user, company, conn).ServeHTTP(w, r) case "": switch r.Method { case http.MethodGet: serveDashboard(w, r, user, company) default: httplib.MethodNotAllowed(w, r, http.MethodGet) } default: http.NotFound(w, r) } }) } func serveDashboard(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) { template.MustRender(w, r, user, company, "dashboard.gohtml", nil) }