/
grpc_query_writer.go
75 lines (60 loc) · 2.46 KB
/
grpc_query_writer.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
72
73
74
75
package keeper
import (
"context"
"github.com/medibloc/panacea-core/v2/types/compkey"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/medibloc/panacea-core/v2/x/aol/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) Writer(c context.Context, req *types.QueryWriterRequest) (*types.QueryWriterResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
ownerAddr, err := sdk.AccAddressFromBech32(req.OwnerAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid owner address")
}
writerAddr, err := sdk.AccAddressFromBech32(req.WriterAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid writer address")
}
writerKey := types.WriterCompositeKey{OwnerAddress: ownerAddr, TopicName: req.TopicName, WriterAddress: writerAddr}
if !k.HasWriter(ctx, writerKey) {
return nil, status.Error(codes.NotFound, "writer not found")
}
writer := k.GetWriter(ctx, writerKey)
return &types.QueryWriterResponse{Writer: &writer}, nil
}
func (k Keeper) Writers(c context.Context, req *types.QueryWritersRequest) (*types.QueryWritersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var writerAddresses []string
ctx := sdk.UnwrapSDKContext(c)
ownerAddr, err := sdk.AccAddressFromBech32(req.OwnerAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid owner address")
}
compKeyPrefix, err := compkey.PartialEncode(&types.WriterCompositeKey{OwnerAddress: ownerAddr, TopicName: req.TopicName, WriterAddress: nil}, 2)
if err != nil {
return nil, status.Error(codes.Internal, "failed to writer key")
}
store := ctx.KVStore(k.storeKey)
writerStore := prefix.NewStore(store, append(types.WriterKeyPrefix, compKeyPrefix...))
pageRes, err := query.Paginate(writerStore, req.Pagination, func(compKeyLast []byte, value []byte) error {
var compKey types.WriterCompositeKey
if err := compkey.Decode(append(compKeyPrefix, compKeyLast...), &compKey); err != nil {
return err
}
writerAddresses = append(writerAddresses, compKey.WriterAddress.String())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryWritersResponse{WriterAddresses: writerAddresses, Pagination: pageRes}, nil
}