From 47d478790742a8876af187b34b766b0e8a772bf5 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 17:31:44 -0600 Subject: Always return results in time desc order from PostStore --- srv/src/post/post.go | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) (limited to 'srv/src/post/post.go') 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) -- cgit v1.2.3