From 1f42c5e000c3fc09e201a8e87eb79ea05cad7e35 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 17:43:47 -0600 Subject: [PATCH] Add tag selector to index --- srv/src/http/index.go | 24 +++++++++++++++++++++++- srv/src/http/tpl/index.html | 7 +++++++ srv/src/post/post.go | 27 +++++++++++++++++++++++++++ srv/src/post/post_test.go | 4 ++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/srv/src/http/index.go b/srv/src/http/index.go index 4557f95..aa606d6 100644 --- a/srv/src/http/index.go +++ b/srv/src/http/index.go @@ -30,7 +30,19 @@ func (a *api) renderIndexHandler() http.Handler { 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 { apiutil.InternalServerError( rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), @@ -38,13 +50,23 @@ func (a *api) renderIndexHandler() http.Handler { return } + tags, err := a.params.PostStore.GetTags() + if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("fething tags: %w", err), + ) + return + } + tplPayload := struct { Posts []post.StoredPost PrevPage, NextPage int + Tags []string }{ Posts: posts, PrevPage: -1, NextPage: -1, + Tags: tags, } if page > 0 { diff --git a/srv/src/http/tpl/index.html b/srv/src/http/tpl/index.html index e27cbef..c75affc 100644 --- a/srv/src/http/tpl/index.html +++ b/srv/src/http/tpl/index.html @@ -1,5 +1,12 @@ {{ define "body" }} +

+ By Tag: + {{ range .Payload.Tags }} + {{ . }} + {{ end }} +

+