apollo-backend/internal/domain/subreddit.go

53 lines
1.1 KiB
Go
Raw Normal View History

2021-09-25 16:56:01 +00:00
package domain
import (
"context"
2022-05-07 15:21:01 +00:00
"errors"
"regexp"
"strings"
2022-03-28 21:05:01 +00:00
"time"
2021-10-12 14:18:40 +00:00
validation "github.com/go-ozzo/ozzo-validation/v4"
2021-09-25 16:56:01 +00:00
)
2022-03-28 21:05:01 +00:00
const SubredditCheckInterval = 2 * time.Minute
2021-09-25 16:56:01 +00:00
type Subreddit struct {
2022-03-28 21:05:01 +00:00
ID int64
NextCheckAt time.Time
2021-09-25 16:56:01 +00:00
// Reddit information
SubredditID string
Name string
}
func (sr *Subreddit) NormalizedName() string {
return strings.ToLower(sr.Name)
2021-09-25 16:56:01 +00:00
}
2022-05-07 15:21:01 +00:00
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")
}
2021-10-12 14:18:40 +00:00
func (sr *Subreddit) Validate() error {
return validation.ValidateStruct(sr,
2022-05-07 15:21:01 +00:00
validation.Field(&sr.Name, validation.Required, validation.Length(3, 32), validation.By(validPrefix), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9]\w{1,19}$`))),
2021-10-14 03:50:34 +00:00
validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
2021-10-12 14:18:40 +00:00
)
}
2021-09-25 16:56:01 +00:00
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
}