83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package home
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
|
"dev.tandem.ws/tandem/camper/pkg/carousel"
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
|
)
|
|
|
|
const coverName = "cover"
|
|
const carouselName = "home"
|
|
|
|
type AdminHandler struct {
|
|
cover *carousel.AdminHandler
|
|
carousel *carousel.AdminHandler
|
|
}
|
|
|
|
func NewAdminHandler() *AdminHandler {
|
|
handler := &AdminHandler{
|
|
cover: carousel.NewAdminHandler(coverName, serveHomeIndex),
|
|
carousel: carousel.NewAdminHandler(carouselName, serveHomeIndex),
|
|
}
|
|
handler.cover.IndexURL = "/admin/home"
|
|
handler.cover.SlidesURL = "/admin/home/cover"
|
|
return handler
|
|
}
|
|
|
|
func (h *AdminHandler) 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:
|
|
serveHomeIndex(w, r, user, company, conn)
|
|
default:
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet)
|
|
}
|
|
case "cover":
|
|
h.cover.Handler(user, company, conn).ServeHTTP(w, r)
|
|
case "slides":
|
|
h.carousel.Handler(user, company, conn).ServeHTTP(w, r)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
}
|
|
|
|
func serveHomeIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
|
cover, err := carousel.CollectSlideEntries(r.Context(), company, conn, coverName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
slides, err := carousel.CollectSlideEntries(r.Context(), company, conn, carouselName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
page := &homeIndex{
|
|
Cover: cover,
|
|
Slides: slides,
|
|
}
|
|
page.MustRender(w, r, user, company)
|
|
}
|
|
|
|
type homeIndex struct {
|
|
Cover []*carousel.SlideEntry
|
|
Slides []*carousel.SlideEntry
|
|
}
|
|
|
|
func (page *homeIndex) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
|
template.MustRenderAdmin(w, r, user, company, "home/index.gohtml", page)
|
|
}
|