From 91a8b561833080aa243207aca634f70b2aa0eac0 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 16 Feb 2024 12:18:43 +0530 Subject: [PATCH] Add a command to get records by bond id --- x/registry/keeper/keeper.go | 30 ++++++++++++++++++++---------- x/registry/keeper/query_server.go | 9 +++++++-- x/registry/keeper/record_keeper.go | 5 ----- x/registry/module/autocli.go | 8 ++++++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index 34ec0e3e..20eef6c7 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -106,16 +106,6 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) { return has, nil } -// GetRecord - gets a record from the store. -func (k Keeper) GetRecord(ctx sdk.Context, id string) (registrytypes.Record, error) { - record, err := k.Records.Get(ctx, id) - if err != nil { - return registrytypes.Record{}, err - } - - return record, nil -} - // ListRecords - get all records. func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) { iter, err := k.Records.Iterate(ctx, nil) @@ -129,6 +119,26 @@ func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) { return iter.Values() } +// GetRecordById - gets a record from the store. +func (k Keeper) GetRecordById(ctx sdk.Context, id string) (registrytypes.Record, error) { + record, err := k.Records.Get(ctx, id) + if err != nil { + return registrytypes.Record{}, err + } + + return record, nil +} + +// GetRecordsByBondId - gets a record from the store. +func (k Keeper) GetRecordsByBondId(ctx sdk.Context, bondId string) ([]registrytypes.Record, error) { + iter, err := k.Records.Indexes.BondId.MatchExact(ctx, bondId) + if err != nil { + return []registrytypes.Record{}, err + } + + return indexes.CollectValues(ctx, k.Records, iter) +} + // RecordsFromAttributes gets a list of records whose attributes match all provided values func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*registrytypes.QueryRecordsRequest_KeyValueInput, all bool) ([]registrytypes.Record, error) { panic("unimplemented") diff --git a/x/registry/keeper/query_server.go b/x/registry/keeper/query_server.go index 32c6ce2d..bada886a 100644 --- a/x/registry/keeper/query_server.go +++ b/x/registry/keeper/query_server.go @@ -67,7 +67,7 @@ func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryRecor return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.") } - record, err := qs.k.GetRecord(ctx, id) + record, err := qs.k.GetRecordById(ctx, id) if err != nil { return nil, err } @@ -77,7 +77,12 @@ func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryRecor func (qs queryServer) GetRecordsByBondId(c context.Context, req *registrytypes.QueryRecordsByBondIdRequest) (*registrytypes.QueryRecordsByBondIdResponse, error) { ctx := sdk.UnwrapSDKContext(c) - records := qs.k.recordKeeper.QueryRecordsByBond(ctx, req.GetId()) + + records, err := qs.k.GetRecordsByBondId(ctx, req.GetId()) + if err != nil { + return nil, err + } + return ®istrytypes.QueryRecordsByBondIdResponse{Records: records}, nil } diff --git a/x/registry/keeper/record_keeper.go b/x/registry/keeper/record_keeper.go index 9dcc8cb5..27cadb8f 100644 --- a/x/registry/keeper/record_keeper.go +++ b/x/registry/keeper/record_keeper.go @@ -17,11 +17,6 @@ type RecordKeeper struct { // storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context } -// QueryRecordsByBond - get all records for the given bond. -func (k RecordKeeper) QueryRecordsByBond(ctx sdk.Context, bondID string) []registrytypes.Record { - panic("unimplemented") -} - // ProcessRenewRecord renews a record. func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg registrytypes.MsgRenewRecord) error { panic("unimplemented") diff --git a/x/registry/module/autocli.go b/x/registry/module/autocli.go index 5c420bfd..ba60a563 100644 --- a/x/registry/module/autocli.go +++ b/x/registry/module/autocli.go @@ -35,6 +35,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { {ProtoField: "id"}, }, }, + { + RpcMethod: "GetRecordsByBondId", + Use: "get-records-by-bond-id [bond-id]", + Short: "Get records by bond id", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "id"}, + }, + }, }, }, Tx: &autocliv1.ServiceCommandDescriptor{