|
|
@ -74,21 +74,33 @@ type Store interface { |
|
|
|
// ascending, or empty slice.
|
|
|
|
// ascending, or empty slice.
|
|
|
|
GetByTag(tag string) ([]StoredPost, error) |
|
|
|
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 will delete the StoredPost with the given ID.
|
|
|
|
Delete(id string) error |
|
|
|
Delete(id string) error |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type store struct { |
|
|
|
type store struct { |
|
|
|
db *sql.DB |
|
|
|
db *sql.DB |
|
|
|
|
|
|
|
order string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewStore initializes a new Store using an existing SQLDB.
|
|
|
|
// NewStore initializes a new Store using an existing SQLDB.
|
|
|
|
func NewStore(db *SQLDB) Store { |
|
|
|
func NewStore(db *SQLDB) Store { |
|
|
|
return &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.
|
|
|
|
// if the callback returns an error then the transaction is aborted.
|
|
|
|
func (s *store) withTx(cb func(*sql.Tx) error) error { |
|
|
|
func (s *store) withTx(cb func(*sql.Tx) error) error { |
|
|
|
|
|
|
|
|
|
|
@ -185,15 +197,17 @@ func (s *store) get( |
|
|
|
[]StoredPost, error, |
|
|
|
[]StoredPost, error, |
|
|
|
) { |
|
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
query := ` |
|
|
|
query := fmt.Sprintf( |
|
|
|
SELECT |
|
|
|
`SELECT |
|
|
|
p.id, p.title, p.description, p.series, pt.tag, |
|
|
|
p.id, p.title, p.description, p.series, pt.tag, |
|
|
|
p.published_at, p.last_updated_at, |
|
|
|
p.published_at, p.last_updated_at, |
|
|
|
p.body |
|
|
|
p.body |
|
|
|
FROM posts p |
|
|
|
FROM posts p |
|
|
|
LEFT JOIN post_tags pt ON (p.id = pt.post_id) |
|
|
|
LEFT JOIN post_tags pt ON (p.id = pt.post_id) |
|
|
|
` + where + ` |
|
|
|
`+where+` |
|
|
|
ORDER BY p.published_at ASC, p.title ASC` |
|
|
|
ORDER BY p.published_at %s, p.title %s`, |
|
|
|
|
|
|
|
s.order, s.order, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if limit > 0 { |
|
|
|
if limit > 0 { |
|
|
|
query += fmt.Sprintf(" LIMIT %d", limit) |
|
|
|
query += fmt.Sprintf(" LIMIT %d", limit) |
|
|
|