diff --git a/internal/api/watcher.go b/internal/api/watcher.go index 426e7c2..77a8528 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -140,6 +140,7 @@ type watcherItem struct { Keyword string Flair string Domain string + Hits int64 } func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { @@ -163,6 +164,7 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { Keyword: watcher.Keyword, Flair: watcher.Flair, Domain: watcher.Domain, + Hits: watcher.Hits, } wis[i] = wi diff --git a/internal/domain/watcher.go b/internal/domain/watcher.go index fe148dd..3a77404 100644 --- a/internal/domain/watcher.go +++ b/internal/domain/watcher.go @@ -14,6 +14,7 @@ type Watcher struct { Keyword string Flair string Domain string + Hits int64 } type WatcherRepository interface { @@ -23,5 +24,6 @@ type WatcherRepository interface { Create(ctx context.Context, watcher *Watcher) error Update(ctx context.Context, watcher *Watcher) error + IncrementHits(ctx context.Context, id int64) error Delete(ctx context.Context, id int64) error } diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index 9dfe158..6de0191 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -38,6 +38,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg &watcher.Keyword, &watcher.Flair, &watcher.Domain, + &watcher.Hits, ); err != nil { return nil, err } @@ -48,7 +49,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (domain.Watcher, error) { query := ` - SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain + SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain, hits FROM watchers WHERE id = $1` @@ -65,7 +66,7 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma func (p *postgresWatcherRepository) GetBySubredditID(ctx context.Context, id int64) ([]domain.Watcher, error) { query := ` - SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain + SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain, hits FROM watchers WHERE subreddit_id = $1` @@ -83,7 +84,8 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c watchers.upvotes, watchers.keyword, watchers.flair, - watchers.domain + watchers.domain, + watchers.hits FROM watchers INNER JOIN accounts ON watchers.account_id = accounts.id INNER JOIN devices ON watchers.device_id = devices.id @@ -142,6 +144,16 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain. return err } +func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error { + query := `UPDATE watchers SET hits = hits + 1 WHERE id = $1` + res, err := p.pool.Exec(ctx, query, id) + + if res.RowsAffected() != 1 { + return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected()) + } + return err +} + func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error { query := `DELETE FROM watchers WHERE id = $1` res, err := p.pool.Exec(ctx, query, id) diff --git a/internal/worker/subreddits.go b/internal/worker/subreddits.go index c3badb9..0312606 100644 --- a/internal/worker/subreddits.go +++ b/internal/worker/subreddits.go @@ -329,6 +329,8 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) { continue } + _ = sc.watcherRepo.IncrementHits(ctx, watcher.ID) + lockKey := fmt.Sprintf("watcher:%d:%s", watcher.DeviceID, post.ID) notified, _ := sc.redis.Get(ctx, lockKey).Bool()