/
api_wallet_service.go
113 lines (94 loc) · 3.49 KB
/
api_wallet_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
/*
* Config OpenAPI
*
* This is the config OpenAPI definition.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package configService
import (
"context"
"errors"
"net/http"
"github.com/CHainGate/backend/internal/model"
"github.com/CHainGate/backend/internal/repository"
"github.com/CHainGate/backend/internal/service"
"github.com/CHainGate/backend/pkg/enum"
"github.com/google/uuid"
"github.com/CHainGate/backend/configApi"
)
// WalletApiService is a service that implements the logic for the WalletApiServicer
// This service should implement the business logic for every endpoint for the WalletApi API.
// Include any external packages or services that will be required by this service.
type WalletApiService struct {
authenticationService service.IAuthenticationService
merchantRepository repository.IMerchantRepository
}
// NewWalletApiService creates a default api service
func NewWalletApiService(
authenticationService service.IAuthenticationService,
merchantRepository repository.IMerchantRepository,
) configApi.WalletApiServicer {
return &WalletApiService{authenticationService, merchantRepository}
}
// AddWallet - add new wallet address
func (s *WalletApiService) AddWallet(_ context.Context, authorization string, walletRequestDto configApi.WalletRequestDto) (configApi.ImplResponse, error) {
merchant, err := s.authenticationService.HandleJwtAuthentication(authorization)
if err != nil {
return configApi.Response(http.StatusForbidden, nil), errors.New("not authorized")
}
mode, ok := enum.ParseStringToModeEnum(walletRequestDto.Mode)
if !ok {
}
currency, ok := enum.ParseStringToCryptoCurrencyEnum(walletRequestDto.Currency)
if !ok {
}
newWallet := model.Wallet{
Base: model.Base{ID: uuid.New()},
Currency: currency,
Address: walletRequestDto.Address,
Mode: mode,
}
merchant.Wallets = append(merchant.Wallets, newWallet)
err = s.merchantRepository.Update(merchant)
if err != nil {
return configApi.Response(http.StatusNotImplemented, nil), err
}
response := configApi.WalletResponseDto{
Id: newWallet.ID.String(),
Address: newWallet.Address,
Mode: newWallet.Mode.String(),
Currency: newWallet.Currency.String(),
}
return configApi.Response(http.StatusCreated, response), nil
}
// DeleteWallet - delete wallet
func (s *WalletApiService) DeleteWallet(ctx context.Context, walletId string, authorization string) (configApi.ImplResponse, error) {
_, err := s.authenticationService.HandleJwtAuthentication(authorization)
if err != nil {
return configApi.Response(http.StatusForbidden, nil), errors.New("not authorized")
}
err = s.merchantRepository.DeleteWalletById(walletId)
if err != nil {
return configApi.Response(http.StatusInternalServerError, nil), err
}
return configApi.Response(http.StatusOK, nil), nil
}
// GetWallets - get wallets
func (s *WalletApiService) GetWallets(ctx context.Context, mode string, authorization string) (configApi.ImplResponse, error) {
merchant, err := s.authenticationService.HandleJwtAuthentication(authorization)
if err != nil {
return configApi.Response(http.StatusForbidden, nil), errors.New("not authorized")
}
result := make([]configApi.WalletResponseDto, 0)
for _, wallet := range merchant.Wallets {
result = append(result, configApi.WalletResponseDto{
Id: wallet.ID.String(),
Mode: wallet.Mode.String(),
Currency: wallet.Currency.String(),
Address: wallet.Address,
})
}
return configApi.Response(http.StatusOK, result), nil
}