/
query_service.go
56 lines (43 loc) · 1.62 KB
/
query_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package keeper
import (
"context"
"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/pokt-network/poktroll/x/service/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)
// AllServices queries all services.
func (k Keeper) AllServices(ctx context.Context, req *types.QueryAllServicesRequest) (*types.QueryAllServicesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var services []sharedtypes.Service
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
serviceStore := prefix.NewStore(store, types.KeyPrefix(types.ServiceKeyPrefix))
pageRes, err := query.Paginate(serviceStore, req.Pagination, func(key []byte, value []byte) error {
var service sharedtypes.Service
if err := k.cdc.Unmarshal(value, &service); err != nil {
return err
}
services = append(services, service)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryAllServicesResponse{Service: services, Pagination: pageRes}, nil
}
// Service returns the requested service if it exists.
func (k Keeper) Service(ctx context.Context, req *types.QueryGetServiceRequest) (*types.QueryGetServiceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
service, found := k.GetService(ctx, req.Id)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryGetServiceResponse{Service: service}, nil
}