From e5ea13114519065cbf03308ab037e0eddfacafab Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Tue, 1 Mar 2022 17:20:36 -0500 Subject: [PATCH] include watcher original name in response --- internal/api/watcher.go | 38 +++++++++++++------------ internal/domain/watcher.go | 9 +++--- internal/repository/postgres_watcher.go | 22 +++++++++++++- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/internal/api/watcher.go b/internal/api/watcher.go index 6a6278f..e2e3226 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -215,15 +215,16 @@ func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) { } type watcherItem struct { - ID int64 `json:"id"` - CreatedAt float64 `json:"created_at"` - Type string `json:"type"` - Label string `json:"label"` - Upvotes int64 `json:"upvotes"` - Keyword string `json:"keyword"` - Flair string `json:"flair"` - Domain string `json:"domain"` - Hits int64 `json:"hits"` + ID int64 `json:"id"` + CreatedAt float64 `json:"created_at"` + Type string `json:"type"` + Label string `json:"label"` + SourceLabel string `json:"source_label"` + Upvotes int64 `json:"upvotes"` + Keyword string `json:"keyword"` + Flair string `json:"flair"` + Domain string `json:"domain"` + Hits int64 `json:"hits"` } func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { @@ -242,15 +243,16 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { wis := make([]watcherItem, len(watchers)) for i, watcher := range watchers { wi := watcherItem{ - ID: watcher.ID, - CreatedAt: watcher.CreatedAt, - Type: watcher.Type.String(), - Label: watcher.Label, - Upvotes: watcher.Upvotes, - Keyword: watcher.Keyword, - Flair: watcher.Flair, - Domain: watcher.Domain, - Hits: watcher.Hits, + ID: watcher.ID, + CreatedAt: watcher.CreatedAt, + Type: watcher.Type.String(), + Label: watcher.Label, + SourceLabel: watcher.WatcheeLabel, + Upvotes: watcher.Upvotes, + 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 0ad4811..a7a7713 100644 --- a/internal/domain/watcher.go +++ b/internal/domain/watcher.go @@ -33,10 +33,11 @@ type Watcher struct { LastNotifiedAt float64 Label string - DeviceID int64 - AccountID int64 - Type WatcherType - WatcheeID int64 + DeviceID int64 + AccountID int64 + Type WatcherType + WatcheeID int64 + WatcheeLabel string Author string Subreddit string diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index 3cd4ab5..0811ab3 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -28,6 +28,8 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg var watchers []domain.Watcher for rows.Next() { var watcher domain.Watcher + var subredditLabel, userLabel string + if err := rows.Scan( &watcher.ID, &watcher.CreatedAt, @@ -50,9 +52,19 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg &watcher.Account.ID, &watcher.Account.AccessToken, &watcher.Account.RefreshToken, + &subredditLabel, + &userLabel, ); err != nil { return nil, err } + + switch watcher.Type { + case domain.SubredditWatcher, domain.TrendingWatcher: + watcher.WatcheeLabel = subredditLabel + case domain.UserWatcher: + watcher.WatcheeLabel = userLabel + } + watchers = append(watchers, watcher) } return watchers, nil @@ -81,10 +93,14 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma devices.sandbox, accounts.id, accounts.access_token, - accounts.refresh_token + accounts.refresh_token, + subreddits.name AS subreddit_label, + users.name AS user_label FROM watchers INNER JOIN devices ON watchers.device_id = devices.id INNER JOIN accounts ON watchers.account_id = accounts.id + LEFT JOIN subreddits ON watchers.type IN(0,2) AND watchers.watchee_id = subreddits.id + LEFT JOIN users ON watchers.type = 1 AND watchers.watchee_id = users.id WHERE watchers.id = $1` watchers, err := p.fetch(ctx, query, id) @@ -125,6 +141,8 @@ func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, t FROM watchers INNER JOIN devices ON watchers.device_id = devices.id INNER JOIN accounts ON watchers.account_id = accounts.id + LEFT JOIN subreddits ON watchers.type IN(0,2) AND watchers.watchee_id = subreddits.id + LEFT JOIN users ON watchers.type = 1 AND watchers.watchee_id = users.id WHERE watchers.type = $1 AND watchers.watchee_id = $2` return p.fetch(ctx, query, typ, id) @@ -169,6 +187,8 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c FROM watchers INNER JOIN accounts ON watchers.account_id = accounts.id INNER JOIN devices ON watchers.device_id = devices.id + LEFT JOIN subreddits ON watchers.type IN(0,2) AND watchers.watchee_id = subreddits.id + LEFT JOIN users ON watchers.type = 1 AND watchers.watchee_id = users.id WHERE devices.apns_token = $1 AND accounts.account_id = $2`