list watchers endpoint

This commit is contained in:
Andre Medeiros 2021-09-25 14:17:23 -04:00
parent bc9456cba2
commit 24ef6cce6b
4 changed files with 64 additions and 2 deletions

View file

@ -90,8 +90,9 @@ func (a *api) Routes() *mux.Router {
r.HandleFunc("/v1/device/{apns}/accounts", a.upsertAccountsHandler).Methods("POST")
r.HandleFunc("/v1/device/{apns}/account/{redditID}", a.disassociateAccountHandler).Methods("DELETE")
r.HandleFunc("/v1/device/{apns}/accounts/{redditID}/watcher", a.createWatcherHandler).Methods("POST")
r.HandleFunc("/v1/device/{apns}/accounts/{redditID}/watcher/{watcherID}", a.deleteWatcherHandler).Methods("DELETE")
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watcher", a.createWatcherHandler).Methods("POST")
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watchers", a.listWatchersHandler).Methods("GET")
r.HandleFunc("/v1/device/{apns}/account/{redditID}/watcher/{watcherID}", a.deleteWatcherHandler).Methods("DELETE")
r.HandleFunc("/v1/receipt", a.checkReceiptHandler).Methods("POST")
r.HandleFunc("/v1/receipt/{apns}", a.checkReceiptHandler).Methods("POST")

View file

@ -133,3 +133,41 @@ func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) {
_ = a.watcherRepo.Delete(ctx, id)
w.WriteHeader(http.StatusOK)
}
type watcherItem struct {
ID int64
Upvotes int64
Keyword string
Flair string
Domain string
}
func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
vars := mux.Vars(r)
apns := vars["apns"]
redditID := vars["redditID"]
watchers, err := a.watcherRepo.GetByDeviceAPNSTokenAndAccountRedditID(ctx, apns, redditID)
if err != nil {
a.errorResponse(w, r, 400, err.Error())
return
}
wis := make([]watcherItem, len(watchers))
for i, watcher := range watchers {
wi := watcherItem{
ID: watcher.ID,
Upvotes: watcher.Upvotes,
Keyword: watcher.Keyword,
Flair: watcher.Flair,
Domain: watcher.Domain,
}
wis[i] = wi
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(wis)
}

View file

@ -19,6 +19,7 @@ type Watcher struct {
type WatcherRepository interface {
GetByID(ctx context.Context, id int64) (Watcher, error)
GetBySubredditID(ctx context.Context, id int64) ([]Watcher, error)
GetByDeviceAPNSTokenAndAccountRedditID(ctx context.Context, apns string, rid string) ([]Watcher, error)
Create(ctx context.Context, watcher *Watcher) error
Update(ctx context.Context, watcher *Watcher) error

View file

@ -72,6 +72,28 @@ func (p *postgresWatcherRepository) GetBySubredditID(ctx context.Context, id int
return p.fetch(ctx, query, id)
}
func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx context.Context, apns string, rid string) ([]domain.Watcher, error) {
query := `
SELECT
watchers.id,
watchers.created_at,
watchers.device_id,
watchers.account_id,
watchers.subreddit_id,
watchers.upvotes,
watchers.keyword,
watchers.flair,
watchers.domain
FROM watchers
INNER JOIN accounts ON watchers.account_id = accounts.id
INNER JOIN devices ON watchers.device_id = devices.id
WHERE
devices.apns_token = $1 AND
accounts.account_id = $2`
return p.fetch(ctx, query, apns, rid)
}
func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.Watcher) error {
now := float64(time.Now().UTC().Unix())