package rest

import (
	"fmt"
	"net/http"

	"github.com/cosmos/cosmos-sdk/client"
	"github.com/cosmos/cosmos-sdk/codec"
	sdk "github.com/cosmos/cosmos-sdk/types"
	"github.com/gorilla/mux"

	"github.com/cosmos/cosmos-sdk/types/rest"

	"github.com/joinresistance/space-pussy/x/dmn/types"
)

func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
	r.HandleFunc(
		"/dmn/params",
		queryParamsHandlerFn(cliCtx)).Methods("GET")

	r.HandleFunc(
		fmt.Sprintf("/dmn/{%s}/{%s}/thoughts", Program, Name),
		queryThoughtHandlerFn(cliCtx)).Methods("GET")

	r.HandleFunc(
		fmt.Sprintf("/dmn/{%s}/{%s}/thought_stats", Program, Name),
		queryThoughtStatsHandlerFn(cliCtx)).Methods("GET")

	r.HandleFunc(
		"/dmn/thoughts",
		queryThoughtsHandlerFn(cliCtx)).Methods("GET")

	r.HandleFunc(
		"/dmn/thoughts_stats",
		queryThoughtsStatsHandlerFn(cliCtx)).Methods("GET")
}

func queryParamsHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)

		res, _, err := cliCtx.QueryWithData(route, nil)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
			return
		}

		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func queryThoughtHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		vars := mux.Vars(r)
		name := vars[Name]

		if len(name) == 0 {
			rest.WriteErrorResponse(w, http.StatusBadRequest, "name cannot be empty")
			return
		}

		program, err := sdk.AccAddressFromBech32(vars[Program])
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
			return
		}

		params := types.NewQueryThoughtParams(
			program, name,
		)

		bz, err := codec.MarshalJSONIndent(cliCtx.LegacyAmino, params)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
			return
		}

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryThought)
		res, height, err := cliCtx.QueryWithData(route, bz)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func queryThoughtStatsHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		vars := mux.Vars(r)
		name := vars[Name]

		if len(name) == 0 {
			rest.WriteErrorResponse(w, http.StatusBadRequest, "name cannot be empty")
			return
		}

		program, err := sdk.AccAddressFromBech32(vars[Program])
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
			return
		}

		params := types.NewQueryThoughtParams(
			program, name,
		)

		bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
		if rest.CheckBadRequestError(w, err) {
			return
		}

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryThoughtStats)
		res, height, err := cliCtx.QueryWithData(route, bz)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func queryThoughtsHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryThoughts)

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

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func queryThoughtsStatsHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryThoughtsStats)

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

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

Synonyms

space-pussy/x/rank/client/cli/query.go
space-pussy/x/rank/client/rest/query.go
space-pussy/x/resources/client/rest/query.go
space-pussy/x/bandwidth/client/rest/query.go
go-cyber/x/dmn/client/cli/query.go
space-pussy/x/graph/client/rest/query.go
space-pussy/x/bandwidth/client/cli/query.go
go-cyber/x/liquidity/client/cli/query.go
go-cyber/x/grid/client/cli/query.go
space-pussy/x/grid/client/cli/query.go
space-pussy/x/graph/client/cli/query.go
go-cyber/x/resources/client/cli/query.go
go-cyber/x/clock/client/cli/query.go
space-pussy/x/dmn/client/cli/query.go
go-cyber/x/rank/client/cli/query.go
go-cyber/x/bandwidth/client/cli/query.go
go-cyber/x/tokenfactory/wasm/types/query.go
go-cyber/x/graph/client/cli/query.go
space-pussy/x/resources/client/cli/query.go
go-cyber/x/tokenfactory/client/cli/query.go
space-pussy/x/grid/client/rest/query.go

Neighbours