apollo-backend/internal/repository/postgres_watcher.go

267 lines
6.6 KiB
Go
Raw Normal View History

2021-09-25 16:56:01 +00:00
package repository
import (
"context"
"fmt"
2021-09-25 18:02:00 +00:00
"time"
2021-09-25 16:56:01 +00:00
"github.com/jackc/pgx/v4/pgxpool"
"github.com/christianselig/apollo-backend/internal/domain"
)
type postgresWatcherRepository struct {
pool *pgxpool.Pool
}
func NewPostgresWatcher(pool *pgxpool.Pool) domain.WatcherRepository {
return &postgresWatcherRepository{pool: pool}
}
func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Watcher, error) {
rows, err := p.pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var watchers []domain.Watcher
for rows.Next() {
var watcher domain.Watcher
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,
&watcher.Account.AccessToken,
&watcher.Account.RefreshToken,
2021-09-25 16:56:01 +00:00
); err != nil {
return nil, err
}
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,
accounts.access_token,
accounts.refresh_token
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
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,
accounts.access_token,
accounts.refresh_token
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
WHERE watchers.type = $1 AND watchers.watchee_id = $2`
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,
accounts.access_token,
accounts.refresh_token
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
WHERE
devices.apns_token = $1 AND
accounts.account_id = $2`
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-09-25 18:02:00 +00:00
now := float64(time.Now().UTC().Unix())
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)
VALUES ($1, 0, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
2021-09-25 16:56:01 +00:00
RETURNING id`
return p.pool.QueryRow(
ctx,
query,
2021-09-25 18:02:00 +00:00
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 {
query := `
UPDATE watchers
2021-10-10 15:51:42 +00:00
SET author = $2,
subreddit = $3,
upvotes = $4,
keyword = $5,
flair = $6,
domain = $7,
label = $8
2021-09-25 16:56:01 +00:00
WHERE id = $1`
res, err := p.pool.Exec(
ctx,
query,
watcher.ID,
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
)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
2021-09-25 18:27:58 +00:00
func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error {
now := time.Now().Unix()
query := `UPDATE watchers SET hits = hits + 1, last_notified_at = $2 WHERE id = $1`
res, err := p.pool.Exec(ctx, query, id, now)
2021-09-25 18:27:58 +00:00
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
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`
res, err := p.pool.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
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`
res, err := p.pool.Exec(ctx, query, typ, id)
if res.RowsAffected() == 0 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}