Skip to content

GRPC endpoint get single escrow transaction by ID #620

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
Mar 4, 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
45 changes: 45 additions & 0 deletions api/client/GetEscrowTransaction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.GetEscrowTransaction(context.Background(), &rpcModel.GetEscrowTransactionRequest{
ID: -2747806915165203447,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why there is an id hardcoded here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for test only @iltoga. All api/client only for test.

})

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

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

}
7 changes: 7 additions & 0 deletions api/handler/escrowTransactionHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ func (eh *EscrowTransactionHandler) GetEscrowTransactions(
) (*model.GetEscrowTransactionsResponse, error) {
return eh.Service.GetEscrowTransactions(req)
}

func (eh *EscrowTransactionHandler) GetEscrowTransaction(
_ context.Context,
req *model.GetEscrowTransactionRequest,
) (*model.Escrow, error) {
return eh.Service.GetEscrowTransaction(req)
}
32 changes: 31 additions & 1 deletion api/service/escrowTransactionApiService.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type (
// EscrowTransactionServiceInterface interface that contain methods of escrow transaction
EscrowTransactionServiceInterface interface {
GetEscrowTransactions(request *model.GetEscrowTransactionsRequest) (*model.GetEscrowTransactionsResponse, error)
GetEscrowTransaction(request *model.GetEscrowTransactionRequest) (*model.Escrow, error)
}
// EscrowTransactionService struct that contain fields that needed
escrowTransactionService struct {
Expand Down Expand Up @@ -59,7 +60,7 @@ func (es *escrowTransactionService) GetEscrowTransactions(
}
caseQuery.And(caseQuery.In("status", statuses...))
}
if params.GetID() > 0 {
if params.GetID() != 0 {
caseQuery.And(caseQuery.Equal("id", params.GetID()))
}

Expand Down Expand Up @@ -108,3 +109,32 @@ func (es *escrowTransactionService) GetEscrowTransactions(
Escrows: escrows,
}, nil
}

// GetEscrowTransaction to get escrow by id and status
func (es *escrowTransactionService) GetEscrowTransaction(
request *model.GetEscrowTransactionRequest,
) (*model.Escrow, error) {
var (
escrowQuery = query.NewEscrowTransactionQuery()
escrow model.Escrow
row *sql.Row
err error
)

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

caseQuery.Select(escrowQuery.TableName, escrowQuery.Fields...)
caseQuery.Where(caseQuery.Equal("id", request.GetID()))
caseQuery.Where(caseQuery.Equal("latest", 1))

qStr, qArgs := caseQuery.Build()

row, _ = es.Query.ExecuteSelectRow(qStr, false, qArgs...)
err = escrowQuery.Scan(&escrow, row)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &escrow, nil
}
81 changes: 81 additions & 0 deletions api/service/escrowTransactionApiService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,84 @@ func TestEscrowTransactionService_GetEscrowTransactions(t *testing.T) {
})
}
}

type (
mockExecutorGetEscrow struct {
query.ExecutorInterface
}
)

func (*mockExecutorGetEscrow) ExecuteSelectRow(string, bool, ...interface{}) (*sql.Row, error) {
db, mock, _ := sqlmock.New()
mockedRow := mock.NewRows(query.NewEscrowTransactionQuery().Fields)
mockedRow.AddRow(
int64(1),
"BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN",
"BCZD_VxfO2S9aziIL3cn_cXW7uPDVPOrnXuP98GEAUC7",
"BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J",
int64(10),
int64(1),
uint64(120),
model.EscrowStatus_Approved,
uint32(0),
true,
"",
)
mock.ExpectQuery("").WillReturnRows(mockedRow)
row := db.QueryRow("")
return row, nil
}

func Test_escrowTransactionService_GetEscrowTransaction(t *testing.T) {
type fields struct {
Query query.ExecutorInterface
}
type args struct {
request *model.GetEscrowTransactionRequest
}
tests := []struct {
name string
fields fields
args args
want *model.Escrow
wantErr bool
}{
{
name: "wantSuccess",
fields: fields{
Query: &mockExecutorGetEscrow{},
},
args: args{
request: &model.GetEscrowTransactionRequest{
ID: 918263123,
},
},
want: &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.GetEscrowTransaction(tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("GetEscrowTransaction() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetEscrowTransaction() got = %v, want %v", got, tt.want)
}
})
}
}
108 changes: 75 additions & 33 deletions common/model/escrow.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading