apollo-backend/internal/domain/watcher.go

98 lines
2.1 KiB
Go
Raw Permalink Normal View History

2021-09-25 16:56:01 +00:00
package domain
2021-10-14 04:25:29 +00:00
import (
"context"
2022-05-07 17:22:06 +00:00
"strings"
2022-03-28 21:05:01 +00:00
"time"
2021-10-14 04:25:29 +00:00
validation "github.com/go-ozzo/ozzo-validation/v4"
)
2021-09-25 16:56:01 +00:00
2021-10-09 14:59:20 +00:00
type WatcherType int64
const (
SubredditWatcher WatcherType = iota
UserWatcher
2021-10-10 15:51:42 +00:00
TrendingWatcher
2021-10-09 14:59:20 +00:00
)
2021-10-10 15:51:42 +00:00
func (wt WatcherType) String() string {
switch wt {
case SubredditWatcher:
return "subreddit"
case UserWatcher:
return "user"
case TrendingWatcher:
return "trending"
}
return "unknown"
}
2021-09-25 16:56:01 +00:00
type Watcher struct {
ID int64
2022-03-28 21:05:01 +00:00
CreatedAt time.Time
LastNotifiedAt time.Time
2021-10-10 15:51:42 +00:00
Label string
2021-09-25 16:56:01 +00:00
DeviceID int64
AccountID int64
Type WatcherType
WatcheeID int64
WatcheeLabel string
2021-09-25 16:56:01 +00:00
Author string
Subreddit string
Upvotes int64
Keyword string
Flair string
Domain string
Hits int64
2021-10-10 15:51:42 +00:00
// Related models
Device Device
Account Account
2021-09-25 16:56:01 +00:00
}
2022-05-07 17:22:06 +00:00
func (w *Watcher) KeywordMatches(haystack string) bool {
if w.Keyword == "" {
return true
}
keywords := strings.FieldsFunc(w.Keyword, func(r rune) bool {
return r == '+' || r == ','
})
haystack = strings.ToLower(haystack)
for _, keyword := range keywords {
if !strings.Contains(haystack, keyword) {
return false
}
}
return true
}
2021-10-14 04:25:29 +00:00
func (w *Watcher) Validate() error {
return validation.ValidateStruct(w,
validation.Field(&w.Label, validation.Required, validation.Length(1, 64)),
2021-10-14 04:26:06 +00:00
validation.Field(&w.Type, validation.In(SubredditWatcher, UserWatcher, TrendingWatcher)),
validation.Field(&w.WatcheeID, validation.Required),
2021-10-14 04:25:29 +00:00
)
}
2021-09-25 16:56:01 +00:00
type WatcherRepository interface {
GetByID(ctx context.Context, id int64) (Watcher, error)
GetBySubredditID(ctx context.Context, id int64) ([]Watcher, error)
2021-10-09 14:59:20 +00:00
GetByUserID(ctx context.Context, id int64) ([]Watcher, error)
2021-10-10 15:51:42 +00:00
GetByTrendingSubredditID(ctx context.Context, id int64) ([]Watcher, error)
2021-09-25 18:17:23 +00:00
GetByDeviceAPNSTokenAndAccountRedditID(ctx context.Context, apns string, rid string) ([]Watcher, error)
2021-09-25 16:56:01 +00:00
Create(ctx context.Context, watcher *Watcher) error
Update(ctx context.Context, watcher *Watcher) error
2021-09-25 18:27:58 +00:00
IncrementHits(ctx context.Context, id int64) error
2021-09-25 16:56:01 +00:00
Delete(ctx context.Context, id int64) error
DeleteByTypeAndWatcheeID(context.Context, WatcherType, int64) error
2021-09-25 16:56:01 +00:00
}