/
service.go
179 lines (145 loc) · 5.08 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package wallet
import (
"context"
"github.com/program-world-labs/pwlogger"
"go.opentelemetry.io/otel"
"github.com/program-world-labs/DDDGo/internal/domain"
"github.com/program-world-labs/DDDGo/internal/domain/domainerrors"
"github.com/program-world-labs/DDDGo/internal/domain/entity"
"github.com/program-world-labs/DDDGo/internal/domain/event"
"github.com/program-world-labs/DDDGo/internal/domain/repository"
)
var _ IService = (*ServiceImpl)(nil)
// ServiceImpl -.
type ServiceImpl struct {
TransactionRepo domain.ITransactionRepo
WalletRepo repository.WalletRepository
UserRepo repository.UserRepository
EventProducer event.Producer
log pwlogger.Interface
}
// NewServiceImpl -.
func NewServiceImpl(walletRepo repository.WalletRepository, userRepo repository.UserRepository, transactionRepo domain.ITransactionRepo, eventProducer event.Producer, l pwlogger.Interface) *ServiceImpl {
return &ServiceImpl{WalletRepo: walletRepo, UserRepo: userRepo, TransactionRepo: transactionRepo, EventProducer: eventProducer, log: l}
}
// CreateWallet creates a wallet.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) CreateWallet(ctx context.Context, walletInfo *CreatedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-createWallet")
defer span.End()
// Validate input.
err := walletInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Create wallet.
e := walletInfo.ToEntity()
createdWallet, err := u.WalletRepo.Create(ctx, e)
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Wallet.
createdWalletEntity, ok := createdWallet.(*entity.Wallet)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(createdWalletEntity), nil
}
// GetWalletList gets wallet list.
func (u *ServiceImpl) GetWalletList(ctx context.Context, walletInfo *ListGotInput) (*OutputList, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-getWalletList")
defer span.End()
// Validate input.
err := walletInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
sq := walletInfo.ToSearchQuery()
// Get wallet list.
list, err := u.WalletRepo.GetAll(ctx, sq, &entity.Wallet{})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
return NewListOutput(list), nil
}
// GetWalletDetail gets wallet detail.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) GetWalletDetail(ctx context.Context, walletInfo *DetailGotInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-getWalletDetail")
defer span.End()
// Validate input.
err := walletInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Get wallet detail.
wallet, err := u.WalletRepo.GetByID(ctx, &entity.Wallet{ID: walletInfo.ID})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Wallet.
walletEntity, ok := wallet.(*entity.Wallet)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(walletEntity), nil
}
// UpdateWallet updates wallet.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) UpdateWallet(ctx context.Context, walletInfo *UpdatedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-updateWallet")
defer span.End()
// Validate input.
err := walletInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Update wallet.
e := walletInfo.ToEntity()
updatedWallet, err := u.WalletRepo.Update(ctx, e)
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Wallet.
updatedWalletEntity, ok := updatedWallet.(*entity.Wallet)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(updatedWalletEntity), nil
}
// DeleteWallet deletes wallet.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) DeleteWallet(ctx context.Context, walletInfo *DeletedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-deleteWallet")
defer span.End()
// Validate input.
err := walletInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Delete wallet.
info, err := u.WalletRepo.Delete(ctx, &entity.Wallet{ID: walletInfo.ID})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Wallet.
walletEntity, ok := info.(*entity.Wallet)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(walletEntity), nil
}