mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-22 03:37:43 +00:00
record watcher hits
This commit is contained in:
parent
24ef6cce6b
commit
537d1711fe
4 changed files with 21 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue