summaryrefslogtreecommitdiff
path: root/srv/src/post/post.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-21 11:15:37 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-21 11:15:37 -0600
commit3059909deb5e21746d3d95e4989a78db51ccec33 (patch)
treee0baba8c3d29b38deb94d67ef2b9781efd22cb71 /srv/src/post/post.go
parent3e67823205a59420d60efe536d4319fd7f1e457a (diff)
Publish new posts to mailing list
Diffstat (limited to 'srv/src/post/post.go')
-rw-r--r--srv/src/post/post.go24
1 files changed, 18 insertions, 6 deletions
diff --git a/srv/src/post/post.go b/srv/src/post/post.go
index 29a984f..a39af61 100644
--- a/srv/src/post/post.go
+++ b/srv/src/post/post.go
@@ -48,9 +48,10 @@ type StoredPost struct {
// Store is used for storing posts to a persistent storage.
type Store interface {
- // Set sets the Post data into the storage, keyed by the Post's ID. It
- // overwrites a previous Post with the same ID, if there was one.
- Set(post Post, now time.Time) error
+ // Set sets the Post data into the storage, keyed by the Post's ID. If there
+ // was not a previously existing Post with the same ID then Set returns
+ // true. It overwrites the previous Post with the same ID otherwise.
+ Set(post Post, now time.Time) (bool, error)
// Get returns count StoredPosts, sorted time descending, offset by the
// given page number. The returned boolean indicates if there are more pages
@@ -114,13 +115,15 @@ func (s *store) withTx(cb func(*sql.Tx) error) error {
return nil
}
-func (s *store) Set(post Post, now time.Time) error {
+func (s *store) Set(post Post, now time.Time) (bool, error) {
if post.ID == "" {
- return errors.New("post ID can't be empty")
+ return false, errors.New("post ID can't be empty")
}
- return s.withTx(func(tx *sql.Tx) error {
+ var first bool
+
+ err := s.withTx(func(tx *sql.Tx) error {
nowTS := now.Unix()
@@ -173,8 +176,17 @@ func (s *store) Set(post Post, now time.Time) error {
}
}
+ err = tx.QueryRow(
+ `SELECT 1 FROM posts WHERE id=? AND last_updated_at IS NULL`,
+ post.ID,
+ ).Scan(new(int))
+
+ first = !errors.Is(err, sql.ErrNoRows)
+
return nil
})
+
+ return first, err
}
func (s *store) get(