include watcher original name in response

This commit is contained in:
Andre Medeiros 2022-03-01 17:20:36 -05:00
parent dfae5d6c77
commit e5ea131145
3 changed files with 46 additions and 23 deletions

View file

@ -219,6 +219,7 @@ type watcherItem struct {
CreatedAt float64 `json:"created_at"` CreatedAt float64 `json:"created_at"`
Type string `json:"type"` Type string `json:"type"`
Label string `json:"label"` Label string `json:"label"`
SourceLabel string `json:"source_label"`
Upvotes int64 `json:"upvotes"` Upvotes int64 `json:"upvotes"`
Keyword string `json:"keyword"` Keyword string `json:"keyword"`
Flair string `json:"flair"` Flair string `json:"flair"`
@ -246,6 +247,7 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
CreatedAt: watcher.CreatedAt, CreatedAt: watcher.CreatedAt,
Type: watcher.Type.String(), Type: watcher.Type.String(),
Label: watcher.Label, Label: watcher.Label,
SourceLabel: watcher.WatcheeLabel,
Upvotes: watcher.Upvotes, Upvotes: watcher.Upvotes,
Keyword: watcher.Keyword, Keyword: watcher.Keyword,
Flair: watcher.Flair, Flair: watcher.Flair,

View file

@ -37,6 +37,7 @@ type Watcher struct {
AccountID int64 AccountID int64
Type WatcherType Type WatcherType
WatcheeID int64 WatcheeID int64
WatcheeLabel string
Author string Author string
Subreddit string Subreddit string

View file

@ -28,6 +28,8 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg
var watchers []domain.Watcher var watchers []domain.Watcher
for rows.Next() { for rows.Next() {
var watcher domain.Watcher var watcher domain.Watcher
var subredditLabel, userLabel string
if err := rows.Scan( if err := rows.Scan(
&watcher.ID, &watcher.ID,
&watcher.CreatedAt, &watcher.CreatedAt,
@ -50,9 +52,19 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg
&watcher.Account.ID, &watcher.Account.ID,
&watcher.Account.AccessToken, &watcher.Account.AccessToken,
&watcher.Account.RefreshToken, &watcher.Account.RefreshToken,
&subredditLabel,
&userLabel,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
switch watcher.Type {
case domain.SubredditWatcher, domain.TrendingWatcher:
watcher.WatcheeLabel = subredditLabel
case domain.UserWatcher:
watcher.WatcheeLabel = userLabel
}
watchers = append(watchers, watcher) watchers = append(watchers, watcher)
} }
return watchers, nil return watchers, nil
@ -81,10 +93,14 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma
devices.sandbox, devices.sandbox,
accounts.id, accounts.id,
accounts.access_token, accounts.access_token,
accounts.refresh_token accounts.refresh_token,
subreddits.name AS subreddit_label,
users.name AS user_label
FROM watchers FROM watchers
INNER JOIN devices ON watchers.device_id = devices.id INNER JOIN devices ON watchers.device_id = devices.id
INNER JOIN accounts ON watchers.account_id = accounts.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` WHERE watchers.id = $1`
watchers, err := p.fetch(ctx, query, id) watchers, err := p.fetch(ctx, query, id)
@ -125,6 +141,8 @@ func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, t
FROM watchers FROM watchers
INNER JOIN devices ON watchers.device_id = devices.id INNER JOIN devices ON watchers.device_id = devices.id
INNER JOIN accounts ON watchers.account_id = accounts.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` WHERE watchers.type = $1 AND watchers.watchee_id = $2`
return p.fetch(ctx, query, typ, id) return p.fetch(ctx, query, typ, id)
@ -169,6 +187,8 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c
FROM watchers FROM watchers
INNER JOIN accounts ON watchers.account_id = accounts.id INNER JOIN accounts ON watchers.account_id = accounts.id
INNER JOIN devices ON watchers.device_id = devices.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 WHERE
devices.apns_token = $1 AND devices.apns_token = $1 AND
accounts.account_id = $2` accounts.account_id = $2`