Fix new index page

This commit is contained in:
Brian Picciano 2022-05-14 21:30:18 -06:00
parent af08122a25
commit 788aba3d0d
5 changed files with 43 additions and 33 deletions

View File

@ -42,7 +42,7 @@ func mustParseTpl(name string) *template.Template {
func (a *api) renderIndexHandler() http.Handler { func (a *api) renderIndexHandler() http.Handler {
tpl := mustParseTpl("index.html") tpl := mustParseTpl("index.html")
const pageCount = 20 const pageCount = 10
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

View File

@ -1,10 +1,11 @@
{{ define "body" }} {{ define "body" }}
<ul id="posts-list"> <ul id="posts-list">
{{ range .Posts }} {{ range .Posts }}
<li> <li>
<h2> <h2>
<a href="{{ .HTTPPath }}">{{ .Title }}</a> <a href="posts/{{ .HTTPPath }}">{{ .Title }}</a>
</h2> </h2>
<span>{{ .PublishedAt.Format "2006-01-02" }}</span> <span>{{ .PublishedAt.Format "2006-01-02" }}</span>
{{ if not .LastUpdatedAt.IsZero }} {{ if not .LastUpdatedAt.IsZero }}
@ -15,14 +16,21 @@
{{ end }} {{ end }}
</ul> </ul>
{{ end }}
{{ if or (ge .PrevPage 0) (ge .NextPage 0) }}
<div id="page-turner">
{{ if ge .PrevPage 0 }} {{ if ge .PrevPage 0 }}
<a href="?p={{ .PrevPage}}">Previous</a> <a style="float: left;" href="?p={{ .PrevPage}}">Newer</a>
{{ end }} {{ end }}
{{ if ge .NextPage 0 }} {{ if ge .NextPage 0 }}
<a href="?p={{ .NextPage}}">Next</a> <a style="float:right;" href="?p={{ .NextPage}}">Older</a>
{{ end }}
</div>
{{ end }}
{{ end }} {{ end }}
{{ template "base.html" . }} {{ template "base.html" . }}

View File

@ -31,7 +31,7 @@ type Post struct {
ID string ID string
Title string Title string
Description string Description string
Tags []string Tags []string // only alphanumeric supported
Series string Series string
Body string Body string
} }
@ -199,12 +199,13 @@ func (s *store) get(
query := fmt.Sprintf( query := fmt.Sprintf(
`SELECT `SELECT
p.id, p.title, p.description, p.series, pt.tag, p.id, p.title, p.description, p.series, GROUP_CONCAT(pt.tag),
p.published_at, p.last_updated_at, p.published_at, p.last_updated_at,
p.body p.body
FROM posts p FROM posts p
LEFT JOIN post_tags pt ON (p.id = pt.post_id) LEFT JOIN post_tags pt ON (p.id = pt.post_id)
`+where+` `+where+`
GROUP BY (p.id)
ORDER BY p.published_at %s, p.title %s`, ORDER BY p.published_at %s, p.title %s`,
s.order, s.order, s.order, s.order,
) )
@ -243,18 +244,12 @@ func (s *store) get(
return nil, fmt.Errorf("scanning row: %w", err) return nil, fmt.Errorf("scanning row: %w", err)
} }
if tag.Valid {
if l := len(posts); l > 0 && posts[l-1].ID == post.ID {
posts[l-1].Tags = append(posts[l-1].Tags, tag.String)
continue
}
post.Tags = append(post.Tags, tag.String)
}
post.Series = series.String post.Series = series.String
if tag.String != "" {
post.Tags = strings.Split(tag.String, ",")
}
if publishedAt.Valid { if publishedAt.Valid {
post.PublishedAt = time.Unix(publishedAt.Int64, 0).UTC() post.PublishedAt = time.Unix(publishedAt.Int64, 0).UTC()
} }

View File

@ -159,6 +159,8 @@ func TestStore(t *testing.T) {
h.testStoredPost(3), h.testStoredPost(3),
} }
posts[1].Tags = []string{"1", "2"}
for _, post := range posts { for _, post := range posts {
assert.NoError(t, h.store.Set(post.Post, now)) assert.NoError(t, h.store.Set(post.Post, now))
} }

View File

@ -98,3 +98,8 @@ footer {
{ {
max-width: 100%; max-width: 100%;
} }
#page-turner {
height: 2rem;
font-size: 2rem;
}