/
service.go
49 lines (39 loc) · 1.28 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
package service
import (
"context"
"fmt"
"github.com/xescugc/notigator/notification"
"github.com/xescugc/notigator/source"
)
// Service it's the public interface for the Notigator Service
type Service interface {
// GetSources returns a list of all the configured source.Source
GetSources(ctx context.Context) ([]source.Source, error)
// GetSourceNotifications returns all the notifications of the srcID
GetSourceNotifications(ctx context.Context, srcID string) ([]*notification.Notification, error)
}
type service struct {
sources source.Repository
notifications map[string]notification.Repository
}
// New returns an implementation of the Service interface
func New(srcs source.Repository, nots map[string]notification.Repository) Service {
return &service{
sources: srcs,
notifications: nots,
}
}
func (s *service) GetSources(ctx context.Context) ([]source.Source, error) {
return s.sources.Filter(ctx)
}
func (s *service) GetSourceNotifications(ctx context.Context, srcID string) ([]*notification.Notification, error) {
nr, ok := s.notifications[srcID]
if !ok {
return nil, fmt.Errorf("source not defined %q", srcID)
}
nts, err := nr.Filter(ctx)
if err != nil {
return nil, fmt.Errorf("error while Filtering notifications: %s", err)
}
return nts, nil
}