aboutsummaryrefslogtreecommitdiff
blob: 79ea8f293ea3f10e06835716e870a1c198329c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package list

import (
	"archives/pkg/cache"
	"archives/pkg/database"
	"archives/pkg/models"
	"github.com/go-pg/pg/v10/orm"
	"net/http"
	"strings"
)

func Show(w http.ResponseWriter, r *http.Request) {
	listName := strings.ReplaceAll(r.URL.Path, "/", "")
	templateData := cache.Get("/" + listName + "/")
	if templateData == nil {
		http.NotFound(w,r)
		return
	}
	renderShowTemplate(w, listName, templateData)
}


func ComputeShowTemplateData(listName string) interface{} {
	var res []struct {
		CombinedDate string
		MessageCount int
	}
	err := database.DBCon.Model((*models.Message)(nil)).
		WhereGroup(func(q *orm.Query) (*orm.Query, error) {
			q = q.WhereOr(`subject LIKE '[` + listName + `]%'`).
				WhereOr(`subject LIKE 'Re: [` + listName + `]%'`)
			return q, nil
		}).
		ColumnExpr("to_char(date, 'YYYY-MM') AS combined_date").
		ColumnExpr("count(*) AS message_count").
		Group("combined_date").
		Order("combined_date DESC").
		Select(&res)

	if err != nil {
		return nil
	}
	return res
}