From 3059909deb5e21746d3d95e4989a78db51ccec33 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 11:15:37 -0600 Subject: Publish new posts to mailing list --- srv/src/post/post.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'srv/src/post/post.go') 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( -- cgit v1.2.3