diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 17:31:44 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-05-20 17:31:44 -0600 |
commit | 47d478790742a8876af187b34b766b0e8a772bf5 (patch) | |
tree | e51f5afcd138a1c7cca5ca3b403f02d01b81a318 /srv/src/post/post.go | |
parent | 99f8c1188ccd1580f58ad4c21cece040ed8e874c (diff) |
Always return results in time desc order from PostStore
Diffstat (limited to 'srv/src/post/post.go')
-rw-r--r-- | srv/src/post/post.go | 30 |
1 files changed, 8 insertions, 22 deletions
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) |