This commit is contained in:
tumillanino
2025-10-28 12:36:37 +11:00
parent 186d6768fb
commit dad3ec655f
31 changed files with 2256 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package handlers
import (
"html/template"
"net/http"
"os"
"path/filepath"
"sort"
"decor-by-hannahs/internal/db"
)
type Photo struct {
URL string
Caption string
}
func GalleryHandler(q *db.Queries, tmpl *template.Template) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var photos []Photo
galleryDir := "assets/gallery-photos"
entries, err := os.ReadDir(galleryDir)
if err == nil {
for _, entry := range entries {
if !entry.IsDir() {
ext := filepath.Ext(entry.Name())
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" {
photos = append(photos, Photo{
URL: "/assets/gallery-photos/" + entry.Name(),
Caption: "",
})
}
}
}
}
sort.Slice(photos, func(i, j int) bool {
return photos[i].URL < photos[j].URL
})
type data struct {
Photos []Photo
ActivePage string
}
if err := tmpl.ExecuteTemplate(w, "gallery.tmpl", data{Photos: photos, ActivePage: "gallery"}); err != nil {
http.Error(w, "Failed to render page", http.StatusInternalServerError)
}
}
}