/
service.go
179 lines (145 loc) · 5.23 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 currency
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
CurrencyRepo repository.CurrencyRepository
UserRepo repository.UserRepository
EventProducer event.Producer
log pwlogger.Interface
}
// NewServiceImpl -.
func NewServiceImpl(currencyRepo repository.CurrencyRepository, userRepo repository.UserRepository, transactionRepo domain.ITransactionRepo, eventProducer event.Producer, l pwlogger.Interface) *ServiceImpl {
return &ServiceImpl{CurrencyRepo: currencyRepo, UserRepo: userRepo, TransactionRepo: transactionRepo, EventProducer: eventProducer, log: l}
}
// CreateCurrency creates a currency.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) CreateCurrency(ctx context.Context, currencyInfo *CreatedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-createCurrency")
defer span.End()
// Validate input.
err := currencyInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Create currency.
e := currencyInfo.ToEntity()
createdCurrency, err := u.CurrencyRepo.Create(ctx, e)
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Currency.
createdCurrencyEntity, ok := createdCurrency.(*entity.Currency)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(createdCurrencyEntity), nil
}
// GetCurrencyList gets currency list.
func (u *ServiceImpl) GetCurrencyList(ctx context.Context, currencyInfo *ListGotInput) (*OutputList, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-getCurrencyList")
defer span.End()
// Validate input.
err := currencyInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
sq := currencyInfo.ToSearchQuery()
// Get currency list.
list, err := u.CurrencyRepo.GetAll(ctx, sq, &entity.Currency{})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
return NewListOutput(list), nil
}
// GetCurrencyDetail gets currency detail.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) GetCurrencyDetail(ctx context.Context, currencyInfo *DetailGotInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-getCurrencyDetail")
defer span.End()
// Validate input.
err := currencyInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Get currency detail.
currency, err := u.CurrencyRepo.GetByID(ctx, &entity.Currency{ID: currencyInfo.ID})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Currency.
currencyEntity, ok := currency.(*entity.Currency)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(currencyEntity), nil
}
// UpdateCurrency updates currency.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) UpdateCurrency(ctx context.Context, currencyInfo *UpdatedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-updateCurrency")
defer span.End()
// Validate input.
err := currencyInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Update currency.
e := currencyInfo.ToEntity()
updatedCurrency, err := u.CurrencyRepo.Update(ctx, e)
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Currency.
updatedCurrencyEntity, ok := updatedCurrency.(*entity.Currency)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(updatedCurrencyEntity), nil
}
// DeleteCurrency deletes currency.
//
//nolint:dupl // business logic is different
func (u *ServiceImpl) DeleteCurrency(ctx context.Context, currencyInfo *DeletedInput) (*Output, error) {
// 開始追蹤
var tracer = otel.Tracer(domainerrors.GruopID)
ctx, span := tracer.Start(ctx, "usecase-deleteCurrency")
defer span.End()
// Validate input.
err := currencyInfo.Validate()
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeValidateInput, err, span)
}
// Delete currency.
info, err := u.CurrencyRepo.Delete(ctx, &entity.Currency{ID: currencyInfo.ID})
if err != nil {
return nil, domainerrors.WrapWithSpan(ErrorCodeRepository, err, span)
}
// Cast to entity.Currency.
currencyEntity, ok := info.(*entity.Currency)
if !ok {
return nil, domainerrors.WrapWithSpan(ErrorCodeCast, err, span)
}
return NewOutput(currencyEntity), nil
}