summaryrefslogtreecommitdiff
path: root/srv
diff options
context:
space:
mode:
Diffstat (limited to 'srv')
-rw-r--r--srv/src/http/feed.go2
-rw-r--r--srv/src/http/index.go2
-rw-r--r--srv/src/http/posts.go6
-rw-r--r--srv/src/post/post.go30
-rw-r--r--srv/src/post/post_test.go55
5 files changed, 19 insertions, 76 deletions
diff --git a/srv/src/http/feed.go b/srv/src/http/feed.go
index 8c0ef1c..ba8b2e6 100644
--- a/srv/src/http/feed.go
+++ b/srv/src/http/feed.go
@@ -26,7 +26,7 @@ func (a *api) renderFeedHandler() http.Handler {
Author: author,
}
- recentPosts, _, err := a.params.PostStore.WithOrderDesc().Get(0, 20)
+ recentPosts, _, err := a.params.PostStore.Get(0, 20)
if err != nil {
apiutil.InternalServerError(rw, r, fmt.Errorf("fetching recent posts: %w", err))
return
diff --git a/srv/src/http/index.go b/srv/src/http/index.go
index bb76568..4557f95 100644
--- a/srv/src/http/index.go
+++ b/srv/src/http/index.go
@@ -30,7 +30,7 @@ func (a *api) renderIndexHandler() http.Handler {
return
}
- posts, hasMore, err := a.params.PostStore.WithOrderDesc().Get(page, pageCount)
+ posts, hasMore, err := a.params.PostStore.Get(page, pageCount)
if err != nil {
apiutil.InternalServerError(
rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err),
diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go
index 816e361..e1ddf80 100644
--- a/srv/src/http/posts.go
+++ b/srv/src/http/posts.go
@@ -69,11 +69,11 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload,
}
if !foundThis {
- tplPayload.SeriesPrevious = &seriesPost
+ tplPayload.SeriesNext = &seriesPost
continue
}
- tplPayload.SeriesNext = &seriesPost
+ tplPayload.SeriesPrevious = &seriesPost
break
}
}
@@ -138,7 +138,7 @@ func (a *api) renderPostsIndexHandler() http.Handler {
return
}
- posts, hasMore, err := a.params.PostStore.WithOrderDesc().Get(page, pageCount)
+ posts, hasMore, err := a.params.PostStore.Get(page, pageCount)
if err != nil {
apiutil.InternalServerError(
rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err),
diff --git a/srv/src/post/post.go b/srv/src/post/post.go
index 803356e..bf851af 100644
--- a/srv/src/post/post.go
+++ b/srv/src/post/post.go
@@ -61,40 +61,28 @@ type Store interface {
GetByID(id string) (StoredPost, error)
// GetBySeries returns all StoredPosts with the given series, sorted time
- // ascending, or empty slice.
+ // descending, or empty slice.
GetBySeries(series string) ([]StoredPost, error)
// GetByTag returns all StoredPosts with the given tag, sorted time
- // ascending, or empty slice.
+ // descending, 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
- order string
+ db *sql.DB
}
// NewStore initializes a new Store using an existing SQLDB.
func NewStore(db *SQLDB) Store {
return &store{
- db: db.db,
- order: "ASC",
+ db: db.db,
}
}
-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 {
@@ -196,18 +184,16 @@ func (s *store) get(
[]StoredPost, error,
) {
- query := fmt.Sprintf(
- `SELECT
+ query := `
+ SELECT
p.id, p.title, p.description, p.series, GROUP_CONCAT(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+`
+ ` + where + `
GROUP BY (p.id)
- ORDER BY p.published_at %s, p.title %s`,
- s.order, s.order,
- )
+ ORDER BY p.published_at DESC, p.title DESC`
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 db247d1..97757e5 100644
--- a/srv/src/post/post_test.go
+++ b/srv/src/post/post_test.go
@@ -153,49 +153,6 @@ func TestStore(t *testing.T) {
now := h.clock.Now().UTC()
posts := []StoredPost{
- h.testStoredPost(0),
- h.testStoredPost(1),
- h.testStoredPost(2),
- h.testStoredPost(3),
- }
-
- posts[1].Tags = []string{"1", "2"}
-
- 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(posts, h.testStoredPost(4))
- assert.NoError(t, h.store.Set(posts[4].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_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),
@@ -236,10 +193,10 @@ func TestStore(t *testing.T) {
now := h.clock.Now().UTC()
posts := []StoredPost{
- h.testStoredPost(0),
- h.testStoredPost(1),
- h.testStoredPost(2),
h.testStoredPost(3),
+ h.testStoredPost(2),
+ h.testStoredPost(1),
+ h.testStoredPost(0),
}
posts[0].Series = "foo"
@@ -270,10 +227,10 @@ func TestStore(t *testing.T) {
now := h.clock.Now().UTC()
posts := []StoredPost{
- h.testStoredPost(0),
- h.testStoredPost(1),
- h.testStoredPost(2),
h.testStoredPost(3),
+ h.testStoredPost(2),
+ h.testStoredPost(1),
+ h.testStoredPost(0),
}
posts[0].Tags = []string{"foo"}