diff --git a/x/crud/client/cli/query.go b/x/crud/client/cli/query.go index c8a7fa63..58a63aba 100644 --- a/x/crud/client/cli/query.go +++ b/x/crud/client/cli/query.go @@ -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]", diff --git a/x/crud/client/rest/query.go b/x/crud/client/rest/query.go index bb200e09..adf32eec 100644 --- a/x/crud/client/rest/query.go +++ b/x/crud/client/rest/query.go @@ -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) diff --git a/x/crud/client/rest/rest.go b/x/crud/client/rest/rest.go index f70ea533..766b8eb0 100644 --- a/x/crud/client/rest/rest.go +++ b/x/crud/client/rest/rest.go @@ -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") diff --git a/x/crud/internal/keeper/querier.go b/x/crud/internal/keeper/querier.go index aeec75e6..0cf5179b 100644 --- a/x/crud/internal/keeper/querier.go +++ b/x/crud/internal/keeper/querier.go @@ -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: @@ -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]) diff --git a/x/crud/internal/types/querier.go b/x/crud/internal/types/querier.go index a7902872..789c1916 100644 --- a/x/crud/internal/types/querier.go +++ b/x/crud/internal/types/querier.go @@ -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