summaryrefslogtreecommitdiff
path: root/srv/src/post
diff options
context:
space:
mode:
Diffstat (limited to 'srv/src/post')
-rw-r--r--srv/src/post/post.go26
-rw-r--r--srv/src/post/post_test.go41
2 files changed, 61 insertions, 6 deletions
diff --git a/srv/src/post/post.go b/srv/src/post/post.go
index 30ded15..cadfcfc 100644
--- a/srv/src/post/post.go
+++ b/srv/src/post/post.go
@@ -74,21 +74,33 @@ type Store interface {
// ascending, or empty slice.
GetByTag(tag string) ([]StoredPost, error)
+ // WithOrderDesc will return a Store whose Get operations return Posts in
+ // time descending order, rather than ascending.
+ WithOrderDesc() Store
+
// Delete will delete the StoredPost with the given ID.
Delete(id string) error
}
type store struct {
- db *sql.DB
+ db *sql.DB
+ order string
}
// NewStore initializes a new Store using an existing SQLDB.
func NewStore(db *SQLDB) Store {
return &store{
- db: db.db,
+ db: db.db,
+ order: "ASC",
}
}
+func (s *store) WithOrderDesc() Store {
+ s2 := *s
+ s2.order = "DESC"
+ return &s2
+}
+
// if the callback returns an error then the transaction is aborted.
func (s *store) withTx(cb func(*sql.Tx) error) error {
@@ -185,15 +197,17 @@ func (s *store) get(
[]StoredPost, error,
) {
- query := `
- SELECT
+ query := fmt.Sprintf(
+ `SELECT
p.id, p.title, p.description, p.series, pt.tag,
p.published_at, p.last_updated_at,
p.body
FROM posts p
LEFT JOIN post_tags pt ON (p.id = pt.post_id)
- ` + where + `
- ORDER BY p.published_at ASC, p.title ASC`
+ `+where+`
+ ORDER BY p.published_at %s, p.title %s`,
+ s.order, s.order,
+ )
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
diff --git a/srv/src/post/post_test.go b/srv/src/post/post_test.go
index 55a29ea..c5587c8 100644
--- a/srv/src/post/post_test.go
+++ b/srv/src/post/post_test.go
@@ -187,6 +187,47 @@ func TestStore(t *testing.T) {
assertPostsEqual(t, posts[4:], gotPosts)
})
+ t.Run("get_desc", func(t *testing.T) {
+ h := newStoreTestHarness(t)
+ h.store = h.store.WithOrderDesc()
+
+ now := h.clock.Now().UTC()
+
+ posts := []StoredPost{
+ h.testStoredPost(3),
+ h.testStoredPost(2),
+ h.testStoredPost(1),
+ h.testStoredPost(0),
+ }
+
+ for _, post := range posts {
+ assert.NoError(t, h.store.Set(post.Post, now))
+ }
+
+ gotPosts, hasMore, err := h.store.Get(0, 2)
+ assert.NoError(t, err)
+ assert.True(t, hasMore)
+ assertPostsEqual(t, posts[:2], gotPosts)
+
+ gotPosts, hasMore, err = h.store.Get(1, 2)
+ assert.NoError(t, err)
+ assert.False(t, hasMore)
+ assertPostsEqual(t, posts[2:4], gotPosts)
+
+ posts = append([]StoredPost{h.testStoredPost(4)}, posts...)
+ assert.NoError(t, h.store.Set(posts[0].Post, now))
+
+ gotPosts, hasMore, err = h.store.Get(1, 2)
+ assert.NoError(t, err)
+ assert.True(t, hasMore)
+ assertPostsEqual(t, posts[2:4], gotPosts)
+
+ gotPosts, hasMore, err = h.store.Get(2, 2)
+ assert.NoError(t, err)
+ assert.False(t, hasMore)
+ assertPostsEqual(t, posts[4:], gotPosts)
+ })
+
t.Run("get_by_series", func(t *testing.T) {
h := newStoreTestHarness(t)