From 24ef6cce6bf27bd5078c21a7eeca7ef748003ddf Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 25 Sep 2021 14:17:23 -0400 Subject: [PATCH] list watchers endpoint --- internal/api/api.go | 5 ++-- internal/api/watcher.go | 38 +++++++++++++++++++++++++ internal/domain/watcher.go | 1 + internal/repository/postgres_watcher.go | 22 ++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index 48a1c05..df8f564 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -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") diff --git a/internal/api/watcher.go b/internal/api/watcher.go index f17c306..426e7c2 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -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) +} diff --git a/internal/domain/watcher.go b/internal/domain/watcher.go index f32254e..fe148dd 100644 --- a/internal/domain/watcher.go +++ b/internal/domain/watcher.go @@ -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 diff --git a/internal/repository/postgres_watcher.go b/internal/repository/postgres_watcher.go index 34ef28d..9dfe158 100644 --- a/internal/repository/postgres_watcher.go +++ b/internal/repository/postgres_watcher.go @@ -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())