apps/easel/handlers.go 3.0 K raw
1
package main
2
3
import (
4
	"html/template"
5
	"net/http"
6
)
7
8
func iiifURL(imageID string) string {
9
	return "https://www.artic.edu/iiif/2/" + imageID + "/full/843,/0/default.jpg"
10
}
11
12
func sourceURL(id int64) string {
13
	return "https://www.artic.edu/artworks/" + itoa64(id)
14
}
15
16
func itoa64(v int64) string {
17
	if v == 0 {
18
		return "0"
19
	}
20
	negative := v < 0
21
	if negative {
22
		v = -v
23
	}
24
	buf := [20]byte{}
25
	i := len(buf)
26
	for v > 0 {
27
		i--
28
		buf[i] = byte('0' + v%10)
29
		v /= 10
30
	}
31
	if negative {
32
		i--
33
		buf[i] = '-'
34
	}
35
	return string(buf[i:])
36
}
37
38
func toArtworkView(a DailyArtwork) artworkView {
39
	v := artworkView{
40
		Date:             a.Date,
41
		Title:            a.Title,
42
		ArtistDisplay:    a.ArtistDisplay.String,
43
		DateDisplay:      a.DateDisplay.String,
44
		MediumDisplay:    a.MediumDisplay.String,
45
		Dimensions:       a.Dimensions.String,
46
		PlaceOfOrigin:    a.PlaceOfOrigin.String,
47
		CreditLine:       a.CreditLine.String,
48
		Description:      a.Description.String,
49
		ShortDescription: a.ShortDescription.String,
50
		ImageURL:         iiifURL(a.ImageID),
51
		SourceURL:        sourceURL(a.ArtworkID),
52
	}
53
	v.DescriptionHTML = template.HTML(v.Description)
54
	return v
55
}
56
57
func (a *App) indexHandler(w http.ResponseWriter, r *http.Request) {
58
	today := a.todayInTZ()
59
	d, err := getDaily(a.DB, today)
60
	if err != nil {
61
		a.Log.Error("index db error", "err", err)
62
		a.renderPageStatus(w, http.StatusInternalServerError, "error.html", errorPageData{Title: "Error", Message: "Could not load today's artwork."})
63
		return
64
	}
65
	data := indexPageData{TodayDate: today}
66
	if d != nil {
67
		v := toArtworkView(*d)
68
		data.Artwork = &v
69
	}
70
	a.renderPage(w, "index.html", data)
71
}
72
73
func (a *App) dayHandler(w http.ResponseWriter, r *http.Request) {
74
	date := r.PathValue("date")
75
	if _, ok := parseDate(date); !ok {
76
		a.renderPageStatus(w, http.StatusBadRequest, "error.html", errorPageData{Title: "Invalid date", Message: "'" + date + "' is not a valid YYYY-MM-DD date."})
77
		return
78
	}
79
	today := a.todayInTZ()
80
	if date > today {
81
		a.renderPageStatus(w, http.StatusNotFound, "error.html", errorPageData{Title: "Not yet", Message: date + " is in the future."})
82
		return
83
	}
84
	d, err := getDaily(a.DB, date)
85
	if err != nil {
86
		a.Log.Error("day db error", "err", err)
87
		a.renderPageStatus(w, http.StatusInternalServerError, "error.html", errorPageData{Title: "Error", Message: "Database error."})
88
		return
89
	}
90
	if d == nil {
91
		a.renderPageStatus(w, http.StatusNotFound, "error.html", errorPageData{Title: "Not found", Message: "No artwork stored for " + date + "."})
92
		return
93
	}
94
	a.renderPage(w, "day.html", dayPageData{Date: date, Artwork: toArtworkView(*d)})
95
}
96
97
func (a *App) archiveHandler(w http.ResponseWriter, r *http.Request) {
98
	items, _ := listDaily(a.DB, 1000)
99
	rows := make([]archiveRow, 0, len(items))
100
	for _, it := range items {
101
		artist := it.ArtistTitle.String
102
		if artist == "" {
103
			artist = it.ArtistDisplay.String
104
		}
105
		rows = append(rows, archiveRow{Date: it.Date, Title: it.Title, Artist: artist})
106
	}
107
	a.renderPage(w, "archive.html", archivePageData{Archive: rows})
108
}