2021-09-25 16:56:01 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/christianselig/apollo-backend/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
type postgresSubredditRepository struct {
|
2022-05-07 16:37:21 +00:00
|
|
|
conn Connection
|
2021-09-25 16:56:01 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
func NewPostgresSubreddit(conn Connection) domain.SubredditRepository {
|
|
|
|
return &postgresSubredditRepository{conn: conn}
|
2021-09-25 16:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postgresSubredditRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Subreddit, error) {
|
2022-05-07 16:37:21 +00:00
|
|
|
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 srs []domain.Subreddit
|
|
|
|
for rows.Next() {
|
|
|
|
var sr domain.Subreddit
|
|
|
|
if err := rows.Scan(
|
|
|
|
&sr.ID,
|
|
|
|
&sr.SubredditID,
|
|
|
|
&sr.Name,
|
2022-03-28 21:05:01 +00:00
|
|
|
&sr.NextCheckAt,
|
2021-09-25 16:56:01 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srs = append(srs, sr)
|
|
|
|
}
|
|
|
|
return srs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postgresSubredditRepository) GetByID(ctx context.Context, id int64) (domain.Subreddit, error) {
|
|
|
|
query := `
|
2022-03-28 21:05:01 +00:00
|
|
|
SELECT id, subreddit_id, name, next_check_at
|
2021-09-25 16:56:01 +00:00
|
|
|
FROM subreddits
|
|
|
|
WHERE id = $1`
|
|
|
|
|
|
|
|
srs, err := p.fetch(ctx, query, id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return domain.Subreddit{}, err
|
|
|
|
}
|
|
|
|
if len(srs) == 0 {
|
|
|
|
return domain.Subreddit{}, domain.ErrNotFound
|
|
|
|
}
|
|
|
|
return srs[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postgresSubredditRepository) GetByName(ctx context.Context, name string) (domain.Subreddit, error) {
|
|
|
|
query := `
|
2022-03-28 21:05:01 +00:00
|
|
|
SELECT id, subreddit_id, name, next_check_at
|
2021-09-25 16:56:01 +00:00
|
|
|
FROM subreddits
|
|
|
|
WHERE name = $1`
|
|
|
|
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
|
|
|
srs, err := p.fetch(ctx, query, name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return domain.Subreddit{}, err
|
|
|
|
}
|
|
|
|
if len(srs) == 0 {
|
|
|
|
return domain.Subreddit{}, domain.ErrNotFound
|
|
|
|
}
|
|
|
|
return srs[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postgresSubredditRepository) CreateOrUpdate(ctx context.Context, sr *domain.Subreddit) error {
|
2021-10-12 14:18:40 +00:00
|
|
|
if err := sr.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:56:01 +00:00
|
|
|
query := `
|
2022-03-28 21:05:01 +00:00
|
|
|
INSERT INTO subreddits (subreddit_id, name, next_check_at)
|
|
|
|
VALUES ($1, $2, NOW())
|
2021-10-10 15:51:42 +00:00
|
|
|
ON CONFLICT(subreddit_id) DO NOTHING
|
2021-09-25 16:56:01 +00:00
|
|
|
RETURNING id`
|
|
|
|
|
2022-05-07 16:37:21 +00:00
|
|
|
return p.conn.QueryRow(
|
2021-09-25 16:56:01 +00:00
|
|
|
ctx,
|
|
|
|
query,
|
|
|
|
sr.SubredditID,
|
|
|
|
sr.NormalizedName(),
|
|
|
|
).Scan(&sr.ID)
|
|
|
|
}
|