laconicd/x/registry/keeper/query_server.go
Prathamesh Musale df43322ef3 Add pagination for query to get records (#58)
Part of [Create a public laconicd testnet](https://www.notion.so/Create-a-public-laconicd-testnet-896a11bdd8094eff8f1b49c0be0ca3b8)
Handles cerc-io/laconic-console#59

Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Reviewed-on: cerc-io/laconicd#58
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-09-05 07:33:48 +00:00

178 lines
4.9 KiB
Go

package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
registrytypes "git.vdb.to/cerc-io/laconicd/x/registry"
)
var _ registrytypes.QueryServer = queryServer{}
type queryServer struct {
k Keeper
}
// NewQueryServerImpl returns an implementation of the module QueryServer.
func NewQueryServerImpl(k Keeper) registrytypes.QueryServer {
return queryServer{k}
}
func (qs queryServer) Params(c context.Context, _ *registrytypes.QueryParamsRequest) (*registrytypes.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params, err := qs.k.GetParams(ctx)
if err != nil {
return nil, err
}
return &registrytypes.QueryParamsResponse{Params: params}, nil
}
func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecordsRequest) (*registrytypes.QueryRecordsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
attributes := req.GetAttributes()
all := req.GetAll()
var records []registrytypes.Record
var pageResp *query.PageResponse
var err error
if len(attributes) > 0 {
records, pageResp, err = qs.k.PaginatedRecordsFromAttributes(ctx, attributes, all, req.Pagination)
if err != nil {
return nil, err
}
} else {
records, pageResp, err = qs.k.PaginatedListRecords(ctx, req.Pagination)
if err != nil {
return nil, err
}
}
return &registrytypes.QueryRecordsResponse{Records: records, Pagination: pageResp}, nil
}
func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryGetRecordRequest) (*registrytypes.QueryGetRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
id := req.GetId()
if has, err := qs.k.HasRecord(ctx, req.Id); !has {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
}
record, err := qs.k.GetRecordById(ctx, id)
if err != nil {
return nil, err
}
return &registrytypes.QueryGetRecordResponse{Record: record}, nil
}
func (qs queryServer) GetRecordsByBondId(
c context.Context,
req *registrytypes.QueryGetRecordsByBondIdRequest,
) (*registrytypes.QueryGetRecordsByBondIdResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
records, err := qs.k.GetRecordsByBondId(ctx, req.GetId())
if err != nil {
return nil, err
}
return &registrytypes.QueryGetRecordsByBondIdResponse{Records: records}, nil
}
func (qs queryServer) GetRegistryModuleBalance(c context.Context,
_ *registrytypes.QueryGetRegistryModuleBalanceRequest,
) (*registrytypes.QueryGetRegistryModuleBalanceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
balances := qs.k.GetModuleBalances(ctx)
return &registrytypes.QueryGetRegistryModuleBalanceResponse{
Balances: balances,
}, nil
}
func (qs queryServer) NameRecords(c context.Context, _ *registrytypes.QueryNameRecordsRequest) (*registrytypes.QueryNameRecordsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nameRecords, err := qs.k.ListNameRecords(ctx)
if err != nil {
return nil, err
}
return &registrytypes.QueryNameRecordsResponse{Names: nameRecords}, nil
}
func (qs queryServer) Whois(c context.Context, req *registrytypes.QueryWhoisRequest) (*registrytypes.QueryWhoisResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nameAuthority, err := qs.k.GetNameAuthority(ctx, req.GetName())
if err != nil {
return nil, err
}
return &registrytypes.QueryWhoisResponse{NameAuthority: nameAuthority}, nil
}
func (qs queryServer) Authorities(c context.Context, req *registrytypes.QueryAuthoritiesRequest) (*registrytypes.QueryAuthoritiesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
authorityEntries, err := qs.k.ListNameAuthorityRecords(ctx, req.GetOwner())
if err != nil {
return nil, err
}
return &registrytypes.QueryAuthoritiesResponse{Authorities: authorityEntries}, nil
}
func (qs queryServer) LookupLrn(c context.Context, req *registrytypes.QueryLookupLrnRequest) (*registrytypes.QueryLookupLrnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
lrn := req.GetLrn()
lrnExists, err := qs.k.HasNameRecord(ctx, lrn)
if err != nil {
return nil, err
}
if !lrnExists {
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "LRN not found.")
}
nameRecord, err := qs.k.LookupNameRecord(ctx, lrn)
if nameRecord == nil {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
}
return &registrytypes.QueryLookupLrnResponse{Name: nameRecord}, nil
}
func (qs queryServer) ResolveLrn(c context.Context, req *registrytypes.QueryResolveLrnRequest) (*registrytypes.QueryResolveLrnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
lrn := req.GetLrn()
record, err := qs.k.ResolveLRN(ctx, lrn)
if record == nil {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
}
return &registrytypes.QueryResolveLrnResponse{Record: record}, nil
}