-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.go
77 lines (64 loc) · 1.64 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
70
71
72
73
74
75
76
77
package sql
import (
"fmt"
"github.com/pkg/errors"
"github.com/urandom/readeef/content/repo"
"github.com/urandom/readeef/content/repo/sql/db"
"github.com/urandom/readeef/log"
)
type Service struct {
user repo.User
tag repo.Tag
feed repo.Feed
subscription repo.Subscription
article repo.Article
extract repo.Extract
scores repo.Scores
thumbnail repo.Thumbnail
}
func NewService(driver, source string, log log.Log) (Service, error) {
switch driver {
case "sqlite3", "postgres":
db := db.New(log)
if err := db.Open(driver, source); err != nil {
return Service{}, errors.Wrap(err, "connecting to database")
}
log.Infof("Initializing sql repo with driver %s", driver)
return Service{
user: userRepo{db, log},
tag: tagRepo{db, log},
feed: feedRepo{db, log},
subscription: subscriptionRepo{db, log},
article: articleRepo{db, log},
extract: extractRepo{db, log},
scores: scoresRepo{db, log},
thumbnail: thumbnailRepo{db, log},
}, nil
default:
panic(fmt.Sprintf("Cannot provide a repo for driver '%s'\n", driver))
}
}
func (s Service) UserRepo() repo.User {
return s.user
}
func (s Service) TagRepo() repo.Tag {
return s.tag
}
func (s Service) FeedRepo() repo.Feed {
return s.feed
}
func (s Service) SubscriptionRepo() repo.Subscription {
return s.subscription
}
func (s Service) ArticleRepo() repo.Article {
return s.article
}
func (s Service) ExtractRepo() repo.Extract {
return s.extract
}
func (s Service) ScoresRepo() repo.Scores {
return s.scores
}
func (s Service) ThumbnailRepo() repo.Thumbnail {
return s.thumbnail
}