-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com>
- Loading branch information
1 parent
bc85579
commit 3a17910
Showing
2 changed files
with
101 additions
and
0 deletions.
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,44 @@ | ||
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" | ||
) | ||
|
||
// Projects queries all projects from a given credit class. | ||
func (k Keeper) Projects(ctx context.Context, request *v1beta1.QueryProjectsRequest) (*v1beta1.QueryProjectsResponse, error) { | ||
pg, err := GogoPageReqToPulsarPageReq(request.Pagination) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cInfo, err := k.stateStore.ClassInfoStore().GetByName(ctx, request.ClassId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
it, err := k.stateStore.ProjectInfoStore().List(ctx, ecocreditv1beta1.ProjectInfoClassIdNameIndexKey{}.WithClassId(cInfo.Id), ormlist.Paginate(pg)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
projectInfos := make([]*v1beta1.ProjectInfo, 0) | ||
for it.Next() { | ||
info, err := it.Value() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var pi v1beta1.ProjectInfo | ||
if err = PulsarToGogoSlow(info, &pi); err != nil { | ||
return nil, err | ||
} | ||
projectInfos = append(projectInfos, &pi) | ||
} | ||
pr, err := PulsarPageResToGogoPageRes(it.PageResponse()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &v1beta1.QueryProjectsResponse{ | ||
Projects: projectInfos, | ||
Pagination: pr, | ||
}, nil | ||
} |
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,57 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors" | ||
"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_Projects(t *testing.T) { | ||
t.Parallel() | ||
s := setupBase(t) | ||
|
||
// create a class and 2 projects from it | ||
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.ProjectInfoStore().Insert(s.ctx, &ecocreditv1beta1.ProjectInfo{ | ||
Name: "P01", | ||
ClassId: 1, | ||
ProjectLocation: "US-CA", | ||
Metadata: nil, | ||
}) | ||
assert.NilError(t, err) | ||
err= s.stateStore.ProjectInfoStore().Insert(s.ctx, &ecocreditv1beta1.ProjectInfo{ | ||
Name: "P02", | ||
ClassId: 1, | ||
ProjectLocation: "US-CA", | ||
Metadata: nil, | ||
}) | ||
assert.NilError(t, err) | ||
|
||
// base query | ||
res, err := s.k.Projects(s.ctx, &v1beta1.QueryProjectsRequest{ClassId: "C01"}) | ||
assert.NilError(t, err) | ||
assert.Equal(t, 2, len(res.Projects)) | ||
assert.Equal(t, "US-CA", res.Projects[0].ProjectLocation) | ||
|
||
// invalid query | ||
_, err = s.k.Projects(s.ctx, &v1beta1.QueryProjectsRequest{ClassId: "F01"}) | ||
assert.ErrorContains(t, err, ormerrors.NotFound.Error()) | ||
|
||
// paginated query | ||
res, err = s.k.Projects(s.ctx, &v1beta1.QueryProjectsRequest{ | ||
ClassId: "C01", | ||
Pagination: &query.PageRequest{Limit: 1, CountTotal: true}, | ||
}) | ||
assert.NilError(t, err) | ||
assert.Equal(t, 1, len(res.Projects)) | ||
assert.Equal(t, uint64(2), res.Pagination.Total) | ||
} |