-
Notifications
You must be signed in to change notification settings - Fork 103
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
feat(x/ecocredit): query Classes
ORM
#837
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95d27fa
feat: query classes
technicallyty b57f09c
chore: unecessary nil check
technicallyty 68f5d40
Apply suggestions from code review
technicallyty b3ceda3
Merge branch 'master' into ty/696-query_classes
technicallyty b40549d
Merge branch 'master' into ty/696-query_classes
technicallyty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"github.com/cosmos/cosmos-sdk/orm/model/ormlist" | ||
ecocreditv1beta1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1beta1" | ||
"github.com/regen-network/regen-ledger/x/ecocredit/v1beta1" | ||
) | ||
|
||
// Classes queries for all credit classes with pagination. | ||
func (k Keeper) Classes(ctx context.Context, request *v1beta1.QueryClassesRequest) (*v1beta1.QueryClassesResponse, error) { | ||
pg, err := GogoPageReqToPulsarPageReq(request.Pagination) | ||
if err != nil { | ||
return nil, err | ||
} | ||
it, err := k.stateStore.ClassInfoStore().List(ctx, &ecocreditv1beta1.ClassInfoPrimaryKey{}, ormlist.Paginate(pg)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
infos := make([]*v1beta1.ClassInfo, 0) | ||
for it.Next() { | ||
info, err := it.Value() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var ci v1beta1.ClassInfo | ||
if err = PulsarToGogoSlow(info, &ci); err != nil { | ||
return nil, err | ||
} | ||
infos = append(infos, &ci) | ||
} | ||
pr, err := PulsarPageResToGogoPageRes(it.PageResponse()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &v1beta1.QueryClassesResponse{ | ||
Classes: infos, | ||
Pagination: pr, | ||
}, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
ecocreditv1beta1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1beta1" | ||
"github.com/regen-network/regen-ledger/x/ecocredit/v1beta1" | ||
"gotest.tools/v3/assert" | ||
"testing" | ||
) | ||
|
||
func TestQuery_Classes(t *testing.T) { | ||
t.Parallel() | ||
s := setupBase(t) | ||
err := s.stateStore.ClassInfoStore().Insert(s.ctx, &ecocreditv1beta1.ClassInfo{ | ||
Name: "C01", | ||
Admin: s.addr, | ||
Metadata: nil, | ||
CreditType: "C", | ||
}) | ||
assert.NilError(t, err) | ||
err = s.stateStore.ClassInfoStore().Insert(s.ctx, &ecocreditv1beta1.ClassInfo{ | ||
Name: "C02", | ||
Admin: s.addr, | ||
Metadata: nil, | ||
CreditType: "C", | ||
}) | ||
assert.NilError(t, err) | ||
|
||
// requesting all | ||
res, err := s.k.Classes(s.ctx, &v1beta1.QueryClassesRequest{}) | ||
assert.NilError(t, err) | ||
assert.Equal(t, 2, len(res.Classes)) | ||
assert.Equal(t, "C01", res.Classes[0].Name) | ||
technicallyty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Equal(t, "C02", res.Classes[1].Name) | ||
|
||
// request with pagination | ||
res, err = s.k.Classes(s.ctx, &v1beta1.QueryClassesRequest{Pagination: &query.PageRequest{ | ||
Limit: 1, | ||
CountTotal: true, | ||
Reverse: true, | ||
}}) | ||
assert.NilError(t, err) | ||
assert.Equal(t, 1, len(res.Classes)) | ||
assert.Equal(t, uint64(2), res.Pagination.Total) | ||
technicallyty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.Equal(t, "C02", res.Classes[0].Name) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relavant to this PR alone, but I think we should move
x/ecocredit/server/core/query_utils.go
to global scopetypes
orapp/utils
. It smells generic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah its pretty generic, but i think we can punt it until someone needs it elsewhere