-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
69 lines (52 loc) · 1.17 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package services
import (
"errors"
"math/rand"
"time"
"github.com/adesokanayo/dailywellness/entity"
"github.com/adesokanayo/dailywellness/repository"
)
type service struct{}
var (
repo = repository.NewFireStoreRepository()
)
func NewTipService() postingInterface {
return &service{}
}
type (
postingInterface interface {
Validate(post *entity.Tip) error
Create(post *entity.Tip) (*entity.Tip, error)
FindAll() ([]entity.Tip, error)
FindOne() (*entity.Tip, error)
FindToday() (*entity.Tip, error)
}
)
func (s *service) Validate(post *entity.Tip) error {
if post == nil {
err := errors.New("empty post")
return err
}
if post.Title == "" {
err := errors.New("empty title")
return err
}
return nil
}
func (s *service) Create(post *entity.Tip) (*entity.Tip, error) {
post.ID = rand.Int63()
return repo.Save(post)
}
func (s *service) FindAll() ([]entity.Tip, error) {
return repo.FindAll()
}
func (s *service) FindOne() (*entity.Tip, error) {
rand.Seed(time.Now().UnixNano())
min := 1
max := 100
num := rand.Intn(max-min+1) + min
return repo.FindOne(int64(num))
}
func (s *service) FindToday() (*entity.Tip, error) {
return repo.FindToday()
}