/
querier.go
71 lines (61 loc) · 1.9 KB
/
querier.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// query endpoints supported by the nameservice Querier
const (
QueryRxs = "rxs"
QueryRx = "rx"
QueryPermission = "permits"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case QueryRxs:
return queryRxs(ctx, path[1:], req, keeper)
case QueryRx:
return queryRx(ctx, path[1:], req, keeper)
case QueryPermission:
return queryPermits(ctx, path[1:], req, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown patient query endpoint")
}
}
}
func queryRxs(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
value, e := keeper.ap.GetRxs(ctx, path[0])
if e != nil {
return nil, e
}
res, err := codec.MarshalJSONIndent(keeper.cdc, value)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryRx(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
value, e := keeper.ap.GetRx(ctx, path[0], path[1])
if e != nil {
return nil, e
}
res, err := codec.MarshalJSONIndent(keeper.cdc, value)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryPermits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
value, e := keeper.ap.GetRxPermission(ctx, path[0])
if e != nil {
return nil, e
}
res, err := codec.MarshalJSONIndent(keeper.cdc, value)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}