summaryrefslogtreecommitdiffstats
path: root/pkg/html/service.go
blob: cba3a6c4313aaaa2ca5813a32d9e935ca72bacc1 (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
45
46
47
48
49
50
package html

import (
	"time"

	"git.leonardobishop.net/history/pkg/database/sqlc"
)

type Service interface {
	GenerateHtml([]sqlc.GetEntriesRow) (string, error)
}

type service struct{}

func NewService() Service {
	return &service{}
}

func (s *service) GenerateHtml(entries []sqlc.GetEntriesRow) (string, error) {
	var str string

	var currentDate time.Time

	for _, entry := range entries {
		date, err := time.Parse(time.DateTime, entry.Timestamp)
		if err != nil {
			return "", err
		}

		if currentDate.Year() != date.Year() || currentDate.Month() != date.Month() {
			str = str + "<h2>" + date.Format("January 2006") + "</h2>"
		}

		currentDate = date

		str += "<span class=\"entry\">"
		if entry.KindName == "starred" {
			str += "<b>"
		}
		str += "<a class=\"entry-title\" href=" + entry.Url + " target=\"_blank\">" + entry.KindEmoji + " " + entry.Title + "</a>"
		if entry.KindName == "starred" {
			str += "</b>"
		}
		str += " - <span class=\"entry-description\">" + entry.Description + "</span>"
		str += " - <span class=\"entry-date\">" + date.Format(time.DateOnly) + "</span>"
		str += "</span><br>"
	}

	return str, nil
}