-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
144 lines (123 loc) · 3.82 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package payments
import (
"context"
"log"
"github.com/michaljemala/payments-sample/pkg/internal/errors"
"github.com/michaljemala/payments-sample/pkg/domain"
"github.com/michaljemala/payments-sample/pkg/internal/service"
"github.com/michaljemala/payments-sample/pkg/internal/store"
)
type defaultPaymentService struct {
*service.Generic
paymentStore paymentStore
enumStore enumStore
logger *log.Logger
}
type (
paymentStore interface {
Count(store.Tx, domain.PaymentSearchRequest) (uint, error)
Find(store.Tx, domain.PaymentSearchRequest) ([]*domain.Payment, error)
Get(store.Tx, domain.ID) (*domain.Payment, error)
Insert(store.Tx, *domain.Payment) error
Delete(store.Tx, domain.ID) error
Update(store.Tx, *domain.Payment) error
}
enumStore interface {
Exists(tx store.Tx, name domain.EnumName, code string) (bool, error)
}
)
func newPaymentService(txManager store.TxManager, paymentStore paymentStore, enumStore enumStore, logger *log.Logger) paymentService {
return &defaultPaymentService{
Generic: &service.Generic{TxManager: txManager},
paymentStore: paymentStore,
enumStore: enumStore,
logger: logger,
}
}
func (s *defaultPaymentService) Search(ctx context.Context, searchReq domain.PaymentSearchRequest) (*domain.PaymentSearchResponse, error) {
searchResp := new(domain.PaymentSearchResponse)
err := s.WithTransaction(ctx, func(tx store.Tx) error {
var err error
searchResp.Data, err = s.paymentStore.Find(tx, searchReq)
if err != nil {
return err
}
if searchReq.SearchPagination != nil {
searchResp.Size, err = s.paymentStore.Count(tx, searchReq)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
return searchResp, nil
}
func (s *defaultPaymentService) Load(ctx context.Context, id domain.ID) (payment *domain.Payment, err error) {
err = s.WithTransaction(ctx, func(tx store.Tx) error {
payment, err = s.paymentStore.Get(tx, id)
return err
})
return payment, err
}
func (s *defaultPaymentService) Create(ctx context.Context, payment *domain.Payment) error {
return s.WithTransaction(ctx, func(tx store.Tx) error {
err := s.validatePayment(tx, payment)
if err != nil {
return err
}
return s.paymentStore.Insert(tx, payment)
})
}
func (s *defaultPaymentService) Delete(ctx context.Context, id domain.ID) error {
return s.WithTransaction(ctx, func(tx store.Tx) error {
return s.paymentStore.Delete(tx, id)
})
}
func (s *defaultPaymentService) Update(ctx context.Context, payment *domain.Payment) error {
return s.WithTransaction(ctx, func(tx store.Tx) error {
err := s.validatePayment(tx, payment)
if err != nil {
return err
}
return s.paymentStore.Update(tx, payment)
})
}
func (s *defaultPaymentService) validatePayment(tx store.Tx, payment *domain.Payment) error {
if payment == nil {
return errors.Generic(errors.ErrCodeGenericInvalidArgument, "payment must not be nil", "")
}
v := &paymentValidator{s.enumStore}
err := v.enumExists(tx, enumNameCurrency, payment.Amount.Currency, "payment.amount.currency")
if err != nil {
return err
}
err = v.enumExists(tx, enumNameCountry, payment.Creditor.Address.CountryCode, "payment.creditor.address.country_code")
if err != nil {
return err
}
err = v.enumExists(tx, enumNameCountry, payment.Debtor.Address.CountryCode, "payment.debtor.address.country_code")
if err != nil {
return err
}
err = v.enumExists(tx, enumNameScheme, payment.Scheme, "payment.scheme")
if err != nil {
return err
}
return nil
}
type paymentValidator struct {
enumStore enumStore
}
func (v *paymentValidator) enumExists(tx store.Tx, name domain.EnumName, code, field string) error {
ok, err := v.enumStore.Exists(tx, name, code)
if err != nil {
return err
}
if !ok {
return errors.Generic(errors.ErrCodeGenericInvalidArgument, "enum not found", field)
}
return nil
}