Skip to content

Commit

Permalink
API: Adding files by owner query (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Oct 31, 2023
2 parents 4c745dd + 8da8675 commit bd85760
Show file tree
Hide file tree
Showing 4 changed files with 487 additions and 56 deletions.
10 changes: 10 additions & 0 deletions proto/canine_chain/filetree/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ service Query {
"/jackal-dao/canine-chain/filetree/files/{address}/{ownerAddress}";
}

// Queries a list of Files items.
rpc FilesAllByOwner(QueryAllFilesByOwnerRequest) returns (QueryAllFilesResponse) {
option (google.api.http).get = "/jackal-dao/canine-chain/filetree/files/{owner}";
}

// Queries a list of Files items.
rpc FilesAll(QueryAllFilesRequest) returns (QueryAllFilesResponse) {
option (google.api.http).get = "/jackal-dao/canine-chain/filetree/files";
Expand Down Expand Up @@ -79,6 +84,11 @@ message QueryFileRequest {

message QueryFileResponse { Files files = 1 [ (gogoproto.nullable) = false ]; }

message QueryAllFilesByOwnerRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string owner = 2;
}

message QueryAllFilesRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}
Expand Down
30 changes: 30 additions & 0 deletions x/filetree/keeper/grpc_query_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ func (k Keeper) FilesAll(c context.Context, req *types.QueryAllFilesRequest) (*t
return &types.QueryAllFilesResponse{Files: filess, Pagination: pageRes}, nil
}

func (k Keeper) FilesAllByOwner(c context.Context, req *types.QueryAllFilesByOwnerRequest) (*types.QueryAllFilesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var filess []types.Files
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
filesStore := prefix.NewStore(store, types.KeyPrefix(types.FilesKeyPrefix))

pageRes, err := query.Paginate(filesStore, req.Pagination, func(key []byte, value []byte) error {
var files types.Files
if err := k.cdc.Unmarshal(value, &files); err != nil {
return err
}

if files.Owner == req.Owner {
filess = append(filess, files)
}

return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryAllFilesResponse{Files: filess, Pagination: pageRes}, nil
}

func (k Keeper) Files(c context.Context, req *types.QueryFileRequest) (*types.QueryFileResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand Down
Loading

0 comments on commit bd85760

Please sign in to comment.