mirror of
https://github.com/christianselig/apollo-backend
synced 2024-12-22 14:25:28 +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()
|
||||
|
||||
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 {
|
||||
a.errorResponse(w, r, 422, err)
|
||||
return
|
||||
|
@ -209,13 +213,68 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
watcher.Label = ewr.Label
|
||||
watcher.Author = strings.ToLower(ewr.Criteria.Author)
|
||||
watcher.Subreddit = strings.ToLower(ewr.Criteria.Subreddit)
|
||||
watcher.Author = strings.ToLower(ewr.User)
|
||||
watcher.Subreddit = strings.ToLower(ewr.Subreddit)
|
||||
watcher.Upvotes = ewr.Criteria.Upvotes
|
||||
watcher.Keyword = strings.ToLower(ewr.Criteria.Keyword)
|
||||
watcher.Flair = strings.ToLower(ewr.Criteria.Flair)
|
||||
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 {
|
||||
a.errorResponse(w, r, 500, err)
|
||||
return
|
||||
|
|
|
@ -245,19 +245,21 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
|
|||
|
||||
query := `
|
||||
UPDATE watchers
|
||||
SET author = $2,
|
||||
subreddit = $3,
|
||||
upvotes = $4,
|
||||
keyword = $5,
|
||||
flair = $6,
|
||||
domain = $7,
|
||||
label = $8
|
||||
SET watchee_id = $2,
|
||||
author = $3,
|
||||
subreddit = $4,
|
||||
upvotes = $5,
|
||||
keyword = $6,
|
||||
flair = $7,
|
||||
domain = $8,
|
||||
label = $9
|
||||
WHERE id = $1`
|
||||
|
||||
res, err := p.conn.Exec(
|
||||
ctx,
|
||||
query,
|
||||
watcher.ID,
|
||||
watcher.WatcheeID,
|
||||
watcher.Author,
|
||||
watcher.Subreddit,
|
||||
watcher.Upvotes,
|
||||
|
|
Loading…
Reference in a new issue