Populate names when fetching records
This commit is contained in:
parent
00d821538f
commit
0ac8398979
@ -156,15 +156,21 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) {
|
|||||||
|
|
||||||
// ListRecords - get all records.
|
// ListRecords - get all records.
|
||||||
func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check if required
|
return records, nil
|
||||||
// decodeRecordNames(store, &record)
|
|
||||||
|
|
||||||
return iter.Values()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecordById - gets a record from the store.
|
// 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
|
return registrytypes.Record{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := k.populateRecordNames(ctx, &record); err != nil {
|
||||||
|
return registrytypes.Record{}, err
|
||||||
|
}
|
||||||
|
|
||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRecordsByBondId - gets a record from the store.
|
// GetRecordsByBondId - gets a record from the store.
|
||||||
func (k Keeper) GetRecordsByBondId(ctx sdk.Context, bondId string) ([]registrytypes.Record, error) {
|
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 {
|
if err != nil {
|
||||||
return []registrytypes.Record{}, err
|
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
|
// 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
|
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.
|
// GetModuleBalances gets the registry module account(s) balances.
|
||||||
func (k Keeper) GetModuleBalances(ctx sdk.Context) []*registrytypes.AccountBalance {
|
func (k Keeper) GetModuleBalances(ctx sdk.Context) []*registrytypes.AccountBalance {
|
||||||
var balances []*registrytypes.AccountBalance
|
var balances []*registrytypes.AccountBalance
|
||||||
|
Loading…
Reference in New Issue
Block a user