summaryrefslogtreecommitdiff
path: root/srv/src/post
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-20 17:43:47 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-20 17:43:47 -0600
commit1f42c5e000c3fc09e201a8e87eb79ea05cad7e35 (patch)
tree1c23c94cf1a59e25aea63e4f422aa7cd86387805 /srv/src/post
parent47d478790742a8876af187b34b766b0e8a772bf5 (diff)
Add tag selector to index
Diffstat (limited to 'srv/src/post')
-rw-r--r--srv/src/post/post.go27
-rw-r--r--srv/src/post/post_test.go4
2 files changed, 31 insertions, 0 deletions
diff --git a/srv/src/post/post.go b/srv/src/post/post.go
index bf851af..29a984f 100644
--- a/srv/src/post/post.go
+++ b/srv/src/post/post.go
@@ -68,6 +68,9 @@ type Store interface {
// descending, or empty slice.
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(id string) error
}
@@ -340,6 +343,30 @@ func (s *store) GetByTag(tag string) ([]StoredPost, error) {
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 {
tx, err := s.db.Begin()
diff --git a/srv/src/post/post_test.go b/srv/src/post/post_test.go
index 97757e5..b6d8a2e 100644
--- a/srv/src/post/post_test.go
+++ b/srv/src/post/post_test.go
@@ -252,5 +252,9 @@ func TestStore(t *testing.T) {
bazPosts, err := h.store.GetByTag("baz")
assert.NoError(t, err)
assert.Empty(t, bazPosts)
+
+ tags, err := h.store.GetTags()
+ assert.NoError(t, err)
+ assert.ElementsMatch(t, []string{"foo", "bar"}, tags)
})
}