apollo-backend/internal/repository/postgres_watcher.go

291 lines
7.5 KiB
Go
Raw Normal View History

2021-09-25 16:56:01 +00:00
package repository
import (
"context"
2021-09-25 18:02:00 +00:00
"time"
2021-09-25 16:56:01 +00:00
"github.com/christianselig/apollo-backend/internal/domain"
)
type postgresWatcherRepository struct {
conn Connection
2021-09-25 16:56:01 +00:00
}
func NewPostgresWatcher(conn Connection) domain.WatcherRepository {
return &postgresWatcherRepository{conn: conn}
2021-09-25 16:56:01 +00:00
}
func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Watcher, error) {
rows, err := p.conn.Query(ctx, query, args...)
2021-09-25 16:56:01 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
var watchers []domain.Watcher
for rows.Next() {
var watcher domain.Watcher
var subredditLabel, userLabel string
2021-09-25 16:56:01 +00:00
if err := rows.Scan(
&watcher.ID,
2021-09-25 18:02:00 +00:00
&watcher.CreatedAt,
&watcher.LastNotifiedAt,
2021-10-10 15:51:42 +00:00
&watcher.Label,
2021-09-25 16:56:01 +00:00
&watcher.DeviceID,
&watcher.AccountID,
2021-10-09 14:59:20 +00:00
&watcher.Type,
&watcher.WatcheeID,
2021-10-10 15:51:42 +00:00
&watcher.Author,
&watcher.Subreddit,
2021-09-25 16:56:01 +00:00
&watcher.Upvotes,
&watcher.Keyword,
&watcher.Flair,
&watcher.Domain,
2021-09-25 18:27:58 +00:00
&watcher.Hits,
2021-10-10 15:51:42 +00:00
&watcher.Device.ID,
&watcher.Device.APNSToken,
&watcher.Device.Sandbox,
&watcher.Account.ID,
2022-03-12 17:50:05 +00:00
&watcher.Account.AccountID,
2021-10-10 15:51:42 +00:00
&watcher.Account.AccessToken,
&watcher.Account.RefreshToken,
&subredditLabel,
&userLabel,
2021-09-25 16:56:01 +00:00
); err != nil {
return nil, err
}
switch watcher.Type {
case domain.SubredditWatcher, domain.TrendingWatcher:
watcher.WatcheeLabel = subredditLabel
case domain.UserWatcher:
watcher.WatcheeLabel = userLabel
}
2021-09-25 16:56:01 +00:00
watchers = append(watchers, watcher)
}
return watchers, nil
}
func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (domain.Watcher, error) {
query := `
2021-10-10 15:51:42 +00:00
SELECT
watchers.id,
watchers.created_at,
watchers.last_notified_at,
watchers.label,
watchers.device_id,
watchers.account_id,
watchers.type,
watchers.watchee_id,
watchers.author,
watchers.subreddit,
2021-10-10 15:51:42 +00:00
watchers.upvotes,
watchers.keyword,
watchers.flair,
watchers.domain,
watchers.hits,
devices.id,
devices.apns_token,
devices.sandbox,
accounts.id,
2022-03-28 21:27:07 +00:00
accounts.reddit_account_id,
2021-10-10 15:51:42 +00:00
accounts.access_token,
accounts.refresh_token,
2022-03-01 22:29:43 +00:00
COALESCE(subreddits.name, '') AS subreddit_label,
COALESCE(users.name, '') AS user_label
2021-09-25 16:56:01 +00:00
FROM watchers
2021-10-10 15:51:42 +00:00
INNER JOIN devices ON watchers.device_id = devices.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
2021-10-10 15:51:42 +00:00
WHERE watchers.id = $1`
2021-09-25 16:56:01 +00:00
watchers, err := p.fetch(ctx, query, id)
if err != nil {
return domain.Watcher{}, err
}
if len(watchers) == 0 {
return domain.Watcher{}, domain.ErrNotFound
}
return watchers[0], nil
}
2021-10-09 14:59:20 +00:00
func (p *postgresWatcherRepository) GetByTypeAndWatcheeID(ctx context.Context, typ domain.WatcherType, id int64) ([]domain.Watcher, error) {
2021-09-25 16:56:01 +00:00
query := `
2021-10-10 15:51:42 +00:00
SELECT
watchers.id,
watchers.created_at,
watchers.last_notified_at,
watchers.label,
watchers.device_id,
watchers.account_id,
watchers.type,
watchers.watchee_id,
watchers.author,
watchers.subreddit,
2021-10-10 15:51:42 +00:00
watchers.upvotes,
watchers.keyword,
watchers.flair,
watchers.domain,
watchers.hits,
devices.id,
devices.apns_token,
devices.sandbox,
accounts.id,
2022-03-28 21:27:07 +00:00
accounts.reddit_account_id,
2021-10-10 15:51:42 +00:00
accounts.access_token,
2022-03-01 22:25:26 +00:00
accounts.refresh_token,
2022-03-01 22:29:43 +00:00
COALESCE(subreddits.name, '') AS subreddit_label,
COALESCE(users.name, '') AS user_label
2021-09-25 16:56:01 +00:00
FROM watchers
2021-10-10 15:51:42 +00:00
INNER JOIN devices ON watchers.device_id = devices.id
INNER JOIN accounts ON watchers.account_id = accounts.id
2022-03-12 17:50:05 +00:00
INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id AND accounts.id = devices_accounts.account_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 AND
devices_accounts.watcher_notifiable = TRUE AND
devices_accounts.global_mute = FALSE`
2021-10-09 14:59:20 +00:00
return p.fetch(ctx, query, typ, id)
}
2021-10-10 15:51:42 +00:00
func (p *postgresWatcherRepository) GetByTrendingSubredditID(ctx context.Context, id int64) ([]domain.Watcher, error) {
return p.GetByTypeAndWatcheeID(ctx, domain.TrendingWatcher, id)
}
2021-10-09 14:59:20 +00:00
func (p *postgresWatcherRepository) GetBySubredditID(ctx context.Context, id int64) ([]domain.Watcher, error) {
return p.GetByTypeAndWatcheeID(ctx, domain.SubredditWatcher, id)
}
2021-09-25 16:56:01 +00:00
2021-10-09 14:59:20 +00:00
func (p *postgresWatcherRepository) GetByUserID(ctx context.Context, id int64) ([]domain.Watcher, error) {
return p.GetByTypeAndWatcheeID(ctx, domain.UserWatcher, id)
2021-09-25 16:56:01 +00:00
}
2021-09-25 18:17:23 +00:00
func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx context.Context, apns string, rid string) ([]domain.Watcher, error) {
query := `
SELECT
watchers.id,
watchers.created_at,
2021-10-10 15:51:42 +00:00
watchers.last_notified_at,
watchers.label,
2021-09-25 18:17:23 +00:00
watchers.device_id,
watchers.account_id,
2021-10-09 14:59:20 +00:00
watchers.type,
watchers.watchee_id,
2021-10-10 15:51:42 +00:00
watchers.author,
watchers.subreddit,
2021-09-25 18:17:23 +00:00
watchers.upvotes,
watchers.keyword,
watchers.flair,
2021-09-25 18:27:58 +00:00
watchers.domain,
2021-10-10 15:51:42 +00:00
watchers.hits,
devices.id,
devices.apns_token,
devices.sandbox,
accounts.id,
2022-03-28 21:27:07 +00:00
accounts.reddit_account_id,
2021-10-10 15:51:42 +00:00
accounts.access_token,
2022-03-01 22:25:26 +00:00
accounts.refresh_token,
2022-03-01 22:29:43 +00:00
COALESCE(subreddits.name, '') AS subreddit_label,
COALESCE(users.name, '') AS user_label
2021-09-25 18:17:23 +00:00
FROM watchers
INNER JOIN accounts ON watchers.account_id = accounts.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
2021-09-25 18:17:23 +00:00
WHERE
devices.apns_token = $1 AND
2022-03-28 21:27:07 +00:00
accounts.reddit_account_id = $2`
2021-09-25 18:17:23 +00:00
return p.fetch(ctx, query, apns, rid)
}
2021-09-25 16:56:01 +00:00
func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.Watcher) error {
2021-10-14 04:25:29 +00:00
if err := watcher.Validate(); err != nil {
return err
}
2022-05-09 01:02:59 +00:00
now := time.Now()
2021-09-25 16:56:01 +00:00
query := `
INSERT INTO watchers
(created_at, last_notified_at, label, device_id, account_id, type, watchee_id, author, subreddit, upvotes, keyword, flair, domain)
2022-05-09 01:02:59 +00:00
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
2021-09-25 16:56:01 +00:00
RETURNING id`
return p.conn.QueryRow(
2021-09-25 16:56:01 +00:00
ctx,
query,
2022-05-09 01:02:59 +00:00
now,
now,
2021-10-10 15:51:42 +00:00
watcher.Label,
2021-09-25 18:02:00 +00:00
watcher.DeviceID,
watcher.AccountID,
2021-10-09 14:59:20 +00:00
watcher.Type,
watcher.WatcheeID,
2021-10-10 15:51:42 +00:00
watcher.Author,
watcher.Subreddit,
2021-09-25 18:02:00 +00:00
watcher.Upvotes,
watcher.Keyword,
watcher.Flair,
watcher.Domain,
2021-09-25 16:56:01 +00:00
).Scan(&watcher.ID)
}
func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.Watcher) error {
2021-10-14 04:25:29 +00:00
if err := watcher.Validate(); err != nil {
return err
}
2021-09-25 16:56:01 +00:00
query := `
UPDATE watchers
2022-05-21 14:26:23 +00:00
SET watchee_id = $2,
author = $3,
subreddit = $4,
upvotes = $5,
keyword = $6,
flair = $7,
domain = $8,
label = $9
2021-09-25 16:56:01 +00:00
WHERE id = $1`
_, err := p.conn.Exec(
2021-09-25 16:56:01 +00:00
ctx,
query,
watcher.ID,
2022-05-21 14:26:23 +00:00
watcher.WatcheeID,
2021-10-10 15:51:42 +00:00
watcher.Author,
watcher.Subreddit,
2021-09-25 16:56:01 +00:00
watcher.Upvotes,
watcher.Keyword,
watcher.Flair,
watcher.Domain,
2021-10-10 15:51:42 +00:00
watcher.Label,
2021-09-25 16:56:01 +00:00
)
return err
}
2021-09-25 18:27:58 +00:00
func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error {
query := `UPDATE watchers SET hits = hits + 1, last_notified_at = $2 WHERE id = $1`
_, err := p.conn.Exec(ctx, query, id, time.Now())
2021-09-25 18:27:58 +00:00
return err
}
2021-09-25 16:56:01 +00:00
func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM watchers WHERE id = $1`
_, err := p.conn.Exec(ctx, query, id)
2021-09-25 16:56:01 +00:00
return err
}
func (p *postgresWatcherRepository) DeleteByTypeAndWatcheeID(ctx context.Context, typ domain.WatcherType, id int64) error {
query := `DELETE FROM watchers WHERE type = $1 AND watchee_id = $2`
_, err := p.conn.Exec(ctx, query, typ, id)
return err
}