Add tag selector to index

This commit is contained in:
Brian Picciano 2022-05-20 17:43:47 -06:00
parent 47d4787907
commit 1f42c5e000
4 changed files with 61 additions and 1 deletions

View File

@ -30,7 +30,19 @@ func (a *api) renderIndexHandler() http.Handler {
return return
} }
posts, hasMore, err := a.params.PostStore.Get(page, pageCount) tag := r.FormValue("tag")
var (
posts []post.StoredPost
hasMore bool
)
if tag == "" {
posts, hasMore, err = a.params.PostStore.Get(page, pageCount)
} else {
posts, err = a.params.PostStore.GetByTag(tag)
}
if err != nil { if err != nil {
apiutil.InternalServerError( apiutil.InternalServerError(
rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err),
@ -38,13 +50,23 @@ func (a *api) renderIndexHandler() http.Handler {
return return
} }
tags, err := a.params.PostStore.GetTags()
if err != nil {
apiutil.InternalServerError(
rw, r, fmt.Errorf("fething tags: %w", err),
)
return
}
tplPayload := struct { tplPayload := struct {
Posts []post.StoredPost Posts []post.StoredPost
PrevPage, NextPage int PrevPage, NextPage int
Tags []string
}{ }{
Posts: posts, Posts: posts,
PrevPage: -1, PrevPage: -1,
NextPage: -1, NextPage: -1,
Tags: tags,
} }
if page > 0 { if page > 0 {

View File

@ -1,5 +1,12 @@
{{ define "body" }} {{ define "body" }}
<p>
By Tag:
{{ range .Payload.Tags }}
<a href="{{ BlogURL "/" }}?tag={{ . }}">{{ . }}</a>
{{ end }}
</p>
<ul id="posts-list"> <ul id="posts-list">
{{ range .Payload.Posts }} {{ range .Payload.Posts }}

View File

@ -68,6 +68,9 @@ type Store interface {
// descending, or empty slice. // descending, or empty slice.
GetByTag(tag string) ([]StoredPost, error) GetByTag(tag string) ([]StoredPost, error)
// GetTags returns all tags which have at least one Post using them.
GetTags() ([]string, error)
// Delete will delete the StoredPost with the given ID. // Delete will delete the StoredPost with the given ID.
Delete(id string) error Delete(id string) error
} }
@ -340,6 +343,30 @@ func (s *store) GetByTag(tag string) ([]StoredPost, error) {
return posts, err return posts, err
} }
func (s *store) GetTags() ([]string, error) {
rows, err := s.db.Query(`SELECT tag FROM post_tags GROUP BY tag`)
if err != nil {
return nil, fmt.Errorf("querying all tags: %w", err)
}
defer rows.Close()
var tags []string
for rows.Next() {
var tag string
if err := rows.Scan(&tag); err != nil {
return nil, fmt.Errorf("scanning tag: %w", err)
}
tags = append(tags, tag)
}
return tags, nil
}
func (s *store) Delete(id string) error { func (s *store) Delete(id string) error {
tx, err := s.db.Begin() tx, err := s.db.Begin()

View File

@ -252,5 +252,9 @@ func TestStore(t *testing.T) {
bazPosts, err := h.store.GetByTag("baz") bazPosts, err := h.store.GetByTag("baz")
assert.NoError(t, err) assert.NoError(t, err)
assert.Empty(t, bazPosts) assert.Empty(t, bazPosts)
tags, err := h.store.GetTags()
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"foo", "bar"}, tags)
}) })
} }