Populate names when fetching records

This commit is contained in:
Prathamesh Musale 2024-02-22 16:28:27 +05:30
parent 00d821538f
commit 0ac8398979

View File

@ -156,15 +156,21 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) {
// ListRecords - get all records.
func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) {
iter, err := k.Records.Iterate(ctx, nil)
var records []registrytypes.Record
err := k.Records.Walk(ctx, nil, func(key string, value registrytypes.Record) (bool, error) {
if err := k.populateRecordNames(ctx, &value); err != nil {
return true, err
}
records = append(records, value)
return false, nil
})
if err != nil {
return nil, err
}
// TODO: Check if required
// decodeRecordNames(store, &record)
return iter.Values()
return records, nil
}
// GetRecordById - gets a record from the store.
@ -174,17 +180,35 @@ func (k Keeper) GetRecordById(ctx sdk.Context, id string) (registrytypes.Record,
return registrytypes.Record{}, err
}
if err := k.populateRecordNames(ctx, &record); 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)
var records []registrytypes.Record
err := k.Records.Indexes.BondId.Walk(ctx, collections.NewPrefixedPairRange[string, string](bondId), func(bondId string, id string) (bool, error) {
record, err := k.Records.Get(ctx, id)
if err != nil {
return true, err
}
if err := k.populateRecordNames(ctx, &record); err != nil {
return true, err
}
records = append(records, record)
return false, nil
})
if err != nil {
return []registrytypes.Record{}, err
}
return indexes.CollectValues(ctx, k.Records, iter)
return records, nil
}
// RecordsFromAttributes gets a list of records whose attributes match all provided values
@ -350,6 +374,21 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
return nil
}
func (k Keeper) populateRecordNames(ctx sdk.Context, record *registrytypes.Record) error {
iter, err := k.NameRecords.Indexes.Cid.MatchExact(ctx, record.Id)
if err != nil {
return err
}
names, err := iter.PrimaryKeys()
if err != nil {
return err
}
record.Names = names
return nil
}
// GetModuleBalances gets the registry module account(s) balances.
func (k Keeper) GetModuleBalances(ctx sdk.Context) []*registrytypes.AccountBalance {
var balances []*registrytypes.AccountBalance