Add a command to get records by bond id

This commit is contained in:
Prathamesh Musale 2024-02-16 12:18:43 +05:30
parent 5e68c7d9b3
commit 91a8b56183
4 changed files with 35 additions and 17 deletions

View File

@ -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")

View File

@ -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 &registrytypes.QueryRecordsByBondIdResponse{Records: records}, nil
}

View File

@ -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")

View File

@ -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{