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"`
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"`
@ -246,6 +247,7 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
CreatedAt: watcher.CreatedAt,
Type: watcher.Type.String(),
Label: watcher.Label,
SourceLabel: watcher.WatcheeLabel,
Upvotes: watcher.Upvotes,
Keyword: watcher.Keyword,
Flair: watcher.Flair,

View file

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

View file

@ -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`