Skip to content

Get escrow transactions with filter fields and pagination #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func startGrpcServer(
Service: service.NewAccountLedgerService(queryExecutor),
})

// Set GRPC handler for escrow transaction request
rpcService.RegisterEscrowTransactionServiceServer(grpcServer, &handler.EscrowTransactionHandler{
Service: service.NewEscrowTransactionService(queryExecutor),
})

// run grpc-gateway handler
go func() {
if err := grpcServer.Serve(serv); err != nil {
Expand Down Expand Up @@ -219,5 +224,6 @@ func runProxy(apiPort, rpcPort int) error {
_ = rpcService.RegisterNodeAdminServiceHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", rpcPort), opts)
_ = rpcService.RegisterTransactionServiceHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", rpcPort), opts)
_ = rpcService.RegisterAccountLedgerServiceHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", rpcPort), opts)
_ = rpcService.RegisterEscrowTransactionServiceHandlerFromEndpoint(ctx, mux, fmt.Sprintf("localhost:%d", rpcPort), opts)
return http.ListenAndServe(fmt.Sprintf(":%d", apiPort), mux)
}
48 changes: 48 additions & 0 deletions api/client/GetEscrowTransactions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"fmt"
"log"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
rpcModel "github.com/zoobc/zoobc-core/common/model"
rpcService "github.com/zoobc/zoobc-core/common/service"
"github.com/zoobc/zoobc-core/common/util"
"google.golang.org/grpc"
)

func main() {
var apiRPCPort int
if err := util.LoadConfig("../../../resource", "config", "toml"); err != nil {
logrus.Fatal(err)
} else {
apiRPCPort = viper.GetInt("apiRPCPort")
if apiRPCPort == 0 {
apiRPCPort = 8080
}
}

conn, err := grpc.Dial(fmt.Sprintf(":%d", apiRPCPort), grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %s", err)
}
defer conn.Close()

c := rpcService.NewEscrowTransactionServiceClient(conn)

response, err := c.GetEscrowTransactions(context.Background(), &rpcModel.GetEscrowTransactionsRequest{
ApproverAddress: "BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE",
Statuses: []rpcModel.EscrowStatus{
rpcModel.EscrowStatus_Approved,
},
})

if err != nil {
log.Fatalf("error calling : %s", err)
}

log.Printf("response from remote : %s", response)

}
19 changes: 9 additions & 10 deletions api/client/PostTransaction/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ func main() {
response, err := c.PostTransaction(context.Background(), &rpc_model.PostTransactionRequest{

// Escrow
TransactionBytes: []byte{1, 0, 0, 0, 1, 127, 39, 65, 94, 0, 0, 0, 0, 44, 0, 0, 0, 72, 108, 90, 76, 104, 51, 86, 99, 110, 78, 108,
TransactionBytes: []byte{1, 0, 0, 0, 1, 114, 186, 68, 94, 0, 0, 0, 0, 44, 0, 0, 0, 72, 108, 90, 76, 104, 51, 86, 99, 110, 78, 108,
118, 66, 121, 87, 111, 65, 122, 88, 79, 81, 50, 106, 65, 108, 119, 70, 79, 105, 121, 79, 57, 95, 110, 106, 73, 51, 111,
113, 53, 89, 103, 104, 97, 44, 0, 0, 0, 110, 75, 95, 111, 117, 120, 100, 68, 68, 119, 117, 74, 105, 111, 103, 105, 68,
65, 105, 95, 122, 115, 49, 76, 113, 101, 78, 55, 102, 53, 90, 115, 88, 98, 70, 116, 88, 71, 113, 71, 99, 48, 80, 100,
1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 87, 4, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 66, 67, 90, 69, 71, 79, 98, 51, 87, 78, 120,
51, 102, 68, 79, 86, 102, 57, 90, 83, 52, 69, 106, 118, 79, 73, 118, 95, 85, 101, 87, 52, 84, 86, 66, 81, 74, 95, 54,
116, 72, 75, 108, 69, 111, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 97, 109, 111, 117, 110, 116, 32,
109, 117, 115, 116, 32, 109, 111, 114, 101, 32, 116, 104, 97, 110, 32, 49, 0, 0, 0, 0, 226, 248, 232, 158, 251, 254,
219, 157, 51, 35, 97, 142, 94, 94, 175, 166, 112, 37, 82, 98, 50, 1, 243, 56, 217, 221, 196, 41, 163, 145, 136, 80, 35,
207, 23, 116, 143, 154, 148, 168, 163, 71, 78, 191, 150, 112, 51, 59, 165, 144, 38, 182, 193, 238, 8, 124, 204, 218, 0,
151, 202, 242, 103, 0},
113, 53, 89, 103, 104, 97, 44, 0, 0, 0, 110, 75, 95, 111, 117, 120, 100, 68, 68, 119, 117, 74, 105, 111, 103, 105, 68, 65,
105, 95, 122, 115, 49, 76, 113, 101, 78, 55, 102, 53, 90, 115, 88, 98, 70, 116, 88, 71, 113, 71, 99, 48, 80, 100, 1, 0, 0,
0, 0, 0, 0, 0, 8, 0, 0, 0, 87, 4, 0, 0, 0, 0, 0, 0, 44, 0, 0, 0, 66, 67, 90, 69, 71, 79, 98, 51, 87, 78, 120, 51, 102, 68,
79, 86, 102, 57, 90, 83, 52, 69, 106, 118, 79, 73, 118, 95, 85, 101, 87, 52, 84, 86, 66, 81, 74, 95, 54, 116, 72, 75, 108,
69, 111, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 97, 109, 111, 117, 110, 116, 32, 109, 117, 115, 116, 32,
109, 111, 114, 101, 32, 116, 104, 97, 110, 32, 49, 0, 0, 0, 0, 178, 66, 128, 131, 168, 110, 230, 13, 235, 177, 8, 220, 123,
148, 159, 170, 237, 219, 168, 84, 207, 106, 112, 50, 2, 39, 139, 246, 51, 100, 142, 98, 198, 14, 196, 147, 248, 167, 20,
150, 114, 204, 47, 56, 215, 165, 36, 81, 178, 159, 224, 190, 147, 117, 103, 120, 246, 25, 24, 79, 142, 209, 39, 2},
// Approval
// TransactionBytes: []byte{4, 0, 0, 0, 1, 162, 32, 65, 94, 0, 0, 0, 0, 44, 0, 0, 0, 66, 67, 90, 69, 71, 79, 98, 51, 87, 78, 120, 51, 102,
// 68, 79, 86, 102, 57, 90, 83, 52, 69, 106, 118, 79, 73, 118, 95, 85, 101, 87, 52, 84, 86, 66, 81, 74, 95, 54, 116, 72, 75, 108,
Expand Down
21 changes: 21 additions & 0 deletions api/handler/escrowTransactionHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handler

import (
"context"

"github.com/zoobc/zoobc-core/api/service"
"github.com/zoobc/zoobc-core/common/model"
)

// EscrowTransactionHandler to handle request related to Escrow Transaction grpc handler from client
type EscrowTransactionHandler struct {
Service service.EscrowTransactionServiceInterface
}

// GetEscrowTransactions get escrow transactions with filter fields params
func (eh *EscrowTransactionHandler) GetEscrowTransactions(
_ context.Context,
req *model.GetEscrowTransactionsRequest,
) (*model.GetEscrowTransactionsResponse, error) {
return eh.Service.GetEscrowTransactions(req)
}
110 changes: 110 additions & 0 deletions api/service/escrowTransactionApiService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package service

import (
"bytes"
"database/sql"

"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type (
// EscrowTransactionServiceInterface interface that contain methods of escrow transaction
EscrowTransactionServiceInterface interface {
GetEscrowTransactions(request *model.GetEscrowTransactionsRequest) (*model.GetEscrowTransactionsResponse, error)
}
// EscrowTransactionService struct that contain fields that needed
escrowTransactionService struct {
Query query.ExecutorInterface
}
)

// NewEscrowTransactionService will create EscrowTransactionServiceInterface instance
func NewEscrowTransactionService(
query query.ExecutorInterface,
) EscrowTransactionServiceInterface {
return &escrowTransactionService{
Query: query,
}
}

// GetEscrowTransactions to get escrow transactions list
func (es *escrowTransactionService) GetEscrowTransactions(
params *model.GetEscrowTransactionsRequest,
) (*model.GetEscrowTransactionsResponse, error) {
var (
escrowQuery = query.NewEscrowTransactionQuery()
countQuery string
escrows []*model.Escrow
rows *sql.Rows
count int64
row *sql.Row
err error
)

caseQuery := query.CaseQuery{
Query: bytes.NewBuffer([]byte{}),
}

caseQuery.Select(escrowQuery.TableName, escrowQuery.Fields...)
if params.GetApproverAddress() != "" {
caseQuery.Where(caseQuery.Equal("approver_address", params.GetApproverAddress()))
}
if len(params.GetStatuses()) > 0 {
var statuses []interface{}
for _, v := range params.GetStatuses() {
statuses = append(statuses, int32(v))
}
caseQuery.And(caseQuery.In("status", statuses...))
}
if params.GetID() > 0 {
caseQuery.And(caseQuery.Equal("id", params.GetID()))
}

blockHeightStart := params.GetBlockHeightStart()
blockHeightEnd := params.GetBlockHeightEnd()
if blockHeightStart > 0 {
caseQuery.Where(caseQuery.Between("block_height", blockHeightStart, blockHeightEnd))
}

// count first
selectQuery, args := caseQuery.Build()
countQuery = query.GetTotalRecordOfSelect(selectQuery)

row, err = es.Query.ExecuteSelectRow(countQuery, false, args...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
err = row.Scan(&count)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

// select records
page := params.GetPagination()
if page.GetOrderField() == "" {
caseQuery.OrderBy("id", page.GetOrderBy())
} else {
caseQuery.OrderBy(page.GetOrderField(), page.GetOrderBy())
}
caseQuery.Paginate(page.GetLimit(), page.GetPage())

escrowQ, escrowArgs := caseQuery.Build()
rows, err = es.Query.ExecuteSelect(escrowQ, false, escrowArgs...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
defer rows.Close()

escrows, err = escrowQuery.BuildModels(rows)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &model.GetEscrowTransactionsResponse{
Total: uint64(count),
Escrows: escrows,
}, nil
}
125 changes: 125 additions & 0 deletions api/service/escrowTransactionApiService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package service

import (
"database/sql"
"reflect"
"regexp"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
)

type (
mockQueryExecutorGetEscrowTransactionsError struct {
query.ExecutorInterface
}
mockQueryExecutorGetEscrowTransactionsSuccess struct {
query.ExecutorInterface
}
)

func (*mockQueryExecutorGetEscrowTransactionsError) ExecuteSelectRow(qStr string, tx bool, args ...interface{}) (*sql.Row, error) {
return nil, sql.ErrNoRows
}
func (*mockQueryExecutorGetEscrowTransactionsError) ExecuteSelect(qStr string, tx bool, args ...interface{}) (*sql.Rows, error) {
return nil, sql.ErrNoRows
}

func (*mockQueryExecutorGetEscrowTransactionsSuccess) ExecuteSelect(qStr string, tx bool, args ...interface{}) (*sql.Rows, error) {
dbMocked, mock, _ := sqlmock.New()
mockedRows := mock.NewRows(query.NewEscrowTransactionQuery().Fields)
mockedRows.AddRow(
int64(1),
"BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
"BCZD_VxfO2S9aziIL3cn_cXW7uPDVPOrnXuP98GEAUC7",
"BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J",
int64(10),
int64(1),
uint64(120),
model.EscrowStatus_Approved,
uint32(0),
true,
"",
)

mock.ExpectQuery(regexp.QuoteMeta(qStr)).WillReturnRows(mockedRows)
return dbMocked.Query(qStr)

}

func (*mockQueryExecutorGetEscrowTransactionsSuccess) ExecuteSelectRow(qStr string, tx bool, args ...interface{}) (*sql.Row, error) {
db, mock, _ := sqlmock.New()
mockedRow := mock.NewRows([]string{"count"})
mockedRow.AddRow(1)
mock.ExpectQuery("").WillReturnRows(mockedRow)
row := db.QueryRow("")
return row, nil
}

func TestEscrowTransactionService_GetEscrowTransactions(t *testing.T) {
type fields struct {
Query query.ExecutorInterface
}
type args struct {
params *model.GetEscrowTransactionsRequest
}
tests := []struct {
name string
fields fields
args args
want *model.GetEscrowTransactionsResponse
wantErr bool
}{
{
name: "WantError",
fields: fields{
Query: &mockQueryExecutorGetEscrowTransactionsError{},
},
wantErr: true,
},
{
name: "wantSuccess",
fields: fields{
Query: &mockQueryExecutorGetEscrowTransactionsSuccess{},
},
args: args{
params: &model.GetEscrowTransactionsRequest{
ApproverAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
},
},
want: &model.GetEscrowTransactionsResponse{
Total: 1,
Escrows: []*model.Escrow{
{
ID: 1,
SenderAddress: "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
RecipientAddress: "BCZD_VxfO2S9aziIL3cn_cXW7uPDVPOrnXuP98GEAUC7",
ApproverAddress: "BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J",
Amount: 10,
Commission: 1,
Timeout: 120,
Status: model.EscrowStatus_Approved,
Latest: true,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &escrowTransactionService{
Query: tt.fields.Query,
}
got, err := es.GetEscrowTransactions(tt.args.params)
if (err != nil) != tt.wantErr {
t.Errorf("GetEscrowTransactions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetEscrowTransactions() got = %v, want %v", got, tt.want)
}
})
}
}
Loading