Add tag parameter to feed endpoint

This commit is contained in:
Brian Picciano 2022-05-20 17:51:41 -06:00
parent 1f42c5e000
commit c4692f72e3

View File

@ -7,12 +7,31 @@ import (
"github.com/gorilla/feeds" "github.com/gorilla/feeds"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil" "github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
) )
func (a *api) renderFeedHandler() http.Handler { func (a *api) renderFeedHandler() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { 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{ author := &feeds.Author{
Name: "mediocregopher", Name: "mediocregopher",
} }
@ -26,13 +45,7 @@ func (a *api) renderFeedHandler() http.Handler {
Author: author, Author: author,
} }
recentPosts, _, err := a.params.PostStore.Get(0, 20) for _, post := range posts {
if err != nil {
apiutil.InternalServerError(rw, r, fmt.Errorf("fetching recent posts: %w", err))
return
}
for _, post := range recentPosts {
if post.PublishedAt.After(feed.Updated) { if post.PublishedAt.After(feed.Updated) {
feed.Updated = post.PublishedAt feed.Updated = post.PublishedAt