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

@ -215,15 +215,16 @@ func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) {
} }
type watcherItem struct { type watcherItem struct {
ID int64 `json:"id"` ID int64 `json:"id"`
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"`
Upvotes int64 `json:"upvotes"` SourceLabel string `json:"source_label"`
Keyword string `json:"keyword"` Upvotes int64 `json:"upvotes"`
Flair string `json:"flair"` Keyword string `json:"keyword"`
Domain string `json:"domain"` Flair string `json:"flair"`
Hits int64 `json:"hits"` Domain string `json:"domain"`
Hits int64 `json:"hits"`
} }
func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { 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)) wis := make([]watcherItem, len(watchers))
for i, watcher := range watchers { for i, watcher := range watchers {
wi := watcherItem{ wi := watcherItem{
ID: watcher.ID, ID: watcher.ID,
CreatedAt: watcher.CreatedAt, CreatedAt: watcher.CreatedAt,
Type: watcher.Type.String(), Type: watcher.Type.String(),
Label: watcher.Label, Label: watcher.Label,
Upvotes: watcher.Upvotes, SourceLabel: watcher.WatcheeLabel,
Keyword: watcher.Keyword, Upvotes: watcher.Upvotes,
Flair: watcher.Flair, Keyword: watcher.Keyword,
Domain: watcher.Domain, Flair: watcher.Flair,
Hits: watcher.Hits, Domain: watcher.Domain,
Hits: watcher.Hits,
} }
wis[i] = wi wis[i] = wi

View file

@ -33,10 +33,11 @@ type Watcher struct {
LastNotifiedAt float64 LastNotifiedAt float64
Label string Label string
DeviceID int64 DeviceID int64
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`