This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
150 lines (135 loc) · 4.12 KB
/
handlers.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
package main
import "C"
import (
"context"
pb "qiwi/protobuf"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (s *Server) CreateOrUpdateAccount(ctx context.Context, in *pb.CreateOrUpdateAccountRequest) (*pb.CreateOrUpdateAccountResponse, error) {
a := Account{Token: in.Token}
a, err := a.CreateOrUpdate()
if err != nil {
return nil, err
}
return &pb.CreateOrUpdateAccountResponse{
ContractID: a.ContractID,
OperationLimit: a.OperationLimit,
MaxAllowableBalance: a.MaxAllowableBalance,
OperationLimitPerMonth: a.OperationLimitPerMonth,
Balance: a.Balance,
Blocked: a.Blocked},
nil
}
func (s *Server) ListAccounts(ctx context.Context, in *pb.ListAccountsRequest) (*pb.ListAccountsResponse, error) {
return &pb.ListAccountsResponse{
ContractIDs: Account{}.ListString()},
nil
}
func (s *Server) GetAccountBalances(ctx context.Context, in *pb.GetAccountBalancesRequest) (*pb.GetAccountBalancesResponse, error) {
b, err := Account{ContractID: in.ContractID}.GetBalance()
if err != nil {
return nil, err
}
return &pb.GetAccountBalancesResponse{
Balance: b},
nil
}
func (s *Server) DepositCreate(ctx context.Context, in *pb.DepositCreateRequest) (*pb.DepositCreateResponse, error) {
deposit, err := Deposit{Amount: in.Amount}.Create()
var amounts []int64
var links []string
var comments []string
var contractIDs []string
for _, d := range deposit.Transactions {
amounts = append(amounts, d.Amount)
links = append(links, d.Link)
comments = append(comments, d.Comment)
contractIDs = append(contractIDs, d.ContractID)
}
if err != nil {
return nil, err
}
return &pb.DepositCreateResponse{
Id: deposit.ID.Hex(),
ContractIDs: contractIDs,
Amounts: amounts,
Links: links,
Comments: comments},
nil
}
func (s *Server) DepositClose(ctx context.Context, in *pb.DepositCloseRequest) (*pb.DepositCloseResponse, error) {
objID, err := primitive.ObjectIDFromHex(in.Id)
if err != nil {
return nil, err //todo readable
}
deposit, err := Deposit{ID: objID}.Close()
if err != nil {
return nil, err
}
return &pb.DepositCloseResponse{
Id: deposit.ID.Hex(),
Status: deposit.Status,
},
nil
}
func (s *Server) DepositCheck(ctx context.Context, in *pb.DepositCheckRequest) (*pb.DepositCheckResponse, error) {
objID, err := primitive.ObjectIDFromHex(in.Id)
if err != nil {
return nil, err //todo readable
}
deposit, err := Deposit{ID: objID}.Check()
if err != nil {
return nil, err
}
var amounts []int64
var links []string
var comments []string
var contractIDs []string
var statuses []bool
for _, d := range deposit.Transactions {
amounts = append(amounts, d.Amount)
links = append(links, d.Link)
comments = append(comments, d.Comment)
contractIDs = append(contractIDs, d.ContractID)
statuses = append(statuses, d.Status)
}
if err != nil {
return nil, err
}
return &pb.DepositCheckResponse{
Id: deposit.ID.Hex(),
Status: deposit.Status,
ContractIDs: contractIDs,
Amounts: amounts,
Links: links,
Comments: comments,
Statuses: statuses},
nil
}
func (s *Server) WithdrawalCreate(ctx context.Context, in *pb.WithdrawalCreateRequest) (*pb.WithdrawalCreateResponse, error) {
withdrawal, err := Withdrawal{Amount: in.Amount, ReceiverContractID: in.ContractID, Comment: in.Comment}.Create()
var amounts []int64
var comments []string
var receiverContractIDs []string
var senderContractIDs []string
var statuses []string
for _, tr := range withdrawal.Transactions {
amounts = append(amounts, tr.Amount)
comments = append(comments, tr.Comment)
receiverContractIDs = append(receiverContractIDs, tr.ReceiverContractID)
senderContractIDs = append(senderContractIDs, tr.SenderContractID)
statuses = append(statuses, tr.Status)
}
if err != nil {
return nil, err
}
return &pb.WithdrawalCreateResponse{
Id: withdrawal.ID.Hex(),
Status: withdrawal.Status,
ReceiverContractIDs: receiverContractIDs,
SenderContractIDs: senderContractIDs,
Amounts: amounts,
Statuses: statuses,
Comments: comments},
nil
}