mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-26 05:37:42 +00:00
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package domain
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
)
|
|
|
|
const SubredditCheckInterval = 2 * time.Minute
|
|
|
|
type Subreddit struct {
|
|
ID int64
|
|
NextCheckAt time.Time
|
|
|
|
// Reddit information
|
|
SubredditID string
|
|
Name string
|
|
}
|
|
|
|
func (sr *Subreddit) NormalizedName() string {
|
|
return strings.ToLower(sr.Name)
|
|
}
|
|
|
|
func validPrefix(value interface{}) error {
|
|
s, _ := value.(string)
|
|
if len(s) < 2 {
|
|
return nil
|
|
}
|
|
if s[1] != '_' || s[0] != 'u' {
|
|
return nil
|
|
}
|
|
|
|
return errors.New("invalid subreddit format")
|
|
}
|
|
|
|
func (sr *Subreddit) Validate() error {
|
|
return validation.ValidateStruct(sr,
|
|
validation.Field(&sr.Name, validation.Required, validation.Length(2, 21), validation.By(validPrefix), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9]\w*$`))),
|
|
validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
|
|
)
|
|
}
|
|
|
|
type SubredditRepository interface {
|
|
GetByID(ctx context.Context, id int64) (Subreddit, error)
|
|
GetByName(ctx context.Context, name string) (Subreddit, error)
|
|
|
|
CreateOrUpdate(ctx context.Context, sr *Subreddit) error
|
|
}
|