Skip to content

Commit

Permalink
feat: add ability to filter all balances not equal to specified value (
Browse files Browse the repository at this point in the history
  • Loading branch information
jdupas22 authored Jan 3, 2023
1 parent 5b30cb3 commit cd87660
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
Empty file modified go.mod
100644 → 100755
Empty file.
Empty file modified go.sum
100644 → 100755
Empty file.
13 changes: 13 additions & 0 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ func TestGetAccounts(t *testing.T) {
assert.Equal(t, cursor.Data[0].Address, "bob")
})

// test filter by balance != 100
t.Run("filter by balance != 100", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"balance": []string{"100"},
"balance_operator": []string{"ne"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
assert.Len(t, cursor.Data, 2)
assert.Equal(t, cursor.Data[0].Address, "world")
assert.Equal(t, cursor.Data[1].Address, "alice")
})

t.Run("invalid balance", func(t *testing.T) {
rsp := internal.GetAccounts(api, url.Values{
"balance": []string{"toto"},
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/controllers/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ paths:
description: Operator used for the filtering of balances can be greater than/equal, less than/equal, greater than, less than, or equal
schema:
type: string
enum: [gte, lte, gt, lt, e]
enum: [gte, lte, gt, lt, e, ne]
example: gte
- name: pagination_token
in: query
Expand Down
2 changes: 2 additions & 0 deletions pkg/ledger/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ const (
BalanceOperatorGte BalanceOperator = "gte"
BalanceOperatorLt BalanceOperator = "lt"
BalanceOperatorLte BalanceOperator = "lte"
BalanceOperatorNe BalanceOperator = "ne"

DefaultBalanceOperator = BalanceOperatorGte
)
Expand All @@ -153,6 +154,7 @@ func (b BalanceOperator) IsValid() bool {
BalanceOperatorGt,
BalanceOperatorGte,
BalanceOperatorLt,
BalanceOperatorNe,
BalanceOperatorLte:
return true
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/sqlstorage/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (s *Store) buildAccountsQuery(p ledger.AccountsQuery) (*sqlbuilder.SelectBu
sb.Where(sb.GreaterThan(balanceOperation, balanceValue))
case ledger.BalanceOperatorE:
sb.Where(sb.Equal(balanceOperation, balanceValue))
case ledger.BalanceOperatorNe:
sb.Where(sb.NotEqual(balanceOperation, balanceValue))
default:
// parameter is validated in the controller for now
panic("invalid balance_operator parameter")
Expand Down

0 comments on commit cd87660

Please sign in to comment.