all repos — cameraman @ 347af7a085d07e23fe44d7a1366e71d8af37cece

ui.go (view raw)

 1package main
 2
 3import (
 4	"embed"
 5	"fmt"
 6	"html/template"
 7	"log"
 8	"time"
 9
10	"github.com/gin-gonic/gin"
11)
12
13func calcYear(currentYear, year uint) uint {
14	return currentYear - year
15}
16
17func padZero(i uint) string {
18	return fmt.Sprintf("%02d", i)
19}
20
21var (
22	//go:embed templates/index.html
23	templates     embed.FS
24	indexTemplate *template.Template
25	funcMap       = template.FuncMap{
26		"padZero":  padZero,
27		"calcYear": calcYear,
28	}
29)
30
31func ParseTemplates() {
32	var err error
33	indexTemplate, err = template.New("index.html").Funcs(funcMap).ParseFS(templates, "templates/index.html")
34	if err != nil {
35		log.Fatal("Could not parse index template")
36		return
37	}
38}
39
40func ShowIndexPage(c *gin.Context) {
41	var occurrences []Occurrence
42	db.Order("month, day, name").Find(&occurrences)
43
44	data := struct {
45		Occurrences []Occurrence
46		CurrentYear uint
47	}{
48		Occurrences: occurrences,
49		CurrentYear: uint(time.Now().Year()),
50	}
51
52	err := indexTemplate.Execute(c.Writer, data)
53	if err != nil {
54		log.Println(err.Error())
55		c.String(500, "Internal Server Error")
56	}
57}