mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-22 11:47:42 +00:00
Allow editing watcher subreddits too
This commit is contained in:
parent
0afa10d64d
commit
f6f13bbd2f
2 changed files with 71 additions and 10 deletions
|
@ -183,7 +183,11 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id, err := strconv.ParseInt(vars["watcherID"], 10, 64)
|
apns := vars["apns"]
|
||||||
|
wid := vars["watcherID"]
|
||||||
|
rid := vars["redditID"]
|
||||||
|
|
||||||
|
id, err := strconv.ParseInt(wid, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, 422, err)
|
a.errorResponse(w, r, 422, err)
|
||||||
return
|
return
|
||||||
|
@ -209,13 +213,68 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher.Label = ewr.Label
|
watcher.Label = ewr.Label
|
||||||
watcher.Author = strings.ToLower(ewr.Criteria.Author)
|
watcher.Author = strings.ToLower(ewr.User)
|
||||||
watcher.Subreddit = strings.ToLower(ewr.Criteria.Subreddit)
|
watcher.Subreddit = strings.ToLower(ewr.Subreddit)
|
||||||
watcher.Upvotes = ewr.Criteria.Upvotes
|
watcher.Upvotes = ewr.Criteria.Upvotes
|
||||||
watcher.Keyword = strings.ToLower(ewr.Criteria.Keyword)
|
watcher.Keyword = strings.ToLower(ewr.Criteria.Keyword)
|
||||||
watcher.Flair = strings.ToLower(ewr.Criteria.Flair)
|
watcher.Flair = strings.ToLower(ewr.Criteria.Flair)
|
||||||
watcher.Domain = strings.ToLower(ewr.Criteria.Domain)
|
watcher.Domain = strings.ToLower(ewr.Criteria.Domain)
|
||||||
|
|
||||||
|
if watcher.Type == domain.SubredditWatcher {
|
||||||
|
lsr := strings.ToLower(watcher.Subreddit)
|
||||||
|
if watcher.WatcheeLabel != lsr {
|
||||||
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
|
||||||
|
if err != nil {
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(accs) == 0 {
|
||||||
|
err := errors.New("cannot create watchers without account")
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
account := accs[0]
|
||||||
|
found := false
|
||||||
|
for _, acc := range accs {
|
||||||
|
if acc.AccountID == rid {
|
||||||
|
found = true
|
||||||
|
account = acc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
err := errors.New("account not associated with device")
|
||||||
|
a.errorResponse(w, r, 401, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ac := a.reddit.NewAuthenticatedClient(account.AccountID, account.RefreshToken, account.AccessToken)
|
||||||
|
|
||||||
|
srr, err := ac.SubredditAbout(ctx, lsr)
|
||||||
|
if err != nil {
|
||||||
|
a.errorResponse(w, r, 422, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sr, err := a.subredditRepo.GetByName(ctx, lsr)
|
||||||
|
if err != nil {
|
||||||
|
switch err {
|
||||||
|
case domain.ErrNotFound:
|
||||||
|
// Might be that we don't know about that subreddit yet
|
||||||
|
sr = domain.Subreddit{SubredditID: srr.ID, Name: srr.Name}
|
||||||
|
_ = a.subredditRepo.CreateOrUpdate(ctx, &sr)
|
||||||
|
default:
|
||||||
|
a.errorResponse(w, r, 500, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watcher.WatcheeID = sr.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := a.watcherRepo.Update(ctx, &watcher); err != nil {
|
if err := a.watcherRepo.Update(ctx, &watcher); err != nil {
|
||||||
a.errorResponse(w, r, 500, err)
|
a.errorResponse(w, r, 500, err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -245,19 +245,21 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
UPDATE watchers
|
UPDATE watchers
|
||||||
SET author = $2,
|
SET watchee_id = $2,
|
||||||
subreddit = $3,
|
author = $3,
|
||||||
upvotes = $4,
|
subreddit = $4,
|
||||||
keyword = $5,
|
upvotes = $5,
|
||||||
flair = $6,
|
keyword = $6,
|
||||||
domain = $7,
|
flair = $7,
|
||||||
label = $8
|
domain = $8,
|
||||||
|
label = $9
|
||||||
WHERE id = $1`
|
WHERE id = $1`
|
||||||
|
|
||||||
res, err := p.conn.Exec(
|
res, err := p.conn.Exec(
|
||||||
ctx,
|
ctx,
|
||||||
query,
|
query,
|
||||||
watcher.ID,
|
watcher.ID,
|
||||||
|
watcher.WatcheeID,
|
||||||
watcher.Author,
|
watcher.Author,
|
||||||
watcher.Subreddit,
|
watcher.Subreddit,
|
||||||
watcher.Upvotes,
|
watcher.Upvotes,
|
||||||
|
|
Loading…
Reference in a new issue