Skip to content

Commit

Permalink
Added owner function to blzcli and rest
Browse files Browse the repository at this point in the history
  • Loading branch information
scottburch committed Sep 17, 2020
1 parent c7fcb65 commit 776a5b7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
24 changes: 24 additions & 0 deletions x/crud/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,35 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
GetCmdQCount(storeKey, cdc),
GetCmdQGetLease(storeKey, cdc),
GetCmdQGetNShortestLeases(storeKey, cdc),
GetCmdQOwner(storeKey, cdc),
)...)

return crudQueryCmd
}

func GetCmdQOwner(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "owner [UUID] [key]",
Short: "owner UUID key",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
UUID := args[0]
key := args[1]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/owner/%s/%s", queryRoute, UUID, key), nil)

if err != nil {
fmt.Printf("could get owner for key - %s : %s\n", UUID, key)
return nil
}

var out types.QueryResultOwner
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}

func GetCmdQRead(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "read [UUID] [key]",
Expand Down
15 changes: 15 additions & 0 deletions x/crud/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func BlzQReadHandler(cliCtx context.CLIContext, storeName string) http.HandlerFu
}
}

func BlzQOwnerHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/owner/%s/%s", storeName, vars["UUID"], vars["key"]), nil)

if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)

}
}

func BlzQProvenReadHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
1 change: 1 addition & 0 deletions x/crud/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
r.HandleFunc(fmt.Sprintf("/%s/pread/{UUID}/{key}", storeName), BlzQProvenReadHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/read", storeName), BlzReadHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/read/{UUID}/{key}", storeName), BlzQReadHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/owner/{UUID}/{key}", storeName), BlzQOwnerHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/rename", storeName), BlzRenameHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/update", storeName), BlzUpdateHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/renewlease", storeName), BlzRenewLease(cliCtx)).Methods("POST")
Expand Down
31 changes: 25 additions & 6 deletions x/crud/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ import (
)

const (
QueryRead = "read"
QueryHas = "has"
QueryKeys = "keys"
QueryKeyValues = "keyvalues"
QueryCount = "count"
QueryGetLease = "getlease"
QueryRead = "read"
QueryHas = "has"
QueryKeys = "keys"
QueryKeyValues = "keyvalues"
QueryCount = "count"
QueryGetLease = "getlease"
QueryGetNShortestLeases = "getnshortestleases"
QueryOwner = "owner"
)

func NewQuerier(keeper IKeeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case QueryOwner:
return queryOwner(ctx, path[1:], req, keeper, keeper.GetCdc())
case QueryRead:
return queryRead(ctx, path[1:], req, keeper, keeper.GetCdc())
case QueryHas:
Expand Down Expand Up @@ -71,6 +74,22 @@ func queryRead(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper IKeep
return res, nil
}

func queryOwner(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper IKeeper, cdc *codec.Codec) ([]byte, error) {
owner := keeper.GetOwner(ctx, keeper.GetKVStore(ctx), path[0], path[1])

if owner == nil {
return []byte{}, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "key not found")
}

res, err := codec.MarshalJSONIndent(cdc, types.QueryResultOwner{UUID: path[0], Key: path[1], Owner: owner.String()})
if err != nil {
panic("could not marshal result to JSON")
}

return res, nil

}

func queryHas(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper IKeeper, cdc *codec.Codec) ([]byte, error) {
has := keeper.IsKeyPresent(ctx, keeper.GetKVStore(ctx), path[0], path[1])

Expand Down
6 changes: 6 additions & 0 deletions x/crud/internal/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type QueryResultRead struct {
Value string `json:"value"`
}

type QueryResultOwner struct {
UUID string `json:"uuid"`
Key string `json:"key"`
Owner string `json:"owner"`
}

// for fmt.Stringer
func (r QueryResultRead) String() string {
return r.Value
Expand Down

0 comments on commit 776a5b7

Please sign in to comment.