-
Notifications
You must be signed in to change notification settings - Fork 168
/
list_stores.go
71 lines (56 loc) · 1.78 KB
/
list_stores.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 commands
import (
"context"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
"github.com/openfga/openfga/pkg/encoder"
"github.com/openfga/openfga/pkg/logger"
serverErrors "github.com/openfga/openfga/pkg/server/errors"
"github.com/openfga/openfga/pkg/storage"
)
type ListStoresQuery struct {
storesBackend storage.StoresBackend
logger logger.Logger
encoder encoder.Encoder
}
type ListStoresQueryOption func(*ListStoresQuery)
func WithListStoresQueryLogger(l logger.Logger) ListStoresQueryOption {
return func(q *ListStoresQuery) {
q.logger = l
}
}
func WithListStoresQueryEncoder(e encoder.Encoder) ListStoresQueryOption {
return func(q *ListStoresQuery) {
q.encoder = e
}
}
func NewListStoresQuery(storesBackend storage.StoresBackend, opts ...ListStoresQueryOption) *ListStoresQuery {
q := &ListStoresQuery{
storesBackend: storesBackend,
logger: logger.NewNoopLogger(),
encoder: encoder.NewBase64Encoder(),
}
for _, opt := range opts {
opt(q)
}
return q
}
func (q *ListStoresQuery) Execute(ctx context.Context, req *openfgav1.ListStoresRequest) (*openfgav1.ListStoresResponse, error) {
decodedContToken, err := q.encoder.Decode(req.GetContinuationToken())
if err != nil {
return nil, serverErrors.InvalidContinuationToken
}
paginationOptions := storage.NewPaginationOptions(req.GetPageSize().GetValue(), string(decodedContToken))
stores, continuationToken, err := q.storesBackend.ListStores(ctx, paginationOptions)
if err != nil {
return nil, serverErrors.HandleError("", err)
}
encodedToken, err := q.encoder.Encode(continuationToken)
if err != nil {
return nil, serverErrors.HandleError("", err)
}
resp := &openfgav1.ListStoresResponse{
Stores: stores,
ContinuationToken: encodedToken,
}
return resp, nil
}