Skip to content

Commit

Permalink
my review
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegelloz committed Jun 16, 2022
1 parent 6986f0b commit a3e0e05
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 34 deletions.
20 changes: 6 additions & 14 deletions pkg/api/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,20 @@ func (ctl *AccountController) GetAccounts(c *gin.Context) {
balance := c.Query("balance")
if balance != "" {
if _, err := strconv.ParseInt(balance, 10, 64); err != nil {
ResponseError(
c,
ledger.ValidationError{
Msg: "invalid parameter 'balance', should be a number"})
ResponseError(c, ledger.NewValidationError(
"invalid parameter 'balance', should be a number"))
return
}
}

var ok bool
var balanceOperator storage.BalanceOperator

var balanceOperator = storage.DefaultBalanceOperator
if balanceOperatorStr := c.Query("balance_operator"); balanceOperatorStr != "" {
var ok bool
if balanceOperator, ok = storage.NewBalanceOperator(balanceOperatorStr); !ok {
ResponseError(
c,
ledger.ValidationError{
Msg: "invalid parameter 'balance_operator', should be one of 'e, gt, gte, lt, lte'"})
ResponseError(c, ledger.NewValidationError(
"invalid parameter 'balance_operator', should be one of 'e, gt, gte, lt, lte'"))
return
}
} else {
// default value for balanceOperator
balanceOperator = storage.BalanceOperatorGte
}

cursor, err = l.(*ledger.Ledger).GetAccounts(c.Request.Context(),
Expand Down
11 changes: 0 additions & 11 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ func TestGetAccounts(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, rsp.Result().StatusCode, rsp.Body.String())
})

t.Run("address", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"address": []string{"b.b"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("filter by balance >= 50 with default operator", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"balance": []string{"50"},
Expand Down
4 changes: 2 additions & 2 deletions pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ func (l *Ledger) RevertTransaction(ctx context.Context, id uint64) (*core.Transa
return &result.GeneratedTransactions[0], nil
}

func (l *Ledger) CountAccounts(ctx context.Context, m ...storage.AccModifier) (uint64, error) {
func (l *Ledger) CountAccounts(ctx context.Context, m ...storage.AccQueryModifier) (uint64, error) {
q := storage.NewAccountsQuery(m)
return l.store.CountAccounts(ctx, q)
}

func (l *Ledger) GetAccounts(ctx context.Context, m ...storage.AccModifier) (sharedapi.Cursor[core.Account], error) {
func (l *Ledger) GetAccounts(ctx context.Context, m ...storage.AccQueryModifier) (sharedapi.Cursor[core.Account], error) {
q := storage.NewAccountsQuery(m)
return l.store.GetAccounts(ctx, q)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/storage/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type AccountsQuery struct {
Params map[string]interface{}
}

type AccModifier func(*AccountsQuery)
type AccQueryModifier func(*AccountsQuery)

type BalanceOperator string

Expand All @@ -17,6 +17,8 @@ const (
BalanceOperatorGte BalanceOperator = "gte"
BalanceOperatorLt BalanceOperator = "lt"
BalanceOperatorLte BalanceOperator = "lte"

DefaultBalanceOperator = BalanceOperatorGte
)

func (b BalanceOperator) IsValid() bool {
Expand Down Expand Up @@ -52,7 +54,7 @@ func SetBalanceOperatorFilter(v BalanceOperator) func(*AccountsQuery) {
}
}

func NewAccountsQuery(qms ...[]AccModifier) AccountsQuery {
func NewAccountsQuery(qms ...[]AccQueryModifier) AccountsQuery {
q := AccountsQuery{
Limit: QueryDefaultLimit,
Params: map[string]interface{}{},
Expand All @@ -65,7 +67,7 @@ func NewAccountsQuery(qms ...[]AccModifier) AccountsQuery {
return q
}

func (q *AccountsQuery) Apply(modifiers []AccModifier) {
func (q *AccountsQuery) Apply(modifiers []AccQueryModifier) {
for _, m := range modifiers {
m(q)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type Code string

const (
QueryDefaultLimit = 15

ConstraintFailed Code = "CONSTRAINT_FAILED"
TooManyClient Code = "TOO_MANY_CLIENT"
Unknown Code = "UNKNOWN"
Expand Down
4 changes: 0 additions & 4 deletions pkg/storage/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"time"
)

const (
QueryDefaultLimit = 15
)

type TransactionsQuery struct {
Limit uint
AfterTxID uint64
Expand Down

0 comments on commit a3e0e05

Please sign in to comment.