-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.go
42 lines (33 loc) · 1.03 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
package fetching
import (
"context"
gopher "github.com/friendsofgo/gopherapi/pkg"
"github.com/friendsofgo/gopherapi/pkg/log"
)
// Service provides fetching operations.
type Service interface {
FetchGophers(ctx context.Context) ([]gopher.Gopher, error)
FetchGopherByID(ctx context.Context, ID string) *gopher.Gopher
}
type service struct {
repository gopher.Repository
logger log.Logger
}
// NewService creates a fetching service with the necessary dependencies
func NewService(repository gopher.Repository, logger log.Logger) Service {
return &service{repository, logger}
}
// FetchGophers returns all gophers
func (s *service) FetchGophers(ctx context.Context) ([]gopher.Gopher, error) {
return s.repository.FetchGophers(ctx)
}
// FetchGopherByID returns a gopher
func (s *service) FetchGopherByID(ctx context.Context, ID string) *gopher.Gopher {
g, err := s.repository.FetchGopherByID(ctx, ID)
// This error can be any error type of our repository
if err != nil {
s.logger.UnexpectedError(ctx, err)
return nil
}
return g
}