forked from mzrausyanfikri/autonotif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
71 lines (58 loc) · 1.66 KB
/
builder.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
package autonotif
import (
"context"
"errors"
"github.com/aimzeter/autonotif/config"
"github.com/aimzeter/autonotif/entity"
"github.com/aimzeter/autonotif/internal/datasource"
"github.com/aimzeter/autonotif/internal/repository"
"github.com/aimzeter/autonotif/internal/target"
)
type ProposalStore interface {
Set(ctx context.Context, p *entity.Proposal) error
GetLastID(ctx context.Context, chainType string) (int, error)
RevokeLastID(ctx context.Context, chainType string, lastID int) error
}
type DatasourceAPI interface {
GetProposalDetail(ctx context.Context, p *entity.Proposal) (*entity.Proposal, error)
}
type Notifier interface {
SendMessage(ctx context.Context, p *entity.Proposal) error
}
type Dependencies struct {
conf *config.Config
dsAPI DatasourceAPI
dsStore ProposalStore
notifier Notifier
}
func BuildDependencies(conf *config.Config) (*Dependencies, error) {
dsAPI := datasource.NewDatasource()
dsStore, storeErr := BuildRepository(conf.Repository)
if storeErr != nil {
return nil, storeErr
}
notifier, notifErr := target.NewTelegram(conf.Target.Telegram.Token, conf.Target.Telegram.ChannelID)
if notifErr != nil {
return nil, notifErr
}
return &Dependencies{
conf: conf,
dsAPI: dsAPI,
dsStore: dsStore,
notifier: notifier,
}, nil
}
func BuildAutonotif(d *Dependencies) *Autonotif {
return &Autonotif{
d: d,
}
}
func BuildRepository(cfg config.Repository) (ProposalStore, error) {
switch {
case cfg.Localfile.Enable:
return repository.NewProposalLocalfile(cfg.Localfile.Dir)
case cfg.Postgresql.Enable:
return repository.NewProposalPSQL(cfg.Postgresql)
}
return nil, errors.New("no repository enabled")
}