diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 17:51:41 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 17:51:41 -0600 |
commit | c4692f72e3b0bf18f737add2bbd4e353274fe3ac (patch) | |
tree | 7c0def1d671234d5a3278c61ca32106599e09295 /srv/src | |
parent | 1f42c5e000c3fc09e201a8e87eb79ea05cad7e35 (diff) |
Add tag parameter to feed endpoint
Diffstat (limited to 'srv/src')
-rw-r--r-- | srv/src/http/feed.go | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/srv/src/http/feed.go b/srv/src/http/feed.go index ba8b2e6..2f1faa8 100644 --- a/srv/src/http/feed.go +++ b/srv/src/http/feed.go @@ -7,12 +7,31 @@ import ( "github.com/gorilla/feeds" "github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil" + "github.com/mediocregopher/blog.mediocregopher.com/srv/post" ) func (a *api) renderFeedHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + tag := r.FormValue("tag") + + var ( + posts []post.StoredPost + err error + ) + + if tag == "" { + posts, _, err = a.params.PostStore.Get(0, 20) + } else { + posts, err = a.params.PostStore.GetByTag(tag) + } + + if err != nil { + apiutil.InternalServerError(rw, r, fmt.Errorf("fetching recent posts: %w", err)) + return + } + author := &feeds.Author{ Name: "mediocregopher", } @@ -26,13 +45,7 @@ func (a *api) renderFeedHandler() http.Handler { Author: author, } - recentPosts, _, err := a.params.PostStore.Get(0, 20) - if err != nil { - apiutil.InternalServerError(rw, r, fmt.Errorf("fetching recent posts: %w", err)) - return - } - - for _, post := range recentPosts { + for _, post := range posts { if post.PublishedAt.After(feed.Updated) { feed.Updated = post.PublishedAt |